*** empty log message ***
[emacs.git] / src / editfns.c
blob63c1183837ca25275acb69d4cd6dad1bb48ec203
1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985,86,87,89,93,94,95,96,97,98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <sys/types.h>
25 #ifdef VMS
26 #include "vms-pwd.h"
27 #else
28 #include <pwd.h>
29 #endif
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 #include "lisp.h"
36 #include "intervals.h"
37 #include "buffer.h"
38 #include "charset.h"
39 #include "coding.h"
40 #include "window.h"
42 #include "systime.h"
44 #define min(a, b) ((a) < (b) ? (a) : (b))
45 #define max(a, b) ((a) > (b) ? (a) : (b))
47 #ifndef NULL
48 #define NULL 0
49 #endif
51 extern char **environ;
52 extern int use_dialog_box;
53 extern Lisp_Object make_time ();
54 extern void insert_from_buffer ();
55 static int tm_diff ();
56 static void update_buffer_properties ();
57 size_t emacs_strftimeu ();
58 void set_time_zone_rule ();
60 Lisp_Object Vbuffer_access_fontify_functions;
61 Lisp_Object Qbuffer_access_fontify_functions;
62 Lisp_Object Vbuffer_access_fontified_property;
64 Lisp_Object Fuser_full_name ();
66 /* Non-nil means don't stop at field boundary in text motion commands. */
68 Lisp_Object Vinhibit_field_text_motion;
70 /* Some static data, and a function to initialize it for each run */
72 Lisp_Object Vsystem_name;
73 Lisp_Object Vuser_real_login_name; /* login name of current user ID */
74 Lisp_Object Vuser_full_name; /* full name of current user */
75 Lisp_Object Vuser_login_name; /* user name from LOGNAME or USER */
77 void
78 init_editfns ()
80 char *user_name;
81 register unsigned char *p;
82 struct passwd *pw; /* password entry for the current user */
83 Lisp_Object tem;
85 /* Set up system_name even when dumping. */
86 init_system_name ();
88 #ifndef CANNOT_DUMP
89 /* Don't bother with this on initial start when just dumping out */
90 if (!initialized)
91 return;
92 #endif /* not CANNOT_DUMP */
94 pw = (struct passwd *) getpwuid (getuid ());
95 #ifdef MSDOS
96 /* We let the real user name default to "root" because that's quite
97 accurate on MSDOG and because it lets Emacs find the init file.
98 (The DVX libraries override the Djgpp libraries here.) */
99 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
100 #else
101 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
102 #endif
104 /* Get the effective user name, by consulting environment variables,
105 or the effective uid if those are unset. */
106 user_name = (char *) getenv ("LOGNAME");
107 if (!user_name)
108 #ifdef WINDOWSNT
109 user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
110 #else /* WINDOWSNT */
111 user_name = (char *) getenv ("USER");
112 #endif /* WINDOWSNT */
113 if (!user_name)
115 pw = (struct passwd *) getpwuid (geteuid ());
116 user_name = (char *) (pw ? pw->pw_name : "unknown");
118 Vuser_login_name = build_string (user_name);
120 /* If the user name claimed in the environment vars differs from
121 the real uid, use the claimed name to find the full name. */
122 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
123 Vuser_full_name = Fuser_full_name (NILP (tem)? make_number (geteuid())
124 : Vuser_login_name);
126 p = (unsigned char *) getenv ("NAME");
127 if (p)
128 Vuser_full_name = build_string (p);
129 else if (NILP (Vuser_full_name))
130 Vuser_full_name = build_string ("unknown");
133 DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
134 "Convert arg CHAR to a string containing that character.")
135 (character)
136 Lisp_Object character;
138 int len;
139 unsigned char str[MAX_MULTIBYTE_LENGTH];
141 CHECK_NUMBER (character, 0);
143 len = CHAR_STRING (XFASTINT (character), str);
144 return make_string_from_bytes (str, 1, len);
147 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
148 "Convert arg STRING to a character, the first character of that string.\n\
149 A multibyte character is handled correctly.")
150 (string)
151 register Lisp_Object string;
153 register Lisp_Object val;
154 register struct Lisp_String *p;
155 CHECK_STRING (string, 0);
156 p = XSTRING (string);
157 if (p->size)
159 if (STRING_MULTIBYTE (string))
160 XSETFASTINT (val, STRING_CHAR (p->data, STRING_BYTES (p)));
161 else
162 XSETFASTINT (val, p->data[0]);
164 else
165 XSETFASTINT (val, 0);
166 return val;
169 static Lisp_Object
170 buildmark (charpos, bytepos)
171 int charpos, bytepos;
173 register Lisp_Object mark;
174 mark = Fmake_marker ();
175 set_marker_both (mark, Qnil, charpos, bytepos);
176 return mark;
179 DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
180 "Return value of point, as an integer.\n\
181 Beginning of buffer is position (point-min)")
184 Lisp_Object temp;
185 XSETFASTINT (temp, PT);
186 return temp;
189 DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
190 "Return value of point, as a marker object.")
193 return buildmark (PT, PT_BYTE);
197 clip_to_bounds (lower, num, upper)
198 int lower, num, upper;
200 if (num < lower)
201 return lower;
202 else if (num > upper)
203 return upper;
204 else
205 return num;
208 DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
209 "Set point to POSITION, a number or marker.\n\
210 Beginning of buffer is position (point-min), end is (point-max).\n\
211 If the position is in the middle of a multibyte form,\n\
212 the actual point is set at the head of the multibyte form\n\
213 except in the case that `enable-multibyte-characters' is nil.")
214 (position)
215 register Lisp_Object position;
217 int pos;
219 if (MARKERP (position)
220 && current_buffer == XMARKER (position)->buffer)
222 pos = marker_position (position);
223 if (pos < BEGV)
224 SET_PT_BOTH (BEGV, BEGV_BYTE);
225 else if (pos > ZV)
226 SET_PT_BOTH (ZV, ZV_BYTE);
227 else
228 SET_PT_BOTH (pos, marker_byte_position (position));
230 return position;
233 CHECK_NUMBER_COERCE_MARKER (position, 0);
235 pos = clip_to_bounds (BEGV, XINT (position), ZV);
236 SET_PT (pos);
237 return position;
240 static Lisp_Object
241 region_limit (beginningp)
242 int beginningp;
244 extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */
245 register Lisp_Object m;
246 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
247 && NILP (current_buffer->mark_active))
248 Fsignal (Qmark_inactive, Qnil);
249 m = Fmarker_position (current_buffer->mark);
250 if (NILP (m)) error ("There is no region now");
251 if ((PT < XFASTINT (m)) == beginningp)
252 return (make_number (PT));
253 else
254 return (m);
257 DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
258 "Return position of beginning of region, as an integer.")
261 return (region_limit (1));
264 DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
265 "Return position of end of region, as an integer.")
268 return (region_limit (0));
271 DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
272 "Return this buffer's mark, as a marker object.\n\
273 Watch out! Moving this marker changes the mark position.\n\
274 If you set the marker not to point anywhere, the buffer will have no mark.")
277 return current_buffer->mark;
280 /* Return nonzero if POS1 and POS2 have the same value
281 for the text property PROP. */
283 static int
284 text_property_eq (prop, pos1, pos2)
285 Lisp_Object prop;
286 Lisp_Object pos1, pos2;
288 Lisp_Object pval1, pval2;
290 pval1 = Fget_text_property (pos1, prop, Qnil);
291 pval2 = Fget_text_property (pos2, prop, Qnil);
293 return EQ (pval1, pval2);
296 /* Return the direction from which the text-property PROP would be
297 inherited by any new text inserted at POS: 1 if it would be
298 inherited from the char after POS, -1 if it would be inherited from
299 the char before POS, and 0 if from neither. */
301 static int
302 text_property_stickiness (prop, pos)
303 Lisp_Object prop;
304 Lisp_Object pos;
306 Lisp_Object front_sticky;
308 if (XINT (pos) > BEGV)
309 /* Consider previous character. */
311 Lisp_Object prev_pos, rear_non_sticky;
313 prev_pos = make_number (XINT (pos) - 1);
314 rear_non_sticky = Fget_text_property (prev_pos, Qrear_nonsticky, Qnil);
316 if (EQ (rear_non_sticky, Qnil)
317 || (CONSP (rear_non_sticky)
318 && NILP (Fmemq (prop, rear_non_sticky))))
319 /* PROP is not rear-non-sticky, and since this takes precedence over
320 any front-stickiness, PROP is inherited from before. */
321 return -1;
324 /* Consider following character. */
325 front_sticky = Fget_text_property (pos, Qfront_sticky, Qnil);
327 if (EQ (front_sticky, Qt)
328 || (CONSP (front_sticky)
329 && !NILP (Fmemq (prop, front_sticky))))
330 /* PROP is inherited from after. */
331 return 1;
333 /* PROP is not inherited from either side. */
334 return 0;
337 /* Symbol for the text property used to mark fields. */
338 Lisp_Object Qfield;
340 /* Find the field surrounding POS in *BEG and *END. If POS is nil,
341 the value of point is used instead.
343 If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
344 position of a field, then the beginning of the previous field
345 is returned instead of the beginning of POS's field (since the end of
346 a field is actually also the beginning of the next input
347 field, this behavior is sometimes useful).
349 Either BEG or END may be 0, in which case the corresponding value
350 is not stored. */
352 void
353 find_field (pos, merge_at_boundary, beg, end)
354 Lisp_Object pos;
355 Lisp_Object merge_at_boundary;
356 int *beg, *end;
358 /* 1 if POS counts as the start of a field. */
359 int at_field_start = 0;
360 /* 1 if POS counts as the end of a field. */
361 int at_field_end = 0;
363 if (NILP (pos))
364 XSETFASTINT (pos, PT);
365 else
366 CHECK_NUMBER_COERCE_MARKER (pos, 0);
368 if (NILP (merge_at_boundary) && XFASTINT (pos) > BEGV)
369 /* See if we need to handle the case where POS is at beginning of a
370 field, which can also be interpreted as the end of the previous
371 field. We decide which one by seeing which field the `field'
372 property sticks to. The case where if MERGE_AT_BOUNDARY is
373 non-nil (see function comment) is actually the more natural one;
374 then we avoid treating the beginning of a field specially. */
376 /* First see if POS is actually *at* a boundary. */
377 Lisp_Object after_field, before_field;
379 after_field = Fget_text_property (pos, Qfield, Qnil);
380 before_field = Fget_text_property (make_number (XINT (pos) - 1),
381 Qfield, Qnil);
383 if (! EQ (after_field, before_field))
384 /* We are at a boundary, see which direction is inclusive. */
386 int stickiness = text_property_stickiness (Qfield, pos);
388 if (stickiness > 0)
389 at_field_start = 1;
390 else if (stickiness < 0)
391 at_field_end = 1;
392 else
393 /* STICKINESS == 0 means that any inserted text will get a
394 `field' text-property of nil, so check to see if that
395 matches either of the adjacent characters (this being a
396 kind of "stickiness by default"). */
398 if (NILP (before_field))
399 at_field_end = 1; /* Sticks to the left. */
400 else if (NILP (after_field))
401 at_field_start = 1; /* Sticks to the right. */
406 if (beg)
408 if (at_field_start)
409 /* POS is at the edge of a field, and we should consider it as
410 the beginning of the following field. */
411 *beg = XFASTINT (pos);
412 else
413 /* Find the previous field boundary. */
415 Lisp_Object prev;
416 prev = Fprevious_single_property_change (pos, Qfield, Qnil, Qnil);
417 *beg = NILP (prev) ? BEGV : XFASTINT (prev);
421 if (end)
423 if (at_field_end)
424 /* POS is at the edge of a field, and we should consider it as
425 the end of the previous field. */
426 *end = XFASTINT (pos);
427 else
428 /* Find the next field boundary. */
430 Lisp_Object next;
431 next = Fnext_single_property_change (pos, Qfield, Qnil, Qnil);
432 *end = NILP (next) ? ZV : XFASTINT (next);
437 DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0,
438 "Delete the field surrounding POS.\n\
439 A field is a region of text with the same `field' property.\n\
440 If POS is nil, the value of point is used for POS.")
441 (pos)
442 Lisp_Object pos;
444 int beg, end;
445 find_field (pos, Qnil, &beg, &end);
446 if (beg != end)
447 del_range (beg, end);
448 return Qnil;
451 DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0,
452 "Return the contents of the field surrounding POS as a string.\n\
453 A field is a region of text with the same `field' property.\n\
454 If POS is nil, the value of point is used for POS.")
455 (pos)
456 Lisp_Object pos;
458 int beg, end;
459 find_field (pos, Qnil, &beg, &end);
460 return make_buffer_string (beg, end, 1);
463 DEFUN ("field-string-no-properties", Ffield_string_no_properties, Sfield_string_no_properties, 0, 1, 0,
464 "Return the contents of the field around POS, without text-properties.\n\
465 A field is a region of text with the same `field' property.\n\
466 If POS is nil, the value of point is used for POS.")
467 (pos)
468 Lisp_Object pos;
470 int beg, end;
471 find_field (pos, Qnil, &beg, &end);
472 return make_buffer_string (beg, end, 0);
475 DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 2, 0,
476 "Return the beginning of the field surrounding POS.\n\
477 A field is a region of text with the same `field' property.\n\
478 If POS is nil, the value of point is used for POS.\n\
479 If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its\n\
480 field, then the beginning of the *previous* field is returned.")
481 (pos, escape_from_edge)
482 Lisp_Object pos, escape_from_edge;
484 int beg;
485 find_field (pos, escape_from_edge, &beg, 0);
486 return make_number (beg);
489 DEFUN ("field-end", Ffield_end, Sfield_end, 0, 2, 0,
490 "Return the end of the field surrounding POS.\n\
491 A field is a region of text with the same `field' property.\n\
492 If POS is nil, the value of point is used for POS.\n\
493 If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,\n\
494 then the end of the *following* field is returned.")
495 (pos, escape_from_edge)
496 Lisp_Object pos, escape_from_edge;
498 int end;
499 find_field (pos, escape_from_edge, 0, &end);
500 return make_number (end);
503 DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 4, 0,
504 "Return the position closest to NEW-POS that is in the same field as OLD-POS.\n\
505 A field is a region of text with the same `field' property.\n\
506 If NEW-POS is nil, then the current point is used instead, and set to the\n\
507 constrained position if that is is different.\n\
509 If OLD-POS is at the boundary of two fields, then the allowable\n\
510 positions for NEW-POS depends on the value of the optional argument\n\
511 ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is\n\
512 constrained to the field that has the same `field' text-property\n\
513 as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE\n\
514 is non-nil, NEW-POS is constrained to the union of the two adjacent\n\
515 fields.\n\
517 If the optional argument ONLY-IN-LINE is non-nil and constraining\n\
518 NEW-POS would move it to a different line, NEW-POS is returned\n\
519 unconstrained. This useful for commands that move by line, like\n\
520 \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries\n\
521 only in the case where they can still move to the right line.\n\
523 Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil.")
524 (new_pos, old_pos, escape_from_edge, only_in_line)
525 Lisp_Object new_pos, old_pos, escape_from_edge, only_in_line;
527 /* If non-zero, then the original point, before re-positioning. */
528 int orig_point = 0;
530 if (NILP (new_pos))
531 /* Use the current point, and afterwards, set it. */
533 orig_point = PT;
534 XSETFASTINT (new_pos, PT);
537 if (NILP (Vinhibit_field_text_motion)
538 && !EQ (new_pos, old_pos)
539 && !text_property_eq (Qfield, new_pos, old_pos))
540 /* NEW_POS is not within the same field as OLD_POS; try to
541 move NEW_POS so that it is. */
543 int fwd;
544 Lisp_Object field_bound;
546 CHECK_NUMBER_COERCE_MARKER (new_pos, 0);
547 CHECK_NUMBER_COERCE_MARKER (old_pos, 0);
549 fwd = (XFASTINT (new_pos) > XFASTINT (old_pos));
551 if (fwd)
552 field_bound = Ffield_end (old_pos, escape_from_edge);
553 else
554 field_bound = Ffield_beginning (old_pos, escape_from_edge);
556 if (/* If ONLY_IN_LINE is non-nil, we only constrain NEW_POS if doing
557 so would remain within the same line. */
558 NILP (only_in_line)
559 /* In that case, see if ESCAPE_FROM_EDGE caused FIELD_BOUND
560 to jump to the other side of NEW_POS, which would mean
561 that NEW_POS is already acceptable, and that we don't
562 have to do the line-check. */
563 || ((XFASTINT (field_bound) < XFASTINT (new_pos)) ? !fwd : fwd)
564 /* If not, see if there's no newline intervening between
565 NEW_POS and FIELD_BOUND. */
566 || (find_before_next_newline (XFASTINT (new_pos),
567 XFASTINT (field_bound),
568 fwd ? -1 : 1)
569 == XFASTINT (field_bound)))
570 /* Constrain NEW_POS to FIELD_BOUND. */
571 new_pos = field_bound;
573 if (orig_point && XFASTINT (new_pos) != orig_point)
574 /* The NEW_POS argument was originally nil, so automatically set PT. */
575 SET_PT (XFASTINT (new_pos));
578 return new_pos;
581 DEFUN ("line-beginning-position", Fline_beginning_position, Sline_beginning_position,
582 0, 1, 0,
583 "Return the character position of the first character on the current line.\n\
584 With argument N not nil or 1, move forward N - 1 lines first.\n\
585 If scan reaches end of buffer, return that position.\n\
586 The scan does not cross a field boundary unless it would move\n\
587 beyond there to a different line. Field boundaries are not noticed if\n\
588 `inhibit-field-text-motion' is non-nil. .And if N is nil or 1,\n\
589 and scan starts at a field boundary, the scan stops as soon as it starts.\n\
591 This function does not move point.")
593 Lisp_Object n;
595 register int orig, orig_byte, end;
597 if (NILP (n))
598 XSETFASTINT (n, 1);
599 else
600 CHECK_NUMBER (n, 0);
602 orig = PT;
603 orig_byte = PT_BYTE;
604 Fforward_line (make_number (XINT (n) - 1));
605 end = PT;
607 SET_PT_BOTH (orig, orig_byte);
609 /* Return END constrained to the current input field. */
610 return Fconstrain_to_field (make_number (end), make_number (orig),
611 XINT (n) != 1 ? Qt : Qnil,
612 Qt);
615 DEFUN ("line-end-position", Fline_end_position, Sline_end_position,
616 0, 1, 0,
617 "Return the character position of the last character on the current line.\n\
618 With argument N not nil or 1, move forward N - 1 lines first.\n\
619 If scan reaches end of buffer, return that position.\n\
620 This function does not move point.")
622 Lisp_Object n;
624 int end_pos;
625 register int orig = PT;
627 if (NILP (n))
628 XSETFASTINT (n, 1);
629 else
630 CHECK_NUMBER (n, 0);
632 end_pos = find_before_next_newline (orig, 0, XINT (n) - (XINT (n) <= 0));
634 /* Return END_POS constrained to the current input field. */
635 return Fconstrain_to_field (make_number (end_pos), make_number (orig),
636 Qnil, Qt);
639 Lisp_Object
640 save_excursion_save ()
642 register int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
643 == current_buffer);
645 return Fcons (Fpoint_marker (),
646 Fcons (Fcopy_marker (current_buffer->mark, Qnil),
647 Fcons (visible ? Qt : Qnil,
648 current_buffer->mark_active)));
651 Lisp_Object
652 save_excursion_restore (info)
653 Lisp_Object info;
655 Lisp_Object tem, tem1, omark, nmark;
656 struct gcpro gcpro1, gcpro2, gcpro3;
658 tem = Fmarker_buffer (Fcar (info));
659 /* If buffer being returned to is now deleted, avoid error */
660 /* Otherwise could get error here while unwinding to top level
661 and crash */
662 /* In that case, Fmarker_buffer returns nil now. */
663 if (NILP (tem))
664 return Qnil;
666 omark = nmark = Qnil;
667 GCPRO3 (info, omark, nmark);
669 Fset_buffer (tem);
670 tem = Fcar (info);
671 Fgoto_char (tem);
672 unchain_marker (tem);
673 tem = Fcar (Fcdr (info));
674 omark = Fmarker_position (current_buffer->mark);
675 Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
676 nmark = Fmarker_position (tem);
677 unchain_marker (tem);
678 tem = Fcdr (Fcdr (info));
679 #if 0 /* We used to make the current buffer visible in the selected window
680 if that was true previously. That avoids some anomalies.
681 But it creates others, and it wasn't documented, and it is simpler
682 and cleaner never to alter the window/buffer connections. */
683 tem1 = Fcar (tem);
684 if (!NILP (tem1)
685 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
686 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
687 #endif /* 0 */
689 tem1 = current_buffer->mark_active;
690 current_buffer->mark_active = Fcdr (tem);
691 if (!NILP (Vrun_hooks))
693 /* If mark is active now, and either was not active
694 or was at a different place, run the activate hook. */
695 if (! NILP (current_buffer->mark_active))
697 if (! EQ (omark, nmark))
698 call1 (Vrun_hooks, intern ("activate-mark-hook"));
700 /* If mark has ceased to be active, run deactivate hook. */
701 else if (! NILP (tem1))
702 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
704 UNGCPRO;
705 return Qnil;
708 DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
709 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
710 Executes BODY just like `progn'.\n\
711 The values of point, mark and the current buffer are restored\n\
712 even in case of abnormal exit (throw or error).\n\
713 The state of activation of the mark is also restored.\n\
715 This construct does not save `deactivate-mark', and therefore\n\
716 functions that change the buffer will still cause deactivation\n\
717 of the mark at the end of the command. To prevent that, bind\n\
718 `deactivate-mark' with `let'.")
719 (args)
720 Lisp_Object args;
722 register Lisp_Object val;
723 int count = specpdl_ptr - specpdl;
725 record_unwind_protect (save_excursion_restore, save_excursion_save ());
727 val = Fprogn (args);
728 return unbind_to (count, val);
731 DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0,
732 "Save the current buffer; execute BODY; restore the current buffer.\n\
733 Executes BODY just like `progn'.")
734 (args)
735 Lisp_Object args;
737 register Lisp_Object val;
738 int count = specpdl_ptr - specpdl;
740 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
742 val = Fprogn (args);
743 return unbind_to (count, val);
746 DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0,
747 "Return the number of characters in the current buffer.\n\
748 If BUFFER, return the number of characters in that buffer instead.")
749 (buffer)
750 Lisp_Object buffer;
752 if (NILP (buffer))
753 return make_number (Z - BEG);
754 else
756 CHECK_BUFFER (buffer, 1);
757 return make_number (BUF_Z (XBUFFER (buffer))
758 - BUF_BEG (XBUFFER (buffer)));
762 DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
763 "Return the minimum permissible value of point in the current buffer.\n\
764 This is 1, unless narrowing (a buffer restriction) is in effect.")
767 Lisp_Object temp;
768 XSETFASTINT (temp, BEGV);
769 return temp;
772 DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
773 "Return a marker to the minimum permissible value of point in this buffer.\n\
774 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
777 return buildmark (BEGV, BEGV_BYTE);
780 DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
781 "Return the maximum permissible value of point in the current buffer.\n\
782 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
783 is in effect, in which case it is less.")
786 Lisp_Object temp;
787 XSETFASTINT (temp, ZV);
788 return temp;
791 DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
792 "Return a marker to the maximum permissible value of point in this buffer.\n\
793 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
794 is in effect, in which case it is less.")
797 return buildmark (ZV, ZV_BYTE);
800 DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0,
801 "Return the position of the gap, in the current buffer.\n\
802 See also `gap-size'.")
805 Lisp_Object temp;
806 XSETFASTINT (temp, GPT);
807 return temp;
810 DEFUN ("gap-size", Fgap_size, Sgap_size, 0, 0, 0,
811 "Return the size of the current buffer's gap.\n\
812 See also `gap-position'.")
815 Lisp_Object temp;
816 XSETFASTINT (temp, GAP_SIZE);
817 return temp;
820 DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
821 "Return the byte position for character position POSITION.\n\
822 If POSITION is out of range, the value is nil.")
823 (position)
824 Lisp_Object position;
826 CHECK_NUMBER_COERCE_MARKER (position, 1);
827 if (XINT (position) < BEG || XINT (position) > Z)
828 return Qnil;
829 return make_number (CHAR_TO_BYTE (XINT (position)));
832 DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
833 "Return the character position for byte position BYTEPOS.\n\
834 If BYTEPOS is out of range, the value is nil.")
835 (bytepos)
836 Lisp_Object bytepos;
838 CHECK_NUMBER (bytepos, 1);
839 if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
840 return Qnil;
841 return make_number (BYTE_TO_CHAR (XINT (bytepos)));
844 DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
845 "Return the character following point, as a number.\n\
846 At the end of the buffer or accessible region, return 0.")
849 Lisp_Object temp;
850 if (PT >= ZV)
851 XSETFASTINT (temp, 0);
852 else
853 XSETFASTINT (temp, FETCH_CHAR (PT_BYTE));
854 return temp;
857 DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
858 "Return the character preceding point, as a number.\n\
859 At the beginning of the buffer or accessible region, return 0.")
862 Lisp_Object temp;
863 if (PT <= BEGV)
864 XSETFASTINT (temp, 0);
865 else if (!NILP (current_buffer->enable_multibyte_characters))
867 int pos = PT_BYTE;
868 DEC_POS (pos);
869 XSETFASTINT (temp, FETCH_CHAR (pos));
871 else
872 XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1));
873 return temp;
876 DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
877 "Return t if point is at the beginning of the buffer.\n\
878 If the buffer is narrowed, this means the beginning of the narrowed part.")
881 if (PT == BEGV)
882 return Qt;
883 return Qnil;
886 DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
887 "Return t if point is at the end of the buffer.\n\
888 If the buffer is narrowed, this means the end of the narrowed part.")
891 if (PT == ZV)
892 return Qt;
893 return Qnil;
896 DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
897 "Return t if point is at the beginning of a line.")
900 if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
901 return Qt;
902 return Qnil;
905 DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
906 "Return t if point is at the end of a line.\n\
907 `End of a line' includes point being at the end of the buffer.")
910 if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
911 return Qt;
912 return Qnil;
915 DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
916 "Return character in current buffer at position POS.\n\
917 POS is an integer or a marker.\n\
918 If POS is out of range, the value is nil.")
919 (pos)
920 Lisp_Object pos;
922 register int pos_byte;
924 if (NILP (pos))
926 pos_byte = PT_BYTE;
927 XSETFASTINT (pos, PT);
930 if (MARKERP (pos))
932 pos_byte = marker_byte_position (pos);
933 if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE)
934 return Qnil;
936 else
938 CHECK_NUMBER_COERCE_MARKER (pos, 0);
939 if (XINT (pos) < BEGV || XINT (pos) >= ZV)
940 return Qnil;
942 pos_byte = CHAR_TO_BYTE (XINT (pos));
945 return make_number (FETCH_CHAR (pos_byte));
948 DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0,
949 "Return character in current buffer preceding position POS.\n\
950 POS is an integer or a marker.\n\
951 If POS is out of range, the value is nil.")
952 (pos)
953 Lisp_Object pos;
955 register Lisp_Object val;
956 register int pos_byte;
958 if (NILP (pos))
960 pos_byte = PT_BYTE;
961 XSETFASTINT (pos, PT);
964 if (MARKERP (pos))
966 pos_byte = marker_byte_position (pos);
968 if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE)
969 return Qnil;
971 else
973 CHECK_NUMBER_COERCE_MARKER (pos, 0);
975 if (XINT (pos) <= BEGV || XINT (pos) > ZV)
976 return Qnil;
978 pos_byte = CHAR_TO_BYTE (XINT (pos));
981 if (!NILP (current_buffer->enable_multibyte_characters))
983 DEC_POS (pos_byte);
984 XSETFASTINT (val, FETCH_CHAR (pos_byte));
986 else
988 pos_byte--;
989 XSETFASTINT (val, FETCH_BYTE (pos_byte));
991 return val;
994 DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
995 "Return the name under which the user logged in, as a string.\n\
996 This is based on the effective uid, not the real uid.\n\
997 Also, if the environment variable LOGNAME or USER is set,\n\
998 that determines the value of this function.\n\n\
999 If optional argument UID is an integer, return the login name of the user\n\
1000 with that uid, or nil if there is no such user.")
1001 (uid)
1002 Lisp_Object uid;
1004 struct passwd *pw;
1006 /* Set up the user name info if we didn't do it before.
1007 (That can happen if Emacs is dumpable
1008 but you decide to run `temacs -l loadup' and not dump. */
1009 if (INTEGERP (Vuser_login_name))
1010 init_editfns ();
1012 if (NILP (uid))
1013 return Vuser_login_name;
1015 CHECK_NUMBER (uid, 0);
1016 pw = (struct passwd *) getpwuid (XINT (uid));
1017 return (pw ? build_string (pw->pw_name) : Qnil);
1020 DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
1021 0, 0, 0,
1022 "Return the name of the user's real uid, as a string.\n\
1023 This ignores the environment variables LOGNAME and USER, so it differs from\n\
1024 `user-login-name' when running under `su'.")
1027 /* Set up the user name info if we didn't do it before.
1028 (That can happen if Emacs is dumpable
1029 but you decide to run `temacs -l loadup' and not dump. */
1030 if (INTEGERP (Vuser_login_name))
1031 init_editfns ();
1032 return Vuser_real_login_name;
1035 DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
1036 "Return the effective uid of Emacs, as an integer.")
1039 return make_number (geteuid ());
1042 DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
1043 "Return the real uid of Emacs, as an integer.")
1046 return make_number (getuid ());
1049 DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0,
1050 "Return the full name of the user logged in, as a string.\n\
1051 If the full name corresponding to Emacs's userid is not known,\n\
1052 return \"unknown\".\n\
1054 If optional argument UID is an integer, return the full name of the user\n\
1055 with that uid, or nil if there is no such user.\n\
1056 If UID is a string, return the full name of the user with that login\n\
1057 name, or nil if there is no such user.")
1058 (uid)
1059 Lisp_Object uid;
1061 struct passwd *pw;
1062 register unsigned char *p, *q;
1063 extern char *index ();
1064 Lisp_Object full;
1066 if (NILP (uid))
1067 return Vuser_full_name;
1068 else if (NUMBERP (uid))
1069 pw = (struct passwd *) getpwuid (XINT (uid));
1070 else if (STRINGP (uid))
1071 pw = (struct passwd *) getpwnam (XSTRING (uid)->data);
1072 else
1073 error ("Invalid UID specification");
1075 if (!pw)
1076 return Qnil;
1078 p = (unsigned char *) USER_FULL_NAME;
1079 /* Chop off everything after the first comma. */
1080 q = (unsigned char *) index (p, ',');
1081 full = make_string (p, q ? q - p : strlen (p));
1083 #ifdef AMPERSAND_FULL_NAME
1084 p = XSTRING (full)->data;
1085 q = (unsigned char *) index (p, '&');
1086 /* Substitute the login name for the &, upcasing the first character. */
1087 if (q)
1089 register unsigned char *r;
1090 Lisp_Object login;
1092 login = Fuser_login_name (make_number (pw->pw_uid));
1093 r = (unsigned char *) alloca (strlen (p) + XSTRING (login)->size + 1);
1094 bcopy (p, r, q - p);
1095 r[q - p] = 0;
1096 strcat (r, XSTRING (login)->data);
1097 r[q - p] = UPCASE (r[q - p]);
1098 strcat (r, q + 1);
1099 full = build_string (r);
1101 #endif /* AMPERSAND_FULL_NAME */
1103 return full;
1106 DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
1107 "Return the name of the machine you are running on, as a string.")
1110 return Vsystem_name;
1113 /* For the benefit of callers who don't want to include lisp.h */
1114 char *
1115 get_system_name ()
1117 if (STRINGP (Vsystem_name))
1118 return (char *) XSTRING (Vsystem_name)->data;
1119 else
1120 return "";
1123 DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
1124 "Return the process ID of Emacs, as an integer.")
1127 return make_number (getpid ());
1130 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
1131 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
1132 The time is returned as a list of three integers. The first has the\n\
1133 most significant 16 bits of the seconds, while the second has the\n\
1134 least significant 16 bits. The third integer gives the microsecond\n\
1135 count.\n\
1137 The microsecond count is zero on systems that do not provide\n\
1138 resolution finer than a second.")
1141 EMACS_TIME t;
1142 Lisp_Object result[3];
1144 EMACS_GET_TIME (t);
1145 XSETINT (result[0], (EMACS_SECS (t) >> 16) & 0xffff);
1146 XSETINT (result[1], (EMACS_SECS (t) >> 0) & 0xffff);
1147 XSETINT (result[2], EMACS_USECS (t));
1149 return Flist (3, result);
1153 static int
1154 lisp_time_argument (specified_time, result)
1155 Lisp_Object specified_time;
1156 time_t *result;
1158 if (NILP (specified_time))
1159 return time (result) != -1;
1160 else
1162 Lisp_Object high, low;
1163 high = Fcar (specified_time);
1164 CHECK_NUMBER (high, 0);
1165 low = Fcdr (specified_time);
1166 if (CONSP (low))
1167 low = Fcar (low);
1168 CHECK_NUMBER (low, 0);
1169 *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
1170 return *result >> 16 == XINT (high);
1174 /* Write information into buffer S of size MAXSIZE, according to the
1175 FORMAT of length FORMAT_LEN, using time information taken from *TP.
1176 Default to Universal Time if UT is nonzero, local time otherwise.
1177 Return the number of bytes written, not including the terminating
1178 '\0'. If S is NULL, nothing will be written anywhere; so to
1179 determine how many bytes would be written, use NULL for S and
1180 ((size_t) -1) for MAXSIZE.
1182 This function behaves like emacs_strftimeu, except it allows null
1183 bytes in FORMAT. */
1184 static size_t
1185 emacs_memftimeu (s, maxsize, format, format_len, tp, ut)
1186 char *s;
1187 size_t maxsize;
1188 const char *format;
1189 size_t format_len;
1190 const struct tm *tp;
1191 int ut;
1193 size_t total = 0;
1195 /* Loop through all the null-terminated strings in the format
1196 argument. Normally there's just one null-terminated string, but
1197 there can be arbitrarily many, concatenated together, if the
1198 format contains '\0' bytes. emacs_strftimeu stops at the first
1199 '\0' byte so we must invoke it separately for each such string. */
1200 for (;;)
1202 size_t len;
1203 size_t result;
1205 if (s)
1206 s[0] = '\1';
1208 result = emacs_strftimeu (s, maxsize, format, tp, ut);
1210 if (s)
1212 if (result == 0 && s[0] != '\0')
1213 return 0;
1214 s += result + 1;
1217 maxsize -= result + 1;
1218 total += result;
1219 len = strlen (format);
1220 if (len == format_len)
1221 return total;
1222 total++;
1223 format += len + 1;
1224 format_len -= len + 1;
1229 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
1230 "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
1231 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
1232 `current-time' or `file-attributes'.\n\
1233 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
1234 as Universal Time; nil means describe TIME in the local time zone.\n\
1235 The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
1236 by text that describes the specified date and time in TIME:\n\
1238 %Y is the year, %y within the century, %C the century.\n\
1239 %G is the year corresponding to the ISO week, %g within the century.\n\
1240 %m is the numeric month.\n\
1241 %b and %h are the locale's abbreviated month name, %B the full name.\n\
1242 %d is the day of the month, zero-padded, %e is blank-padded.\n\
1243 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\
1244 %a is the locale's abbreviated name of the day of week, %A the full name.\n\
1245 %U is the week number starting on Sunday, %W starting on Monday,\n\
1246 %V according to ISO 8601.\n\
1247 %j is the day of the year.\n\
1249 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\
1250 only blank-padded, %l is like %I blank-padded.\n\
1251 %p is the locale's equivalent of either AM or PM.\n\
1252 %M is the minute.\n\
1253 %S is the second.\n\
1254 %Z is the time zone name, %z is the numeric form.\n\
1255 %s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\
1257 %c is the locale's date and time format.\n\
1258 %x is the locale's \"preferred\" date format.\n\
1259 %D is like \"%m/%d/%y\".\n\
1261 %R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\
1262 %X is the locale's \"preferred\" time format.\n\
1264 Finally, %n is a newline, %t is a tab, %% is a literal %.\n\
1266 Certain flags and modifiers are available with some format controls.\n\
1267 The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
1268 but padded with blanks; %-X is like %X, but without padding.\n\
1269 %NX (where N stands for an integer) is like %X,\n\
1270 but takes up at least N (a number) positions.\n\
1271 The modifiers are `E' and `O'. For certain characters X,\n\
1272 %EX is a locale's alternative version of %X;\n\
1273 %OX is like %X, but uses the locale's number symbols.\n\
1275 For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
1276 (format_string, time, universal)
1279 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
1280 0 /* See immediately above */)
1281 (format_string, time, universal)
1282 Lisp_Object format_string, time, universal;
1284 time_t value;
1285 int size;
1286 struct tm *tm;
1287 int ut = ! NILP (universal);
1289 CHECK_STRING (format_string, 1);
1291 if (! lisp_time_argument (time, &value))
1292 error ("Invalid time specification");
1294 format_string = code_convert_string_norecord (format_string,
1295 Vlocale_coding_system, 1);
1297 /* This is probably enough. */
1298 size = STRING_BYTES (XSTRING (format_string)) * 6 + 50;
1300 tm = ut ? gmtime (&value) : localtime (&value);
1301 if (! tm)
1302 error ("Specified time is not representable");
1304 synchronize_system_time_locale ();
1306 while (1)
1308 char *buf = (char *) alloca (size + 1);
1309 int result;
1311 buf[0] = '\1';
1312 result = emacs_memftimeu (buf, size, XSTRING (format_string)->data,
1313 STRING_BYTES (XSTRING (format_string)),
1314 tm, ut);
1315 if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
1316 return code_convert_string_norecord (make_string (buf, result),
1317 Vlocale_coding_system, 0);
1319 /* If buffer was too small, make it bigger and try again. */
1320 result = emacs_memftimeu (NULL, (size_t) -1,
1321 XSTRING (format_string)->data,
1322 STRING_BYTES (XSTRING (format_string)),
1323 tm, ut);
1324 size = result + 1;
1328 DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
1329 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
1330 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
1331 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
1332 to use the current time. The list has the following nine members:\n\
1333 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
1334 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
1335 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
1336 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
1337 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
1338 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
1339 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
1340 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
1341 (specified_time)
1342 Lisp_Object specified_time;
1344 time_t time_spec;
1345 struct tm save_tm;
1346 struct tm *decoded_time;
1347 Lisp_Object list_args[9];
1349 if (! lisp_time_argument (specified_time, &time_spec))
1350 error ("Invalid time specification");
1352 decoded_time = localtime (&time_spec);
1353 if (! decoded_time)
1354 error ("Specified time is not representable");
1355 XSETFASTINT (list_args[0], decoded_time->tm_sec);
1356 XSETFASTINT (list_args[1], decoded_time->tm_min);
1357 XSETFASTINT (list_args[2], decoded_time->tm_hour);
1358 XSETFASTINT (list_args[3], decoded_time->tm_mday);
1359 XSETFASTINT (list_args[4], decoded_time->tm_mon + 1);
1360 XSETINT (list_args[5], decoded_time->tm_year + 1900);
1361 XSETFASTINT (list_args[6], decoded_time->tm_wday);
1362 list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
1364 /* Make a copy, in case gmtime modifies the struct. */
1365 save_tm = *decoded_time;
1366 decoded_time = gmtime (&time_spec);
1367 if (decoded_time == 0)
1368 list_args[8] = Qnil;
1369 else
1370 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
1371 return Flist (9, list_args);
1374 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
1375 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
1376 This is the reverse operation of `decode-time', which see.\n\
1377 ZONE defaults to the current time zone rule. This can\n\
1378 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
1379 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
1380 applied without consideration for daylight savings time.\n\
1382 You can pass more than 7 arguments; then the first six arguments\n\
1383 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
1384 The intervening arguments are ignored.\n\
1385 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
1387 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
1388 for example, a DAY of 0 means the day preceding the given month.\n\
1389 Year numbers less than 100 are treated just like other year numbers.\n\
1390 If you want them to stand for years in this century, you must do that yourself.")
1391 (nargs, args)
1392 int nargs;
1393 register Lisp_Object *args;
1395 time_t time;
1396 struct tm tm;
1397 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
1399 CHECK_NUMBER (args[0], 0); /* second */
1400 CHECK_NUMBER (args[1], 1); /* minute */
1401 CHECK_NUMBER (args[2], 2); /* hour */
1402 CHECK_NUMBER (args[3], 3); /* day */
1403 CHECK_NUMBER (args[4], 4); /* month */
1404 CHECK_NUMBER (args[5], 5); /* year */
1406 tm.tm_sec = XINT (args[0]);
1407 tm.tm_min = XINT (args[1]);
1408 tm.tm_hour = XINT (args[2]);
1409 tm.tm_mday = XINT (args[3]);
1410 tm.tm_mon = XINT (args[4]) - 1;
1411 tm.tm_year = XINT (args[5]) - 1900;
1412 tm.tm_isdst = -1;
1414 if (CONSP (zone))
1415 zone = Fcar (zone);
1416 if (NILP (zone))
1417 time = mktime (&tm);
1418 else
1420 char tzbuf[100];
1421 char *tzstring;
1422 char **oldenv = environ, **newenv;
1424 if (EQ (zone, Qt))
1425 tzstring = "UTC0";
1426 else if (STRINGP (zone))
1427 tzstring = (char *) XSTRING (zone)->data;
1428 else if (INTEGERP (zone))
1430 int abszone = abs (XINT (zone));
1431 sprintf (tzbuf, "XXX%s%d:%02d:%02d", "-" + (XINT (zone) < 0),
1432 abszone / (60*60), (abszone/60) % 60, abszone % 60);
1433 tzstring = tzbuf;
1435 else
1436 error ("Invalid time zone specification");
1438 /* Set TZ before calling mktime; merely adjusting mktime's returned
1439 value doesn't suffice, since that would mishandle leap seconds. */
1440 set_time_zone_rule (tzstring);
1442 time = mktime (&tm);
1444 /* Restore TZ to previous value. */
1445 newenv = environ;
1446 environ = oldenv;
1447 xfree (newenv);
1448 #ifdef LOCALTIME_CACHE
1449 tzset ();
1450 #endif
1453 if (time == (time_t) -1)
1454 error ("Specified time is not representable");
1456 return make_time (time);
1459 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
1460 "Return the current time, as a human-readable string.\n\
1461 Programs can use this function to decode a time,\n\
1462 since the number of columns in each field is fixed.\n\
1463 The format is `Sun Sep 16 01:03:52 1973'.\n\
1464 However, see also the functions `decode-time' and `format-time-string'\n\
1465 which provide a much more powerful and general facility.\n\
1467 If an argument is given, it specifies a time to format\n\
1468 instead of the current time. The argument should have the form:\n\
1469 (HIGH . LOW)\n\
1470 or the form:\n\
1471 (HIGH LOW . IGNORED).\n\
1472 Thus, you can use times obtained from `current-time'\n\
1473 and from `file-attributes'.")
1474 (specified_time)
1475 Lisp_Object specified_time;
1477 time_t value;
1478 char buf[30];
1479 register char *tem;
1481 if (! lisp_time_argument (specified_time, &value))
1482 value = -1;
1483 tem = (char *) ctime (&value);
1485 strncpy (buf, tem, 24);
1486 buf[24] = 0;
1488 return build_string (buf);
1491 #define TM_YEAR_BASE 1900
1493 /* Yield A - B, measured in seconds.
1494 This function is copied from the GNU C Library. */
1495 static int
1496 tm_diff (a, b)
1497 struct tm *a, *b;
1499 /* Compute intervening leap days correctly even if year is negative.
1500 Take care to avoid int overflow in leap day calculations,
1501 but it's OK to assume that A and B are close to each other. */
1502 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
1503 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
1504 int a100 = a4 / 25 - (a4 % 25 < 0);
1505 int b100 = b4 / 25 - (b4 % 25 < 0);
1506 int a400 = a100 >> 2;
1507 int b400 = b100 >> 2;
1508 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
1509 int years = a->tm_year - b->tm_year;
1510 int days = (365 * years + intervening_leap_days
1511 + (a->tm_yday - b->tm_yday));
1512 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
1513 + (a->tm_min - b->tm_min))
1514 + (a->tm_sec - b->tm_sec));
1517 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
1518 "Return the offset and name for the local time zone.\n\
1519 This returns a list of the form (OFFSET NAME).\n\
1520 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1521 A negative value means west of Greenwich.\n\
1522 NAME is a string giving the name of the time zone.\n\
1523 If an argument is given, it specifies when the time zone offset is determined\n\
1524 instead of using the current time. The argument should have the form:\n\
1525 (HIGH . LOW)\n\
1526 or the form:\n\
1527 (HIGH LOW . IGNORED).\n\
1528 Thus, you can use times obtained from `current-time'\n\
1529 and from `file-attributes'.\n\
1531 Some operating systems cannot provide all this information to Emacs;\n\
1532 in this case, `current-time-zone' returns a list containing nil for\n\
1533 the data it can't find.")
1534 (specified_time)
1535 Lisp_Object specified_time;
1537 time_t value;
1538 struct tm *t;
1539 struct tm gmt;
1541 if (lisp_time_argument (specified_time, &value)
1542 && (t = gmtime (&value)) != 0
1543 && (gmt = *t, t = localtime (&value)) != 0)
1545 int offset = tm_diff (t, &gmt);
1546 char *s = 0;
1547 char buf[6];
1548 #ifdef HAVE_TM_ZONE
1549 if (t->tm_zone)
1550 s = (char *)t->tm_zone;
1551 #else /* not HAVE_TM_ZONE */
1552 #ifdef HAVE_TZNAME
1553 if (t->tm_isdst == 0 || t->tm_isdst == 1)
1554 s = tzname[t->tm_isdst];
1555 #endif
1556 #endif /* not HAVE_TM_ZONE */
1557 if (!s)
1559 /* No local time zone name is available; use "+-NNNN" instead. */
1560 int am = (offset < 0 ? -offset : offset) / 60;
1561 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
1562 s = buf;
1564 return Fcons (make_number (offset), Fcons (build_string (s), Qnil));
1566 else
1567 return Fmake_list (make_number (2), Qnil);
1570 /* This holds the value of `environ' produced by the previous
1571 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1572 has never been called. */
1573 static char **environbuf;
1575 DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
1576 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
1577 If TZ is nil, use implementation-defined default time zone information.\n\
1578 If TZ is t, use Universal Time.")
1579 (tz)
1580 Lisp_Object tz;
1582 char *tzstring;
1584 if (NILP (tz))
1585 tzstring = 0;
1586 else if (EQ (tz, Qt))
1587 tzstring = "UTC0";
1588 else
1590 CHECK_STRING (tz, 0);
1591 tzstring = (char *) XSTRING (tz)->data;
1594 set_time_zone_rule (tzstring);
1595 if (environbuf)
1596 free (environbuf);
1597 environbuf = environ;
1599 return Qnil;
1602 #ifdef LOCALTIME_CACHE
1604 /* These two values are known to load tz files in buggy implementations,
1605 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1606 Their values shouldn't matter in non-buggy implementations.
1607 We don't use string literals for these strings,
1608 since if a string in the environment is in readonly
1609 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1610 See Sun bugs 1113095 and 1114114, ``Timezone routines
1611 improperly modify environment''. */
1613 static char set_time_zone_rule_tz1[] = "TZ=GMT+0";
1614 static char set_time_zone_rule_tz2[] = "TZ=GMT+1";
1616 #endif
1618 /* Set the local time zone rule to TZSTRING.
1619 This allocates memory into `environ', which it is the caller's
1620 responsibility to free. */
1621 void
1622 set_time_zone_rule (tzstring)
1623 char *tzstring;
1625 int envptrs;
1626 char **from, **to, **newenv;
1628 /* Make the ENVIRON vector longer with room for TZSTRING. */
1629 for (from = environ; *from; from++)
1630 continue;
1631 envptrs = from - environ + 2;
1632 newenv = to = (char **) xmalloc (envptrs * sizeof (char *)
1633 + (tzstring ? strlen (tzstring) + 4 : 0));
1635 /* Add TZSTRING to the end of environ, as a value for TZ. */
1636 if (tzstring)
1638 char *t = (char *) (to + envptrs);
1639 strcpy (t, "TZ=");
1640 strcat (t, tzstring);
1641 *to++ = t;
1644 /* Copy the old environ vector elements into NEWENV,
1645 but don't copy the TZ variable.
1646 So we have only one definition of TZ, which came from TZSTRING. */
1647 for (from = environ; *from; from++)
1648 if (strncmp (*from, "TZ=", 3) != 0)
1649 *to++ = *from;
1650 *to = 0;
1652 environ = newenv;
1654 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1655 the TZ variable is stored. If we do not have a TZSTRING,
1656 TO points to the vector slot which has the terminating null. */
1658 #ifdef LOCALTIME_CACHE
1660 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1661 "US/Pacific" that loads a tz file, then changes to a value like
1662 "XXX0" that does not load a tz file, and then changes back to
1663 its original value, the last change is (incorrectly) ignored.
1664 Also, if TZ changes twice in succession to values that do
1665 not load a tz file, tzset can dump core (see Sun bug#1225179).
1666 The following code works around these bugs. */
1668 if (tzstring)
1670 /* Temporarily set TZ to a value that loads a tz file
1671 and that differs from tzstring. */
1672 char *tz = *newenv;
1673 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
1674 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
1675 tzset ();
1676 *newenv = tz;
1678 else
1680 /* The implied tzstring is unknown, so temporarily set TZ to
1681 two different values that each load a tz file. */
1682 *to = set_time_zone_rule_tz1;
1683 to[1] = 0;
1684 tzset ();
1685 *to = set_time_zone_rule_tz2;
1686 tzset ();
1687 *to = 0;
1690 /* Now TZ has the desired value, and tzset can be invoked safely. */
1693 tzset ();
1694 #endif
1697 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1698 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1699 type of object is Lisp_String). INHERIT is passed to
1700 INSERT_FROM_STRING_FUNC as the last argument. */
1702 void
1703 general_insert_function (insert_func, insert_from_string_func,
1704 inherit, nargs, args)
1705 void (*insert_func) P_ ((unsigned char *, int));
1706 void (*insert_from_string_func) P_ ((Lisp_Object, int, int, int, int, int));
1707 int inherit, nargs;
1708 register Lisp_Object *args;
1710 register int argnum;
1711 register Lisp_Object val;
1713 for (argnum = 0; argnum < nargs; argnum++)
1715 val = args[argnum];
1716 retry:
1717 if (INTEGERP (val))
1719 unsigned char str[MAX_MULTIBYTE_LENGTH];
1720 int len;
1722 if (!NILP (current_buffer->enable_multibyte_characters))
1723 len = CHAR_STRING (XFASTINT (val), str);
1724 else
1726 str[0] = (SINGLE_BYTE_CHAR_P (XINT (val))
1727 ? XINT (val)
1728 : multibyte_char_to_unibyte (XINT (val), Qnil));
1729 len = 1;
1731 (*insert_func) (str, len);
1733 else if (STRINGP (val))
1735 (*insert_from_string_func) (val, 0, 0,
1736 XSTRING (val)->size,
1737 STRING_BYTES (XSTRING (val)),
1738 inherit);
1740 else
1742 val = wrong_type_argument (Qchar_or_string_p, val);
1743 goto retry;
1748 void
1749 insert1 (arg)
1750 Lisp_Object arg;
1752 Finsert (1, &arg);
1756 /* Callers passing one argument to Finsert need not gcpro the
1757 argument "array", since the only element of the array will
1758 not be used after calling insert or insert_from_string, so
1759 we don't care if it gets trashed. */
1761 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
1762 "Insert the arguments, either strings or characters, at point.\n\
1763 Point and before-insertion markers move forward to end up\n\
1764 after the inserted text.\n\
1765 Any other markers at the point of insertion remain before the text.\n\
1767 If the current buffer is multibyte, unibyte strings are converted\n\
1768 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1769 If the current buffer is unibyte, multibyte strings are converted\n\
1770 to unibyte for insertion.")
1771 (nargs, args)
1772 int nargs;
1773 register Lisp_Object *args;
1775 general_insert_function (insert, insert_from_string, 0, nargs, args);
1776 return Qnil;
1779 DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
1780 0, MANY, 0,
1781 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1782 Point and before-insertion markers move forward to end up\n\
1783 after the inserted text.\n\
1784 Any other markers at the point of insertion remain before the text.\n\
1786 If the current buffer is multibyte, unibyte strings are converted\n\
1787 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1788 If the current buffer is unibyte, multibyte strings are converted\n\
1789 to unibyte for insertion.")
1790 (nargs, args)
1791 int nargs;
1792 register Lisp_Object *args;
1794 general_insert_function (insert_and_inherit, insert_from_string, 1,
1795 nargs, args);
1796 return Qnil;
1799 DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
1800 "Insert strings or characters at point, relocating markers after the text.\n\
1801 Point and markers move forward to end up after the inserted text.\n\
1803 If the current buffer is multibyte, unibyte strings are converted\n\
1804 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1805 If the current buffer is unibyte, multibyte strings are converted\n\
1806 to unibyte for insertion.")
1807 (nargs, args)
1808 int nargs;
1809 register Lisp_Object *args;
1811 general_insert_function (insert_before_markers,
1812 insert_from_string_before_markers, 0,
1813 nargs, args);
1814 return Qnil;
1817 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
1818 Sinsert_and_inherit_before_markers, 0, MANY, 0,
1819 "Insert text at point, relocating markers and inheriting properties.\n\
1820 Point and markers move forward to end up after the inserted text.\n\
1822 If the current buffer is multibyte, unibyte strings are converted\n\
1823 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1824 If the current buffer is unibyte, multibyte strings are converted\n\
1825 to unibyte for insertion.")
1826 (nargs, args)
1827 int nargs;
1828 register Lisp_Object *args;
1830 general_insert_function (insert_before_markers_and_inherit,
1831 insert_from_string_before_markers, 1,
1832 nargs, args);
1833 return Qnil;
1836 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
1837 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1838 Both arguments are required.\n\
1839 Point, and before-insertion markers, are relocated as in the function `insert'.\n\
1840 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1841 from adjoining text, if those properties are sticky.")
1842 (character, count, inherit)
1843 Lisp_Object character, count, inherit;
1845 register unsigned char *string;
1846 register int strlen;
1847 register int i, n;
1848 int len;
1849 unsigned char str[MAX_MULTIBYTE_LENGTH];
1851 CHECK_NUMBER (character, 0);
1852 CHECK_NUMBER (count, 1);
1854 if (!NILP (current_buffer->enable_multibyte_characters))
1855 len = CHAR_STRING (XFASTINT (character), str);
1856 else
1857 str[0] = XFASTINT (character), len = 1;
1858 n = XINT (count) * len;
1859 if (n <= 0)
1860 return Qnil;
1861 strlen = min (n, 256 * len);
1862 string = (unsigned char *) alloca (strlen);
1863 for (i = 0; i < strlen; i++)
1864 string[i] = str[i % len];
1865 while (n >= strlen)
1867 QUIT;
1868 if (!NILP (inherit))
1869 insert_and_inherit (string, strlen);
1870 else
1871 insert (string, strlen);
1872 n -= strlen;
1874 if (n > 0)
1876 if (!NILP (inherit))
1877 insert_and_inherit (string, n);
1878 else
1879 insert (string, n);
1881 return Qnil;
1885 /* Making strings from buffer contents. */
1887 /* Return a Lisp_String containing the text of the current buffer from
1888 START to END. If text properties are in use and the current buffer
1889 has properties in the range specified, the resulting string will also
1890 have them, if PROPS is nonzero.
1892 We don't want to use plain old make_string here, because it calls
1893 make_uninit_string, which can cause the buffer arena to be
1894 compacted. make_string has no way of knowing that the data has
1895 been moved, and thus copies the wrong data into the string. This
1896 doesn't effect most of the other users of make_string, so it should
1897 be left as is. But we should use this function when conjuring
1898 buffer substrings. */
1900 Lisp_Object
1901 make_buffer_string (start, end, props)
1902 int start, end;
1903 int props;
1905 int start_byte = CHAR_TO_BYTE (start);
1906 int end_byte = CHAR_TO_BYTE (end);
1908 return make_buffer_string_both (start, start_byte, end, end_byte, props);
1911 /* Return a Lisp_String containing the text of the current buffer from
1912 START / START_BYTE to END / END_BYTE.
1914 If text properties are in use and the current buffer
1915 has properties in the range specified, the resulting string will also
1916 have them, if PROPS is nonzero.
1918 We don't want to use plain old make_string here, because it calls
1919 make_uninit_string, which can cause the buffer arena to be
1920 compacted. make_string has no way of knowing that the data has
1921 been moved, and thus copies the wrong data into the string. This
1922 doesn't effect most of the other users of make_string, so it should
1923 be left as is. But we should use this function when conjuring
1924 buffer substrings. */
1926 Lisp_Object
1927 make_buffer_string_both (start, start_byte, end, end_byte, props)
1928 int start, start_byte, end, end_byte;
1929 int props;
1931 Lisp_Object result, tem, tem1;
1933 if (start < GPT && GPT < end)
1934 move_gap (start);
1936 if (! NILP (current_buffer->enable_multibyte_characters))
1937 result = make_uninit_multibyte_string (end - start, end_byte - start_byte);
1938 else
1939 result = make_uninit_string (end - start);
1940 bcopy (BYTE_POS_ADDR (start_byte), XSTRING (result)->data,
1941 end_byte - start_byte);
1943 /* If desired, update and copy the text properties. */
1944 if (props)
1946 update_buffer_properties (start, end);
1948 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
1949 tem1 = Ftext_properties_at (make_number (start), Qnil);
1951 if (XINT (tem) != end || !NILP (tem1))
1952 copy_intervals_to_string (result, current_buffer, start,
1953 end - start);
1956 return result;
1959 /* Call Vbuffer_access_fontify_functions for the range START ... END
1960 in the current buffer, if necessary. */
1962 static void
1963 update_buffer_properties (start, end)
1964 int start, end;
1966 /* If this buffer has some access functions,
1967 call them, specifying the range of the buffer being accessed. */
1968 if (!NILP (Vbuffer_access_fontify_functions))
1970 Lisp_Object args[3];
1971 Lisp_Object tem;
1973 args[0] = Qbuffer_access_fontify_functions;
1974 XSETINT (args[1], start);
1975 XSETINT (args[2], end);
1977 /* But don't call them if we can tell that the work
1978 has already been done. */
1979 if (!NILP (Vbuffer_access_fontified_property))
1981 tem = Ftext_property_any (args[1], args[2],
1982 Vbuffer_access_fontified_property,
1983 Qnil, Qnil);
1984 if (! NILP (tem))
1985 Frun_hook_with_args (3, args);
1987 else
1988 Frun_hook_with_args (3, args);
1992 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
1993 "Return the contents of part of the current buffer as a string.\n\
1994 The two arguments START and END are character positions;\n\
1995 they can be in either order.\n\
1996 The string returned is multibyte if the buffer is multibyte.")
1997 (start, end)
1998 Lisp_Object start, end;
2000 register int b, e;
2002 validate_region (&start, &end);
2003 b = XINT (start);
2004 e = XINT (end);
2006 return make_buffer_string (b, e, 1);
2009 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
2010 Sbuffer_substring_no_properties, 2, 2, 0,
2011 "Return the characters of part of the buffer, without the text properties.\n\
2012 The two arguments START and END are character positions;\n\
2013 they can be in either order.")
2014 (start, end)
2015 Lisp_Object start, end;
2017 register int b, e;
2019 validate_region (&start, &end);
2020 b = XINT (start);
2021 e = XINT (end);
2023 return make_buffer_string (b, e, 0);
2026 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
2027 "Return the contents of the current buffer as a string.\n\
2028 If narrowing is in effect, this function returns only the visible part\n\
2029 of the buffer. If in a mini-buffer, don't include the prompt in the\n\
2030 string returned.")
2033 return make_buffer_string (BEGV, ZV, 1);
2036 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
2037 1, 3, 0,
2038 "Insert before point a substring of the contents of buffer BUFFER.\n\
2039 BUFFER may be a buffer or a buffer name.\n\
2040 Arguments START and END are character numbers specifying the substring.\n\
2041 They default to the beginning and the end of BUFFER.")
2042 (buf, start, end)
2043 Lisp_Object buf, start, end;
2045 register int b, e, temp;
2046 register struct buffer *bp, *obuf;
2047 Lisp_Object buffer;
2049 buffer = Fget_buffer (buf);
2050 if (NILP (buffer))
2051 nsberror (buf);
2052 bp = XBUFFER (buffer);
2053 if (NILP (bp->name))
2054 error ("Selecting deleted buffer");
2056 if (NILP (start))
2057 b = BUF_BEGV (bp);
2058 else
2060 CHECK_NUMBER_COERCE_MARKER (start, 0);
2061 b = XINT (start);
2063 if (NILP (end))
2064 e = BUF_ZV (bp);
2065 else
2067 CHECK_NUMBER_COERCE_MARKER (end, 1);
2068 e = XINT (end);
2071 if (b > e)
2072 temp = b, b = e, e = temp;
2074 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
2075 args_out_of_range (start, end);
2077 obuf = current_buffer;
2078 set_buffer_internal_1 (bp);
2079 update_buffer_properties (b, e);
2080 set_buffer_internal_1 (obuf);
2082 insert_from_buffer (bp, b, e - b, 0);
2083 return Qnil;
2086 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
2087 6, 6, 0,
2088 "Compare two substrings of two buffers; return result as number.\n\
2089 the value is -N if first string is less after N-1 chars,\n\
2090 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
2091 Each substring is represented as three arguments: BUFFER, START and END.\n\
2092 That makes six args in all, three for each substring.\n\n\
2093 The value of `case-fold-search' in the current buffer\n\
2094 determines whether case is significant or ignored.")
2095 (buffer1, start1, end1, buffer2, start2, end2)
2096 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
2098 register int begp1, endp1, begp2, endp2, temp;
2099 register struct buffer *bp1, *bp2;
2100 register Lisp_Object *trt
2101 = (!NILP (current_buffer->case_fold_search)
2102 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0);
2103 int chars = 0;
2104 int i1, i2, i1_byte, i2_byte;
2106 /* Find the first buffer and its substring. */
2108 if (NILP (buffer1))
2109 bp1 = current_buffer;
2110 else
2112 Lisp_Object buf1;
2113 buf1 = Fget_buffer (buffer1);
2114 if (NILP (buf1))
2115 nsberror (buffer1);
2116 bp1 = XBUFFER (buf1);
2117 if (NILP (bp1->name))
2118 error ("Selecting deleted buffer");
2121 if (NILP (start1))
2122 begp1 = BUF_BEGV (bp1);
2123 else
2125 CHECK_NUMBER_COERCE_MARKER (start1, 1);
2126 begp1 = XINT (start1);
2128 if (NILP (end1))
2129 endp1 = BUF_ZV (bp1);
2130 else
2132 CHECK_NUMBER_COERCE_MARKER (end1, 2);
2133 endp1 = XINT (end1);
2136 if (begp1 > endp1)
2137 temp = begp1, begp1 = endp1, endp1 = temp;
2139 if (!(BUF_BEGV (bp1) <= begp1
2140 && begp1 <= endp1
2141 && endp1 <= BUF_ZV (bp1)))
2142 args_out_of_range (start1, end1);
2144 /* Likewise for second substring. */
2146 if (NILP (buffer2))
2147 bp2 = current_buffer;
2148 else
2150 Lisp_Object buf2;
2151 buf2 = Fget_buffer (buffer2);
2152 if (NILP (buf2))
2153 nsberror (buffer2);
2154 bp2 = XBUFFER (buf2);
2155 if (NILP (bp2->name))
2156 error ("Selecting deleted buffer");
2159 if (NILP (start2))
2160 begp2 = BUF_BEGV (bp2);
2161 else
2163 CHECK_NUMBER_COERCE_MARKER (start2, 4);
2164 begp2 = XINT (start2);
2166 if (NILP (end2))
2167 endp2 = BUF_ZV (bp2);
2168 else
2170 CHECK_NUMBER_COERCE_MARKER (end2, 5);
2171 endp2 = XINT (end2);
2174 if (begp2 > endp2)
2175 temp = begp2, begp2 = endp2, endp2 = temp;
2177 if (!(BUF_BEGV (bp2) <= begp2
2178 && begp2 <= endp2
2179 && endp2 <= BUF_ZV (bp2)))
2180 args_out_of_range (start2, end2);
2182 i1 = begp1;
2183 i2 = begp2;
2184 i1_byte = buf_charpos_to_bytepos (bp1, i1);
2185 i2_byte = buf_charpos_to_bytepos (bp2, i2);
2187 while (i1 < endp1 && i2 < endp2)
2189 /* When we find a mismatch, we must compare the
2190 characters, not just the bytes. */
2191 int c1, c2;
2193 if (! NILP (bp1->enable_multibyte_characters))
2195 c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte);
2196 BUF_INC_POS (bp1, i1_byte);
2197 i1++;
2199 else
2201 c1 = BUF_FETCH_BYTE (bp1, i1);
2202 c1 = unibyte_char_to_multibyte (c1);
2203 i1++;
2206 if (! NILP (bp2->enable_multibyte_characters))
2208 c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte);
2209 BUF_INC_POS (bp2, i2_byte);
2210 i2++;
2212 else
2214 c2 = BUF_FETCH_BYTE (bp2, i2);
2215 c2 = unibyte_char_to_multibyte (c2);
2216 i2++;
2219 if (trt)
2221 c1 = XINT (trt[c1]);
2222 c2 = XINT (trt[c2]);
2224 if (c1 < c2)
2225 return make_number (- 1 - chars);
2226 if (c1 > c2)
2227 return make_number (chars + 1);
2229 chars++;
2232 /* The strings match as far as they go.
2233 If one is shorter, that one is less. */
2234 if (chars < endp1 - begp1)
2235 return make_number (chars + 1);
2236 else if (chars < endp2 - begp2)
2237 return make_number (- chars - 1);
2239 /* Same length too => they are equal. */
2240 return make_number (0);
2243 static Lisp_Object
2244 subst_char_in_region_unwind (arg)
2245 Lisp_Object arg;
2247 return current_buffer->undo_list = arg;
2250 static Lisp_Object
2251 subst_char_in_region_unwind_1 (arg)
2252 Lisp_Object arg;
2254 return current_buffer->filename = arg;
2257 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
2258 Ssubst_char_in_region, 4, 5, 0,
2259 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
2260 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
2261 and don't mark the buffer as really changed.\n\
2262 Both characters must have the same length of multi-byte form.")
2263 (start, end, fromchar, tochar, noundo)
2264 Lisp_Object start, end, fromchar, tochar, noundo;
2266 register int pos, pos_byte, stop, i, len, end_byte;
2267 int changed = 0;
2268 unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH];
2269 unsigned char *p;
2270 int count = specpdl_ptr - specpdl;
2271 #define COMBINING_NO 0
2272 #define COMBINING_BEFORE 1
2273 #define COMBINING_AFTER 2
2274 #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
2275 int maybe_byte_combining = COMBINING_NO;
2276 int last_changed;
2277 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2279 validate_region (&start, &end);
2280 CHECK_NUMBER (fromchar, 2);
2281 CHECK_NUMBER (tochar, 3);
2283 if (multibyte_p)
2285 len = CHAR_STRING (XFASTINT (fromchar), fromstr);
2286 if (CHAR_STRING (XFASTINT (tochar), tostr) != len)
2287 error ("Characters in subst-char-in-region have different byte-lengths");
2288 if (!ASCII_BYTE_P (*tostr))
2290 /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a
2291 complete multibyte character, it may be combined with the
2292 after bytes. If it is in the range 0xA0..0xFF, it may be
2293 combined with the before and after bytes. */
2294 if (!CHAR_HEAD_P (*tostr))
2295 maybe_byte_combining = COMBINING_BOTH;
2296 else if (BYTES_BY_CHAR_HEAD (*tostr) > len)
2297 maybe_byte_combining = COMBINING_AFTER;
2300 else
2302 len = 1;
2303 fromstr[0] = XFASTINT (fromchar);
2304 tostr[0] = XFASTINT (tochar);
2307 pos = XINT (start);
2308 pos_byte = CHAR_TO_BYTE (pos);
2309 stop = CHAR_TO_BYTE (XINT (end));
2310 end_byte = stop;
2312 /* If we don't want undo, turn off putting stuff on the list.
2313 That's faster than getting rid of things,
2314 and it prevents even the entry for a first change.
2315 Also inhibit locking the file. */
2316 if (!NILP (noundo))
2318 record_unwind_protect (subst_char_in_region_unwind,
2319 current_buffer->undo_list);
2320 current_buffer->undo_list = Qt;
2321 /* Don't do file-locking. */
2322 record_unwind_protect (subst_char_in_region_unwind_1,
2323 current_buffer->filename);
2324 current_buffer->filename = Qnil;
2327 if (pos_byte < GPT_BYTE)
2328 stop = min (stop, GPT_BYTE);
2329 while (1)
2331 int pos_byte_next = pos_byte;
2333 if (pos_byte >= stop)
2335 if (pos_byte >= end_byte) break;
2336 stop = end_byte;
2338 p = BYTE_POS_ADDR (pos_byte);
2339 if (multibyte_p)
2340 INC_POS (pos_byte_next);
2341 else
2342 ++pos_byte_next;
2343 if (pos_byte_next - pos_byte == len
2344 && p[0] == fromstr[0]
2345 && (len == 1
2346 || (p[1] == fromstr[1]
2347 && (len == 2 || (p[2] == fromstr[2]
2348 && (len == 3 || p[3] == fromstr[3]))))))
2350 if (! changed)
2352 changed = pos;
2353 modify_region (current_buffer, changed, XINT (end));
2355 if (! NILP (noundo))
2357 if (MODIFF - 1 == SAVE_MODIFF)
2358 SAVE_MODIFF++;
2359 if (MODIFF - 1 == current_buffer->auto_save_modified)
2360 current_buffer->auto_save_modified++;
2364 /* Take care of the case where the new character
2365 combines with neighboring bytes. */
2366 if (maybe_byte_combining
2367 && (maybe_byte_combining == COMBINING_AFTER
2368 ? (pos_byte_next < Z_BYTE
2369 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2370 : ((pos_byte_next < Z_BYTE
2371 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2372 || (pos_byte > BEG_BYTE
2373 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1))))))
2375 Lisp_Object tem, string;
2377 struct gcpro gcpro1;
2379 tem = current_buffer->undo_list;
2380 GCPRO1 (tem);
2382 /* Make a multibyte string containing this single character. */
2383 string = make_multibyte_string (tostr, 1, len);
2384 /* replace_range is less efficient, because it moves the gap,
2385 but it handles combining correctly. */
2386 replace_range (pos, pos + 1, string,
2387 0, 0, 1);
2388 pos_byte_next = CHAR_TO_BYTE (pos);
2389 if (pos_byte_next > pos_byte)
2390 /* Before combining happened. We should not increment
2391 POS. So, to cancel the later increment of POS,
2392 decrease it now. */
2393 pos--;
2394 else
2395 INC_POS (pos_byte_next);
2397 if (! NILP (noundo))
2398 current_buffer->undo_list = tem;
2400 UNGCPRO;
2402 else
2404 if (NILP (noundo))
2405 record_change (pos, 1);
2406 for (i = 0; i < len; i++) *p++ = tostr[i];
2408 last_changed = pos + 1;
2410 pos_byte = pos_byte_next;
2411 pos++;
2414 if (changed)
2416 signal_after_change (changed,
2417 last_changed - changed, last_changed - changed);
2418 update_compositions (changed, last_changed, CHECK_ALL);
2421 unbind_to (count, Qnil);
2422 return Qnil;
2425 DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
2426 "From START to END, translate characters according to TABLE.\n\
2427 TABLE is a string; the Nth character in it is the mapping\n\
2428 for the character with code N.\n\
2429 This function does not alter multibyte characters.\n\
2430 It returns the number of characters changed.")
2431 (start, end, table)
2432 Lisp_Object start;
2433 Lisp_Object end;
2434 register Lisp_Object table;
2436 register int pos_byte, stop; /* Limits of the region. */
2437 register unsigned char *tt; /* Trans table. */
2438 register int nc; /* New character. */
2439 int cnt; /* Number of changes made. */
2440 int size; /* Size of translate table. */
2441 int pos;
2442 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
2444 validate_region (&start, &end);
2445 CHECK_STRING (table, 2);
2447 size = STRING_BYTES (XSTRING (table));
2448 tt = XSTRING (table)->data;
2450 pos_byte = CHAR_TO_BYTE (XINT (start));
2451 stop = CHAR_TO_BYTE (XINT (end));
2452 modify_region (current_buffer, XINT (start), XINT (end));
2453 pos = XINT (start);
2455 cnt = 0;
2456 for (; pos_byte < stop; )
2458 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
2459 int len;
2460 int oc;
2461 int pos_byte_next;
2463 if (multibyte)
2464 oc = STRING_CHAR_AND_LENGTH (p, stop - pos_byte, len);
2465 else
2466 oc = *p, len = 1;
2467 pos_byte_next = pos_byte + len;
2468 if (oc < size && len == 1)
2470 nc = tt[oc];
2471 if (nc != oc)
2473 /* Take care of the case where the new character
2474 combines with neighboring bytes. */
2475 if (!ASCII_BYTE_P (nc)
2476 && (CHAR_HEAD_P (nc)
2477 ? ! CHAR_HEAD_P (FETCH_BYTE (pos_byte + 1))
2478 : (pos_byte > BEG_BYTE
2479 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1)))))
2481 Lisp_Object string;
2483 string = make_multibyte_string (tt + oc, 1, 1);
2484 /* This is less efficient, because it moves the gap,
2485 but it handles combining correctly. */
2486 replace_range (pos, pos + 1, string,
2487 1, 0, 1);
2488 pos_byte_next = CHAR_TO_BYTE (pos);
2489 if (pos_byte_next > pos_byte)
2490 /* Before combining happened. We should not
2491 increment POS. So, to cancel the later
2492 increment of POS, we decrease it now. */
2493 pos--;
2494 else
2495 INC_POS (pos_byte_next);
2497 else
2499 record_change (pos, 1);
2500 *p = nc;
2501 signal_after_change (pos, 1, 1);
2502 update_compositions (pos, pos + 1, CHECK_BORDER);
2504 ++cnt;
2507 pos_byte = pos_byte_next;
2508 pos++;
2511 return make_number (cnt);
2514 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
2515 "Delete the text between point and mark.\n\
2516 When called from a program, expects two arguments,\n\
2517 positions (integers or markers) specifying the stretch to be deleted.")
2518 (start, end)
2519 Lisp_Object start, end;
2521 validate_region (&start, &end);
2522 del_range (XINT (start), XINT (end));
2523 return Qnil;
2526 DEFUN ("delete-and-extract-region", Fdelete_and_extract_region,
2527 Sdelete_and_extract_region, 2, 2, 0,
2528 "Delete the text between START and END and return it.")
2529 (start, end)
2530 Lisp_Object start, end;
2532 validate_region (&start, &end);
2533 return del_range_1 (XINT (start), XINT (end), 1, 1);
2536 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
2537 "Remove restrictions (narrowing) from current buffer.\n\
2538 This allows the buffer's full text to be seen and edited.")
2541 if (BEG != BEGV || Z != ZV)
2542 current_buffer->clip_changed = 1;
2543 BEGV = BEG;
2544 BEGV_BYTE = BEG_BYTE;
2545 SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE);
2546 /* Changing the buffer bounds invalidates any recorded current column. */
2547 invalidate_current_column ();
2548 return Qnil;
2551 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
2552 "Restrict editing in this buffer to the current region.\n\
2553 The rest of the text becomes temporarily invisible and untouchable\n\
2554 but is not deleted; if you save the buffer in a file, the invisible\n\
2555 text is included in the file. \\[widen] makes all visible again.\n\
2556 See also `save-restriction'.\n\
2558 When calling from a program, pass two arguments; positions (integers\n\
2559 or markers) bounding the text that should remain visible.")
2560 (start, end)
2561 register Lisp_Object start, end;
2563 CHECK_NUMBER_COERCE_MARKER (start, 0);
2564 CHECK_NUMBER_COERCE_MARKER (end, 1);
2566 if (XINT (start) > XINT (end))
2568 Lisp_Object tem;
2569 tem = start; start = end; end = tem;
2572 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
2573 args_out_of_range (start, end);
2575 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
2576 current_buffer->clip_changed = 1;
2578 SET_BUF_BEGV (current_buffer, XFASTINT (start));
2579 SET_BUF_ZV (current_buffer, XFASTINT (end));
2580 if (PT < XFASTINT (start))
2581 SET_PT (XFASTINT (start));
2582 if (PT > XFASTINT (end))
2583 SET_PT (XFASTINT (end));
2584 /* Changing the buffer bounds invalidates any recorded current column. */
2585 invalidate_current_column ();
2586 return Qnil;
2589 Lisp_Object
2590 save_restriction_save ()
2592 register Lisp_Object bottom, top;
2593 /* Note: I tried using markers here, but it does not win
2594 because insertion at the end of the saved region
2595 does not advance mh and is considered "outside" the saved region. */
2596 XSETFASTINT (bottom, BEGV - BEG);
2597 XSETFASTINT (top, Z - ZV);
2599 return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
2602 Lisp_Object
2603 save_restriction_restore (data)
2604 Lisp_Object data;
2606 register struct buffer *buf;
2607 register int newhead, newtail;
2608 register Lisp_Object tem;
2609 int obegv, ozv;
2611 buf = XBUFFER (XCAR (data));
2613 data = XCDR (data);
2615 tem = XCAR (data);
2616 newhead = XINT (tem);
2617 tem = XCDR (data);
2618 newtail = XINT (tem);
2619 if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
2621 newhead = 0;
2622 newtail = 0;
2625 obegv = BUF_BEGV (buf);
2626 ozv = BUF_ZV (buf);
2628 SET_BUF_BEGV (buf, BUF_BEG (buf) + newhead);
2629 SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
2631 if (obegv != BUF_BEGV (buf) || ozv != BUF_ZV (buf))
2632 current_buffer->clip_changed = 1;
2634 /* If point is outside the new visible range, move it inside. */
2635 SET_BUF_PT_BOTH (buf,
2636 clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)),
2637 clip_to_bounds (BUF_BEGV_BYTE (buf), BUF_PT_BYTE (buf),
2638 BUF_ZV_BYTE (buf)));
2640 return Qnil;
2643 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
2644 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2645 The buffer's restrictions make parts of the beginning and end invisible.\n\
2646 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2647 This special form, `save-restriction', saves the current buffer's restrictions\n\
2648 when it is entered, and restores them when it is exited.\n\
2649 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2650 The old restrictions settings are restored\n\
2651 even in case of abnormal exit (throw or error).\n\
2653 The value returned is the value of the last form in BODY.\n\
2655 `save-restriction' can get confused if, within the BODY, you widen\n\
2656 and then make changes outside the area within the saved restrictions.\n\
2657 See Info node `(elisp)Narrowing' for details and an appropriate technique.\n\
2659 Note: if you are using both `save-excursion' and `save-restriction',\n\
2660 use `save-excursion' outermost:\n\
2661 (save-excursion (save-restriction ...))")
2662 (body)
2663 Lisp_Object body;
2665 register Lisp_Object val;
2666 int count = specpdl_ptr - specpdl;
2668 record_unwind_protect (save_restriction_restore, save_restriction_save ());
2669 val = Fprogn (body);
2670 return unbind_to (count, val);
2673 #ifndef HAVE_MENUS
2675 /* Buffer for the most recent text displayed by Fmessage. */
2676 static char *message_text;
2678 /* Allocated length of that buffer. */
2679 static int message_length;
2681 #endif /* not HAVE_MENUS */
2683 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
2684 "Print a one-line message at the bottom of the screen.\n\
2685 The first argument is a format control string, and the rest are data\n\
2686 to be formatted under control of the string. See `format' for details.\n\
2688 If the first argument is nil, clear any existing message; let the\n\
2689 minibuffer contents show.")
2690 (nargs, args)
2691 int nargs;
2692 Lisp_Object *args;
2694 if (NILP (args[0]))
2696 message (0);
2697 return Qnil;
2699 else
2701 register Lisp_Object val;
2702 val = Fformat (nargs, args);
2703 message3 (val, STRING_BYTES (XSTRING (val)), STRING_MULTIBYTE (val));
2704 return val;
2708 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
2709 "Display a message, in a dialog box if possible.\n\
2710 If a dialog box is not available, use the echo area.\n\
2711 The first argument is a format control string, and the rest are data\n\
2712 to be formatted under control of the string. See `format' for details.\n\
2714 If the first argument is nil, clear any existing message; let the\n\
2715 minibuffer contents show.")
2716 (nargs, args)
2717 int nargs;
2718 Lisp_Object *args;
2720 if (NILP (args[0]))
2722 message (0);
2723 return Qnil;
2725 else
2727 register Lisp_Object val;
2728 val = Fformat (nargs, args);
2729 #ifdef HAVE_MENUS
2731 Lisp_Object pane, menu, obj;
2732 struct gcpro gcpro1;
2733 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
2734 GCPRO1 (pane);
2735 menu = Fcons (val, pane);
2736 obj = Fx_popup_dialog (Qt, menu);
2737 UNGCPRO;
2738 return val;
2740 #else /* not HAVE_MENUS */
2741 /* Copy the data so that it won't move when we GC. */
2742 if (! message_text)
2744 message_text = (char *)xmalloc (80);
2745 message_length = 80;
2747 if (STRING_BYTES (XSTRING (val)) > message_length)
2749 message_length = STRING_BYTES (XSTRING (val));
2750 message_text = (char *)xrealloc (message_text, message_length);
2752 bcopy (XSTRING (val)->data, message_text, STRING_BYTES (XSTRING (val)));
2753 message2 (message_text, STRING_BYTES (XSTRING (val)),
2754 STRING_MULTIBYTE (val));
2755 return val;
2756 #endif /* not HAVE_MENUS */
2759 #ifdef HAVE_MENUS
2760 extern Lisp_Object last_nonmenu_event;
2761 #endif
2763 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
2764 "Display a message in a dialog box or in the echo area.\n\
2765 If this command was invoked with the mouse, use a dialog box.\n\
2766 Otherwise, use the echo area.\n\
2767 The first argument is a format control string, and the rest are data\n\
2768 to be formatted under control of the string. See `format' for details.\n\
2770 If the first argument is nil, clear any existing message; let the\n\
2771 minibuffer contents show.")
2772 (nargs, args)
2773 int nargs;
2774 Lisp_Object *args;
2776 #ifdef HAVE_MENUS
2777 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2778 && use_dialog_box)
2779 return Fmessage_box (nargs, args);
2780 #endif
2781 return Fmessage (nargs, args);
2784 DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0,
2785 "Return the string currently displayed in the echo area, or nil if none.")
2788 return current_message ();
2792 DEFUN ("propertize", Fpropertize, Spropertize, 3, MANY, 0,
2793 "Return a copy of STRING with text properties added.\n\
2794 First argument is the string to copy.\n\
2795 Remaining arguments form a sequence of PROPERTY VALUE pairs for text\n\
2796 properties to add to the result ")
2797 (nargs, args)
2798 int nargs;
2799 Lisp_Object *args;
2801 Lisp_Object properties, string;
2802 struct gcpro gcpro1, gcpro2;
2803 int i;
2805 /* Number of args must be odd. */
2806 if ((nargs & 1) == 0 || nargs < 3)
2807 error ("Wrong number of arguments");
2809 properties = string = Qnil;
2810 GCPRO2 (properties, string);
2812 /* First argument must be a string. */
2813 CHECK_STRING (args[0], 0);
2814 string = Fcopy_sequence (args[0]);
2816 for (i = 1; i < nargs; i += 2)
2818 CHECK_SYMBOL (args[i], i);
2819 properties = Fcons (args[i], Fcons (args[i + 1], properties));
2822 Fadd_text_properties (make_number (0),
2823 make_number (XSTRING (string)->size),
2824 properties, string);
2825 RETURN_UNGCPRO (string);
2829 /* Number of bytes that STRING will occupy when put into the result.
2830 MULTIBYTE is nonzero if the result should be multibyte. */
2832 #define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \
2833 (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \
2834 ? count_size_as_multibyte (XSTRING (STRING)->data, \
2835 STRING_BYTES (XSTRING (STRING))) \
2836 : STRING_BYTES (XSTRING (STRING)))
2838 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
2839 "Format a string out of a control-string and arguments.\n\
2840 The first argument is a control string.\n\
2841 The other arguments are substituted into it to make the result, a string.\n\
2842 It may contain %-sequences meaning to substitute the next argument.\n\
2843 %s means print a string argument. Actually, prints any object, with `princ'.\n\
2844 %d means print as number in decimal (%o octal, %x hex).\n\
2845 %e means print a number in exponential notation.\n\
2846 %f means print a number in decimal-point notation.\n\
2847 %g means print a number in exponential notation\n\
2848 or decimal-point notation, whichever uses fewer characters.\n\
2849 %c means print a number as a single character.\n\
2850 %S means print any object as an s-expression (using `prin1').\n\
2851 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2852 Use %% to put a single % into the output.")
2853 (nargs, args)
2854 int nargs;
2855 register Lisp_Object *args;
2857 register int n; /* The number of the next arg to substitute */
2858 register int total; /* An estimate of the final length */
2859 char *buf, *p;
2860 register unsigned char *format, *end;
2861 int nchars;
2862 /* Nonzero if the output should be a multibyte string,
2863 which is true if any of the inputs is one. */
2864 int multibyte = 0;
2865 /* When we make a multibyte string, we must pay attention to the
2866 byte combining problem, i.e., a byte may be combined with a
2867 multibyte charcter of the previous string. This flag tells if we
2868 must consider such a situation or not. */
2869 int maybe_combine_byte;
2870 unsigned char *this_format;
2871 int longest_format;
2872 Lisp_Object val;
2873 struct info
2875 int start, end;
2876 } *info = 0;
2878 extern char *index ();
2880 /* It should not be necessary to GCPRO ARGS, because
2881 the caller in the interpreter should take care of that. */
2883 /* Try to determine whether the result should be multibyte.
2884 This is not always right; sometimes the result needs to be multibyte
2885 because of an object that we will pass through prin1,
2886 and in that case, we won't know it here. */
2887 for (n = 0; n < nargs; n++)
2888 if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
2889 multibyte = 1;
2891 CHECK_STRING (args[0], 0);
2893 /* If we start out planning a unibyte result,
2894 and later find it has to be multibyte, we jump back to retry. */
2895 retry:
2897 format = XSTRING (args[0])->data;
2898 end = format + STRING_BYTES (XSTRING (args[0]));
2899 longest_format = 0;
2901 /* Make room in result for all the non-%-codes in the control string. */
2902 total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]);
2904 /* Add to TOTAL enough space to hold the converted arguments. */
2906 n = 0;
2907 while (format != end)
2908 if (*format++ == '%')
2910 int minlen, thissize = 0;
2911 unsigned char *this_format_start = format - 1;
2913 /* Process a numeric arg and skip it. */
2914 minlen = atoi (format);
2915 if (minlen < 0)
2916 minlen = - minlen;
2918 while ((*format >= '0' && *format <= '9')
2919 || *format == '-' || *format == ' ' || *format == '.')
2920 format++;
2922 if (format - this_format_start + 1 > longest_format)
2923 longest_format = format - this_format_start + 1;
2925 if (format == end)
2926 error ("Format string ends in middle of format specifier");
2927 if (*format == '%')
2928 format++;
2929 else if (++n >= nargs)
2930 error ("Not enough arguments for format string");
2931 else if (*format == 'S')
2933 /* For `S', prin1 the argument and then treat like a string. */
2934 register Lisp_Object tem;
2935 tem = Fprin1_to_string (args[n], Qnil);
2936 if (STRING_MULTIBYTE (tem) && ! multibyte)
2938 multibyte = 1;
2939 goto retry;
2941 args[n] = tem;
2942 goto string;
2944 else if (SYMBOLP (args[n]))
2946 /* Use a temp var to avoid problems when ENABLE_CHECKING
2947 is turned on. */
2948 struct Lisp_String *t = XSYMBOL (args[n])->name;
2949 XSETSTRING (args[n], t);
2950 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
2952 multibyte = 1;
2953 goto retry;
2955 goto string;
2957 else if (STRINGP (args[n]))
2959 string:
2960 if (*format != 's' && *format != 'S')
2961 error ("Format specifier doesn't match argument type");
2962 thissize = CONVERTED_BYTE_SIZE (multibyte, args[n]);
2964 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
2965 else if (INTEGERP (args[n]) && *format != 's')
2967 /* The following loop assumes the Lisp type indicates
2968 the proper way to pass the argument.
2969 So make sure we have a flonum if the argument should
2970 be a double. */
2971 if (*format == 'e' || *format == 'f' || *format == 'g')
2972 args[n] = Ffloat (args[n]);
2973 else
2974 if (*format != 'd' && *format != 'o' && *format != 'x'
2975 && *format != 'i' && *format != 'X' && *format != 'c')
2976 error ("Invalid format operation %%%c", *format);
2978 thissize = 30;
2979 if (*format == 'c'
2980 && (! SINGLE_BYTE_CHAR_P (XINT (args[n]))
2981 || XINT (args[n]) == 0))
2983 if (! multibyte)
2985 multibyte = 1;
2986 goto retry;
2988 args[n] = Fchar_to_string (args[n]);
2989 thissize = STRING_BYTES (XSTRING (args[n]));
2992 else if (FLOATP (args[n]) && *format != 's')
2994 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
2995 args[n] = Ftruncate (args[n], Qnil);
2996 thissize = 200;
2998 else
3000 /* Anything but a string, convert to a string using princ. */
3001 register Lisp_Object tem;
3002 tem = Fprin1_to_string (args[n], Qt);
3003 if (STRING_MULTIBYTE (tem) & ! multibyte)
3005 multibyte = 1;
3006 goto retry;
3008 args[n] = tem;
3009 goto string;
3012 if (thissize < minlen)
3013 thissize = minlen;
3015 total += thissize + 4;
3018 /* Now we can no longer jump to retry.
3019 TOTAL and LONGEST_FORMAT are known for certain. */
3021 this_format = (unsigned char *) alloca (longest_format + 1);
3023 /* Allocate the space for the result.
3024 Note that TOTAL is an overestimate. */
3025 if (total < 1000)
3026 buf = (char *) alloca (total + 1);
3027 else
3028 buf = (char *) xmalloc (total + 1);
3030 p = buf;
3031 nchars = 0;
3032 n = 0;
3034 /* Scan the format and store result in BUF. */
3035 format = XSTRING (args[0])->data;
3036 maybe_combine_byte = 0;
3037 while (format != end)
3039 if (*format == '%')
3041 int minlen;
3042 int negative = 0;
3043 unsigned char *this_format_start = format;
3045 format++;
3047 /* Process a numeric arg and skip it. */
3048 minlen = atoi (format);
3049 if (minlen < 0)
3050 minlen = - minlen, negative = 1;
3052 while ((*format >= '0' && *format <= '9')
3053 || *format == '-' || *format == ' ' || *format == '.')
3054 format++;
3056 if (*format++ == '%')
3058 *p++ = '%';
3059 nchars++;
3060 continue;
3063 ++n;
3065 if (STRINGP (args[n]))
3067 int padding, nbytes;
3068 int width = strwidth (XSTRING (args[n])->data,
3069 STRING_BYTES (XSTRING (args[n])));
3070 int start = nchars;
3072 /* If spec requires it, pad on right with spaces. */
3073 padding = minlen - width;
3074 if (! negative)
3075 while (padding-- > 0)
3077 *p++ = ' ';
3078 nchars++;
3081 if (p > buf
3082 && multibyte
3083 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3084 && STRING_MULTIBYTE (args[n])
3085 && !CHAR_HEAD_P (XSTRING (args[n])->data[0]))
3086 maybe_combine_byte = 1;
3087 nbytes = copy_text (XSTRING (args[n])->data, p,
3088 STRING_BYTES (XSTRING (args[n])),
3089 STRING_MULTIBYTE (args[n]), multibyte);
3090 p += nbytes;
3091 nchars += XSTRING (args[n])->size;
3093 if (negative)
3094 while (padding-- > 0)
3096 *p++ = ' ';
3097 nchars++;
3100 /* If this argument has text properties, record where
3101 in the result string it appears. */
3102 if (XSTRING (args[n])->intervals)
3104 if (!info)
3106 int nbytes = nargs * sizeof *info;
3107 info = (struct info *) alloca (nbytes);
3108 bzero (info, nbytes);
3111 info[n].start = start;
3112 info[n].end = nchars;
3115 else if (INTEGERP (args[n]) || FLOATP (args[n]))
3117 int this_nchars;
3119 bcopy (this_format_start, this_format,
3120 format - this_format_start);
3121 this_format[format - this_format_start] = 0;
3123 if (INTEGERP (args[n]))
3124 sprintf (p, this_format, XINT (args[n]));
3125 else
3126 sprintf (p, this_format, XFLOAT_DATA (args[n]));
3128 if (p > buf
3129 && multibyte
3130 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3131 && !CHAR_HEAD_P (*((unsigned char *) p)))
3132 maybe_combine_byte = 1;
3133 this_nchars = strlen (p);
3134 if (multibyte)
3135 p += str_to_multibyte (p, buf + total - p, this_nchars);
3136 else
3137 p += this_nchars;
3138 nchars += this_nchars;
3141 else if (STRING_MULTIBYTE (args[0]))
3143 /* Copy a whole multibyte character. */
3144 if (p > buf
3145 && multibyte
3146 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
3147 && !CHAR_HEAD_P (*format))
3148 maybe_combine_byte = 1;
3149 *p++ = *format++;
3150 while (! CHAR_HEAD_P (*format)) *p++ = *format++;
3151 nchars++;
3153 else if (multibyte)
3155 /* Convert a single-byte character to multibyte. */
3156 int len = copy_text (format, p, 1, 0, 1);
3158 p += len;
3159 format++;
3160 nchars++;
3162 else
3163 *p++ = *format++, nchars++;
3166 if (maybe_combine_byte)
3167 nchars = multibyte_chars_in_text (buf, p - buf);
3168 val = make_specified_string (buf, nchars, p - buf, multibyte);
3170 /* If we allocated BUF with malloc, free it too. */
3171 if (total >= 1000)
3172 xfree (buf);
3174 /* If the format string has text properties, or any of the string
3175 arguments has text properties, set up text properties of the
3176 result string. */
3178 if (XSTRING (args[0])->intervals || info)
3180 Lisp_Object len, new_len, props;
3181 struct gcpro gcpro1;
3183 /* Add text properties from the format string. */
3184 len = make_number (XSTRING (args[0])->size);
3185 props = text_property_list (args[0], make_number (0), len, Qnil);
3186 GCPRO1 (props);
3188 if (CONSP (props))
3190 new_len = make_number (XSTRING (val)->size);
3191 extend_property_ranges (props, len, new_len);
3192 add_text_properties_from_list (val, props, make_number (0));
3195 /* Add text properties from arguments. */
3196 if (info)
3197 for (n = 1; n < nargs; ++n)
3198 if (info[n].end)
3200 len = make_number (XSTRING (args[n])->size);
3201 new_len = make_number (info[n].end - info[n].start);
3202 props = text_property_list (args[n], make_number (0), len, Qnil);
3203 extend_property_ranges (props, len, new_len);
3204 add_text_properties_from_list (val, props,
3205 make_number (info[n].start));
3208 UNGCPRO;
3211 return val;
3215 /* VARARGS 1 */
3216 Lisp_Object
3217 #ifdef NO_ARG_ARRAY
3218 format1 (string1, arg0, arg1, arg2, arg3, arg4)
3219 EMACS_INT arg0, arg1, arg2, arg3, arg4;
3220 #else
3221 format1 (string1)
3222 #endif
3223 char *string1;
3225 char buf[100];
3226 #ifdef NO_ARG_ARRAY
3227 EMACS_INT args[5];
3228 args[0] = arg0;
3229 args[1] = arg1;
3230 args[2] = arg2;
3231 args[3] = arg3;
3232 args[4] = arg4;
3233 doprnt (buf, sizeof buf, string1, (char *)0, 5, (char **) args);
3234 #else
3235 doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1);
3236 #endif
3237 return build_string (buf);
3240 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
3241 "Return t if two characters match, optionally ignoring case.\n\
3242 Both arguments must be characters (i.e. integers).\n\
3243 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
3244 (c1, c2)
3245 register Lisp_Object c1, c2;
3247 int i1, i2;
3248 CHECK_NUMBER (c1, 0);
3249 CHECK_NUMBER (c2, 1);
3251 if (XINT (c1) == XINT (c2))
3252 return Qt;
3253 if (NILP (current_buffer->case_fold_search))
3254 return Qnil;
3256 /* Do these in separate statements,
3257 then compare the variables.
3258 because of the way DOWNCASE uses temp variables. */
3259 i1 = DOWNCASE (XFASTINT (c1));
3260 i2 = DOWNCASE (XFASTINT (c2));
3261 return (i1 == i2 ? Qt : Qnil);
3264 /* Transpose the markers in two regions of the current buffer, and
3265 adjust the ones between them if necessary (i.e.: if the regions
3266 differ in size).
3268 START1, END1 are the character positions of the first region.
3269 START1_BYTE, END1_BYTE are the byte positions.
3270 START2, END2 are the character positions of the second region.
3271 START2_BYTE, END2_BYTE are the byte positions.
3273 Traverses the entire marker list of the buffer to do so, adding an
3274 appropriate amount to some, subtracting from some, and leaving the
3275 rest untouched. Most of this is copied from adjust_markers in insdel.c.
3277 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
3279 void
3280 transpose_markers (start1, end1, start2, end2,
3281 start1_byte, end1_byte, start2_byte, end2_byte)
3282 register int start1, end1, start2, end2;
3283 register int start1_byte, end1_byte, start2_byte, end2_byte;
3285 register int amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
3286 register Lisp_Object marker;
3288 /* Update point as if it were a marker. */
3289 if (PT < start1)
3291 else if (PT < end1)
3292 TEMP_SET_PT_BOTH (PT + (end2 - end1),
3293 PT_BYTE + (end2_byte - end1_byte));
3294 else if (PT < start2)
3295 TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1),
3296 (PT_BYTE + (end2_byte - start2_byte)
3297 - (end1_byte - start1_byte)));
3298 else if (PT < end2)
3299 TEMP_SET_PT_BOTH (PT - (start2 - start1),
3300 PT_BYTE - (start2_byte - start1_byte));
3302 /* We used to adjust the endpoints here to account for the gap, but that
3303 isn't good enough. Even if we assume the caller has tried to move the
3304 gap out of our way, it might still be at start1 exactly, for example;
3305 and that places it `inside' the interval, for our purposes. The amount
3306 of adjustment is nontrivial if there's a `denormalized' marker whose
3307 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
3308 the dirty work to Fmarker_position, below. */
3310 /* The difference between the region's lengths */
3311 diff = (end2 - start2) - (end1 - start1);
3312 diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte);
3314 /* For shifting each marker in a region by the length of the other
3315 region plus the distance between the regions. */
3316 amt1 = (end2 - start2) + (start2 - end1);
3317 amt2 = (end1 - start1) + (start2 - end1);
3318 amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte);
3319 amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte);
3321 for (marker = BUF_MARKERS (current_buffer); !NILP (marker);
3322 marker = XMARKER (marker)->chain)
3324 mpos = marker_byte_position (marker);
3325 if (mpos >= start1_byte && mpos < end2_byte)
3327 if (mpos < end1_byte)
3328 mpos += amt1_byte;
3329 else if (mpos < start2_byte)
3330 mpos += diff_byte;
3331 else
3332 mpos -= amt2_byte;
3333 XMARKER (marker)->bytepos = mpos;
3335 mpos = XMARKER (marker)->charpos;
3336 if (mpos >= start1 && mpos < end2)
3338 if (mpos < end1)
3339 mpos += amt1;
3340 else if (mpos < start2)
3341 mpos += diff;
3342 else
3343 mpos -= amt2;
3345 XMARKER (marker)->charpos = mpos;
3349 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
3350 "Transpose region START1 to END1 with START2 to END2.\n\
3351 The regions may not be overlapping, because the size of the buffer is\n\
3352 never changed in a transposition.\n\
3354 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update\n\
3355 any markers that happen to be located in the regions.\n\
3357 Transposing beyond buffer boundaries is an error.")
3358 (startr1, endr1, startr2, endr2, leave_markers)
3359 Lisp_Object startr1, endr1, startr2, endr2, leave_markers;
3361 register int start1, end1, start2, end2;
3362 int start1_byte, start2_byte, len1_byte, len2_byte;
3363 int gap, len1, len_mid, len2;
3364 unsigned char *start1_addr, *start2_addr, *temp;
3365 struct gcpro gcpro1, gcpro2;
3367 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2;
3368 cur_intv = BUF_INTERVALS (current_buffer);
3370 validate_region (&startr1, &endr1);
3371 validate_region (&startr2, &endr2);
3373 start1 = XFASTINT (startr1);
3374 end1 = XFASTINT (endr1);
3375 start2 = XFASTINT (startr2);
3376 end2 = XFASTINT (endr2);
3377 gap = GPT;
3379 /* Swap the regions if they're reversed. */
3380 if (start2 < end1)
3382 register int glumph = start1;
3383 start1 = start2;
3384 start2 = glumph;
3385 glumph = end1;
3386 end1 = end2;
3387 end2 = glumph;
3390 len1 = end1 - start1;
3391 len2 = end2 - start2;
3393 if (start2 < end1)
3394 error ("Transposed regions overlap");
3395 else if (start1 == end1 || start2 == end2)
3396 error ("Transposed region has length 0");
3398 /* The possibilities are:
3399 1. Adjacent (contiguous) regions, or separate but equal regions
3400 (no, really equal, in this case!), or
3401 2. Separate regions of unequal size.
3403 The worst case is usually No. 2. It means that (aside from
3404 potential need for getting the gap out of the way), there also
3405 needs to be a shifting of the text between the two regions. So
3406 if they are spread far apart, we are that much slower... sigh. */
3408 /* It must be pointed out that the really studly thing to do would
3409 be not to move the gap at all, but to leave it in place and work
3410 around it if necessary. This would be extremely efficient,
3411 especially considering that people are likely to do
3412 transpositions near where they are working interactively, which
3413 is exactly where the gap would be found. However, such code
3414 would be much harder to write and to read. So, if you are
3415 reading this comment and are feeling squirrely, by all means have
3416 a go! I just didn't feel like doing it, so I will simply move
3417 the gap the minimum distance to get it out of the way, and then
3418 deal with an unbroken array. */
3420 /* Make sure the gap won't interfere, by moving it out of the text
3421 we will operate on. */
3422 if (start1 < gap && gap < end2)
3424 if (gap - start1 < end2 - gap)
3425 move_gap (start1);
3426 else
3427 move_gap (end2);
3430 start1_byte = CHAR_TO_BYTE (start1);
3431 start2_byte = CHAR_TO_BYTE (start2);
3432 len1_byte = CHAR_TO_BYTE (end1) - start1_byte;
3433 len2_byte = CHAR_TO_BYTE (end2) - start2_byte;
3435 #ifdef BYTE_COMBINING_DEBUG
3436 if (end1 == start2)
3438 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
3439 len2_byte, start1, start1_byte)
3440 || count_combining_before (BYTE_POS_ADDR (start1_byte),
3441 len1_byte, end2, start2_byte + len2_byte)
3442 || count_combining_after (BYTE_POS_ADDR (start1_byte),
3443 len1_byte, end2, start2_byte + len2_byte))
3444 abort ();
3446 else
3448 if (count_combining_before (BYTE_POS_ADDR (start2_byte),
3449 len2_byte, start1, start1_byte)
3450 || count_combining_before (BYTE_POS_ADDR (start1_byte),
3451 len1_byte, start2, start2_byte)
3452 || count_combining_after (BYTE_POS_ADDR (start2_byte),
3453 len2_byte, end1, start1_byte + len1_byte)
3454 || count_combining_after (BYTE_POS_ADDR (start1_byte),
3455 len1_byte, end2, start2_byte + len2_byte))
3456 abort ();
3458 #endif
3460 /* Hmmm... how about checking to see if the gap is large
3461 enough to use as the temporary storage? That would avoid an
3462 allocation... interesting. Later, don't fool with it now. */
3464 /* Working without memmove, for portability (sigh), so must be
3465 careful of overlapping subsections of the array... */
3467 if (end1 == start2) /* adjacent regions */
3469 modify_region (current_buffer, start1, end2);
3470 record_change (start1, len1 + len2);
3472 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3473 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
3474 Fset_text_properties (make_number (start1), make_number (end2),
3475 Qnil, Qnil);
3477 /* First region smaller than second. */
3478 if (len1_byte < len2_byte)
3480 /* We use alloca only if it is small,
3481 because we want to avoid stack overflow. */
3482 if (len2_byte > 20000)
3483 temp = (unsigned char *) xmalloc (len2_byte);
3484 else
3485 temp = (unsigned char *) alloca (len2_byte);
3487 /* Don't precompute these addresses. We have to compute them
3488 at the last minute, because the relocating allocator might
3489 have moved the buffer around during the xmalloc. */
3490 start1_addr = BYTE_POS_ADDR (start1_byte);
3491 start2_addr = BYTE_POS_ADDR (start2_byte);
3493 bcopy (start2_addr, temp, len2_byte);
3494 bcopy (start1_addr, start1_addr + len2_byte, len1_byte);
3495 bcopy (temp, start1_addr, len2_byte);
3496 if (len2_byte > 20000)
3497 free (temp);
3499 else
3500 /* First region not smaller than second. */
3502 if (len1_byte > 20000)
3503 temp = (unsigned char *) xmalloc (len1_byte);
3504 else
3505 temp = (unsigned char *) alloca (len1_byte);
3506 start1_addr = BYTE_POS_ADDR (start1_byte);
3507 start2_addr = BYTE_POS_ADDR (start2_byte);
3508 bcopy (start1_addr, temp, len1_byte);
3509 bcopy (start2_addr, start1_addr, len2_byte);
3510 bcopy (temp, start1_addr + len2_byte, len1_byte);
3511 if (len1_byte > 20000)
3512 free (temp);
3514 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
3515 len1, current_buffer, 0);
3516 graft_intervals_into_buffer (tmp_interval2, start1,
3517 len2, current_buffer, 0);
3518 update_compositions (start1, start1 + len2, CHECK_BORDER);
3519 update_compositions (start1 + len2, end2, CHECK_TAIL);
3521 /* Non-adjacent regions, because end1 != start2, bleagh... */
3522 else
3524 len_mid = start2_byte - (start1_byte + len1_byte);
3526 if (len1_byte == len2_byte)
3527 /* Regions are same size, though, how nice. */
3529 modify_region (current_buffer, start1, end1);
3530 modify_region (current_buffer, start2, end2);
3531 record_change (start1, len1);
3532 record_change (start2, len2);
3533 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3534 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
3535 Fset_text_properties (make_number (start1), make_number (end1),
3536 Qnil, Qnil);
3537 Fset_text_properties (make_number (start2), make_number (end2),
3538 Qnil, Qnil);
3540 if (len1_byte > 20000)
3541 temp = (unsigned char *) xmalloc (len1_byte);
3542 else
3543 temp = (unsigned char *) alloca (len1_byte);
3544 start1_addr = BYTE_POS_ADDR (start1_byte);
3545 start2_addr = BYTE_POS_ADDR (start2_byte);
3546 bcopy (start1_addr, temp, len1_byte);
3547 bcopy (start2_addr, start1_addr, len2_byte);
3548 bcopy (temp, start2_addr, len1_byte);
3549 if (len1_byte > 20000)
3550 free (temp);
3551 graft_intervals_into_buffer (tmp_interval1, start2,
3552 len1, current_buffer, 0);
3553 graft_intervals_into_buffer (tmp_interval2, start1,
3554 len2, current_buffer, 0);
3557 else if (len1_byte < len2_byte) /* Second region larger than first */
3558 /* Non-adjacent & unequal size, area between must also be shifted. */
3560 modify_region (current_buffer, start1, end2);
3561 record_change (start1, (end2 - start1));
3562 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3563 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
3564 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
3565 Fset_text_properties (make_number (start1), make_number (end2),
3566 Qnil, Qnil);
3568 /* holds region 2 */
3569 if (len2_byte > 20000)
3570 temp = (unsigned char *) xmalloc (len2_byte);
3571 else
3572 temp = (unsigned char *) alloca (len2_byte);
3573 start1_addr = BYTE_POS_ADDR (start1_byte);
3574 start2_addr = BYTE_POS_ADDR (start2_byte);
3575 bcopy (start2_addr, temp, len2_byte);
3576 bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte);
3577 safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
3578 bcopy (temp, start1_addr, len2_byte);
3579 if (len2_byte > 20000)
3580 free (temp);
3581 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
3582 len1, current_buffer, 0);
3583 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
3584 len_mid, current_buffer, 0);
3585 graft_intervals_into_buffer (tmp_interval2, start1,
3586 len2, current_buffer, 0);
3588 else
3589 /* Second region smaller than first. */
3591 record_change (start1, (end2 - start1));
3592 modify_region (current_buffer, start1, end2);
3594 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3595 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
3596 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
3597 Fset_text_properties (make_number (start1), make_number (end2),
3598 Qnil, Qnil);
3600 /* holds region 1 */
3601 if (len1_byte > 20000)
3602 temp = (unsigned char *) xmalloc (len1_byte);
3603 else
3604 temp = (unsigned char *) alloca (len1_byte);
3605 start1_addr = BYTE_POS_ADDR (start1_byte);
3606 start2_addr = BYTE_POS_ADDR (start2_byte);
3607 bcopy (start1_addr, temp, len1_byte);
3608 bcopy (start2_addr, start1_addr, len2_byte);
3609 bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
3610 bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte);
3611 if (len1_byte > 20000)
3612 free (temp);
3613 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
3614 len1, current_buffer, 0);
3615 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
3616 len_mid, current_buffer, 0);
3617 graft_intervals_into_buffer (tmp_interval2, start1,
3618 len2, current_buffer, 0);
3621 update_compositions (start1, start1 + len2, CHECK_BORDER);
3622 update_compositions (end2 - len1, end2, CHECK_BORDER);
3625 /* When doing multiple transpositions, it might be nice
3626 to optimize this. Perhaps the markers in any one buffer
3627 should be organized in some sorted data tree. */
3628 if (NILP (leave_markers))
3630 transpose_markers (start1, end1, start2, end2,
3631 start1_byte, start1_byte + len1_byte,
3632 start2_byte, start2_byte + len2_byte);
3633 fix_overlays_in_range (start1, end2);
3636 return Qnil;
3640 void
3641 syms_of_editfns ()
3643 environbuf = 0;
3645 Qbuffer_access_fontify_functions
3646 = intern ("buffer-access-fontify-functions");
3647 staticpro (&Qbuffer_access_fontify_functions);
3649 DEFVAR_LISP ("inhibit-field-text-motion", &Vinhibit_field_text_motion,
3650 "Non-nil means.text motion commands don't notice fields.");
3651 Vinhibit_field_text_motion = Qnil;
3653 DEFVAR_LISP ("buffer-access-fontify-functions",
3654 &Vbuffer_access_fontify_functions,
3655 "List of functions called by `buffer-substring' to fontify if necessary.\n\
3656 Each function is called with two arguments which specify the range\n\
3657 of the buffer being accessed.");
3658 Vbuffer_access_fontify_functions = Qnil;
3661 Lisp_Object obuf;
3662 extern Lisp_Object Vprin1_to_string_buffer;
3663 obuf = Fcurrent_buffer ();
3664 /* Do this here, because init_buffer_once is too early--it won't work. */
3665 Fset_buffer (Vprin1_to_string_buffer);
3666 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
3667 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
3668 Qnil);
3669 Fset_buffer (obuf);
3672 DEFVAR_LISP ("buffer-access-fontified-property",
3673 &Vbuffer_access_fontified_property,
3674 "Property which (if non-nil) indicates text has been fontified.\n\
3675 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
3676 functions if all the text being accessed has this property.");
3677 Vbuffer_access_fontified_property = Qnil;
3679 DEFVAR_LISP ("system-name", &Vsystem_name,
3680 "The name of the machine Emacs is running on.");
3682 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
3683 "The full name of the user logged in.");
3685 DEFVAR_LISP ("user-login-name", &Vuser_login_name,
3686 "The user's name, taken from environment variables if possible.");
3688 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name,
3689 "The user's name, based upon the real uid only.");
3691 defsubr (&Spropertize);
3692 defsubr (&Schar_equal);
3693 defsubr (&Sgoto_char);
3694 defsubr (&Sstring_to_char);
3695 defsubr (&Schar_to_string);
3696 defsubr (&Sbuffer_substring);
3697 defsubr (&Sbuffer_substring_no_properties);
3698 defsubr (&Sbuffer_string);
3700 defsubr (&Spoint_marker);
3701 defsubr (&Smark_marker);
3702 defsubr (&Spoint);
3703 defsubr (&Sregion_beginning);
3704 defsubr (&Sregion_end);
3706 staticpro (&Qfield);
3707 Qfield = intern ("field");
3708 defsubr (&Sfield_beginning);
3709 defsubr (&Sfield_end);
3710 defsubr (&Sfield_string);
3711 defsubr (&Sfield_string_no_properties);
3712 defsubr (&Sdelete_field);
3713 defsubr (&Sconstrain_to_field);
3715 defsubr (&Sline_beginning_position);
3716 defsubr (&Sline_end_position);
3718 /* defsubr (&Smark); */
3719 /* defsubr (&Sset_mark); */
3720 defsubr (&Ssave_excursion);
3721 defsubr (&Ssave_current_buffer);
3723 defsubr (&Sbufsize);
3724 defsubr (&Spoint_max);
3725 defsubr (&Spoint_min);
3726 defsubr (&Spoint_min_marker);
3727 defsubr (&Spoint_max_marker);
3728 defsubr (&Sgap_position);
3729 defsubr (&Sgap_size);
3730 defsubr (&Sposition_bytes);
3731 defsubr (&Sbyte_to_position);
3733 defsubr (&Sbobp);
3734 defsubr (&Seobp);
3735 defsubr (&Sbolp);
3736 defsubr (&Seolp);
3737 defsubr (&Sfollowing_char);
3738 defsubr (&Sprevious_char);
3739 defsubr (&Schar_after);
3740 defsubr (&Schar_before);
3741 defsubr (&Sinsert);
3742 defsubr (&Sinsert_before_markers);
3743 defsubr (&Sinsert_and_inherit);
3744 defsubr (&Sinsert_and_inherit_before_markers);
3745 defsubr (&Sinsert_char);
3747 defsubr (&Suser_login_name);
3748 defsubr (&Suser_real_login_name);
3749 defsubr (&Suser_uid);
3750 defsubr (&Suser_real_uid);
3751 defsubr (&Suser_full_name);
3752 defsubr (&Semacs_pid);
3753 defsubr (&Scurrent_time);
3754 defsubr (&Sformat_time_string);
3755 defsubr (&Sdecode_time);
3756 defsubr (&Sencode_time);
3757 defsubr (&Scurrent_time_string);
3758 defsubr (&Scurrent_time_zone);
3759 defsubr (&Sset_time_zone_rule);
3760 defsubr (&Ssystem_name);
3761 defsubr (&Smessage);
3762 defsubr (&Smessage_box);
3763 defsubr (&Smessage_or_box);
3764 defsubr (&Scurrent_message);
3765 defsubr (&Sformat);
3767 defsubr (&Sinsert_buffer_substring);
3768 defsubr (&Scompare_buffer_substrings);
3769 defsubr (&Ssubst_char_in_region);
3770 defsubr (&Stranslate_region);
3771 defsubr (&Sdelete_region);
3772 defsubr (&Sdelete_and_extract_region);
3773 defsubr (&Swiden);
3774 defsubr (&Snarrow_to_region);
3775 defsubr (&Ssave_restriction);
3776 defsubr (&Stranspose_regions);