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)
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. */
23 #include <sys/types.h>
36 #include "intervals.h"
44 #define min(a, b) ((a) < (b) ? (a) : (b))
45 #define max(a, b) ((a) > (b) ? (a) : (b))
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 /* Some static data, and a function to initialize it for each run */
68 Lisp_Object Vsystem_name
;
69 Lisp_Object Vuser_real_login_name
; /* login name of current user ID */
70 Lisp_Object Vuser_full_name
; /* full name of current user */
71 Lisp_Object Vuser_login_name
; /* user name from LOGNAME or USER */
77 register unsigned char *p
;
78 struct passwd
*pw
; /* password entry for the current user */
81 /* Set up system_name even when dumping. */
85 /* Don't bother with this on initial start when just dumping out */
88 #endif /* not CANNOT_DUMP */
90 pw
= (struct passwd
*) getpwuid (getuid ());
92 /* We let the real user name default to "root" because that's quite
93 accurate on MSDOG and because it lets Emacs find the init file.
94 (The DVX libraries override the Djgpp libraries here.) */
95 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "root");
97 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "unknown");
100 /* Get the effective user name, by consulting environment variables,
101 or the effective uid if those are unset. */
102 user_name
= (char *) getenv ("LOGNAME");
105 user_name
= (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
106 #else /* WINDOWSNT */
107 user_name
= (char *) getenv ("USER");
108 #endif /* WINDOWSNT */
111 pw
= (struct passwd
*) getpwuid (geteuid ());
112 user_name
= (char *) (pw
? pw
->pw_name
: "unknown");
114 Vuser_login_name
= build_string (user_name
);
116 /* If the user name claimed in the environment vars differs from
117 the real uid, use the claimed name to find the full name. */
118 tem
= Fstring_equal (Vuser_login_name
, Vuser_real_login_name
);
119 Vuser_full_name
= Fuser_full_name (NILP (tem
)? make_number (geteuid())
122 p
= (unsigned char *) getenv ("NAME");
124 Vuser_full_name
= build_string (p
);
125 else if (NILP (Vuser_full_name
))
126 Vuser_full_name
= build_string ("unknown");
129 DEFUN ("char-to-string", Fchar_to_string
, Schar_to_string
, 1, 1, 0,
130 "Convert arg CHAR to a string containing that character.")
132 Lisp_Object character
;
135 unsigned char workbuf
[4], *str
;
137 CHECK_NUMBER (character
, 0);
139 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
140 return make_string_from_bytes (str
, 1, len
);
143 DEFUN ("string-to-char", Fstring_to_char
, Sstring_to_char
, 1, 1, 0,
144 "Convert arg STRING to a character, the first character of that string.\n\
145 A multibyte character is handled correctly.")
147 register Lisp_Object string
;
149 register Lisp_Object val
;
150 register struct Lisp_String
*p
;
151 CHECK_STRING (string
, 0);
152 p
= XSTRING (string
);
155 if (STRING_MULTIBYTE (string
))
156 XSETFASTINT (val
, STRING_CHAR (p
->data
, STRING_BYTES (p
)));
158 XSETFASTINT (val
, p
->data
[0]);
161 XSETFASTINT (val
, 0);
166 buildmark (charpos
, bytepos
)
167 int charpos
, bytepos
;
169 register Lisp_Object mark
;
170 mark
= Fmake_marker ();
171 set_marker_both (mark
, Qnil
, charpos
, bytepos
);
175 DEFUN ("point", Fpoint
, Spoint
, 0, 0, 0,
176 "Return value of point, as an integer.\n\
177 Beginning of buffer is position (point-min)")
181 XSETFASTINT (temp
, PT
);
185 DEFUN ("point-marker", Fpoint_marker
, Spoint_marker
, 0, 0, 0,
186 "Return value of point, as a marker object.")
189 return buildmark (PT
, PT_BYTE
);
193 clip_to_bounds (lower
, num
, upper
)
194 int lower
, num
, upper
;
198 else if (num
> upper
)
204 DEFUN ("goto-char", Fgoto_char
, Sgoto_char
, 1, 1, "NGoto char: ",
205 "Set point to POSITION, a number or marker.\n\
206 Beginning of buffer is position (point-min), end is (point-max).\n\
207 If the position is in the middle of a multibyte form,\n\
208 the actual point is set at the head of the multibyte form\n\
209 except in the case that `enable-multibyte-characters' is nil.")
211 register Lisp_Object position
;
215 if (MARKERP (position
)
216 && current_buffer
== XMARKER (position
)->buffer
)
218 pos
= marker_position (position
);
220 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
222 SET_PT_BOTH (ZV
, ZV_BYTE
);
224 SET_PT_BOTH (pos
, marker_byte_position (position
));
229 CHECK_NUMBER_COERCE_MARKER (position
, 0);
231 pos
= clip_to_bounds (BEGV
, XINT (position
), ZV
);
237 region_limit (beginningp
)
240 extern Lisp_Object Vmark_even_if_inactive
; /* Defined in callint.c. */
241 register Lisp_Object m
;
242 if (!NILP (Vtransient_mark_mode
) && NILP (Vmark_even_if_inactive
)
243 && NILP (current_buffer
->mark_active
))
244 Fsignal (Qmark_inactive
, Qnil
);
245 m
= Fmarker_position (current_buffer
->mark
);
246 if (NILP (m
)) error ("There is no region now");
247 if ((PT
< XFASTINT (m
)) == beginningp
)
248 return (make_number (PT
));
253 DEFUN ("region-beginning", Fregion_beginning
, Sregion_beginning
, 0, 0, 0,
254 "Return position of beginning of region, as an integer.")
257 return (region_limit (1));
260 DEFUN ("region-end", Fregion_end
, Sregion_end
, 0, 0, 0,
261 "Return position of end of region, as an integer.")
264 return (region_limit (0));
267 DEFUN ("mark-marker", Fmark_marker
, Smark_marker
, 0, 0, 0,
268 "Return this buffer's mark, as a marker object.\n\
269 Watch out! Moving this marker changes the mark position.\n\
270 If you set the marker not to point anywhere, the buffer will have no mark.")
273 return current_buffer
->mark
;
276 /* Return nonzero if POS1 and POS2 have the same value
277 for the text property PROP. */
280 text_property_eq (prop
, pos1
, pos2
)
282 Lisp_Object pos1
, pos2
;
284 Lisp_Object pval1
, pval2
;
286 pval1
= Fget_text_property (pos1
, prop
, Qnil
);
287 pval2
= Fget_text_property (pos2
, prop
, Qnil
);
289 return EQ (pval1
, pval2
);
292 /* Return the direction from which the text-property PROP would be
293 inherited by any new text inserted at POS: 1 if it would be
294 inherited from the char after POS, -1 if it would be inherited from
295 the char before POS, and 0 if from neither. */
298 text_property_stickiness (prop
, pos
)
302 Lisp_Object front_sticky
;
304 if (XINT (pos
) > BEGV
)
305 /* Consider previous character. */
307 Lisp_Object prev_pos
, rear_non_sticky
;
309 prev_pos
= make_number (XINT (pos
) - 1);
310 rear_non_sticky
= Fget_text_property (prev_pos
, Qrear_nonsticky
, Qnil
);
312 if (EQ (rear_non_sticky
, Qnil
)
313 || (CONSP (rear_non_sticky
)
314 && !Fmemq (prop
, rear_non_sticky
)))
315 /* PROP is not rear-non-sticky, and since this takes precedence over
316 any front-stickiness, PROP is inherited from before. */
320 /* Consider following character. */
321 front_sticky
= Fget_text_property (pos
, Qfront_sticky
, Qnil
);
323 if (EQ (front_sticky
, Qt
)
324 || (CONSP (front_sticky
)
325 && Fmemq (prop
, front_sticky
)))
326 /* PROP is inherited from after. */
329 /* PROP is not inherited from either side. */
333 /* Symbol for the text property used to mark fields. */
336 /* Find the field surrounding POS in *BEG and *END. If POS is nil,
337 the value of point is used instead.
339 If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
340 position of a field, then the beginning of the previous field
341 is returned instead of the beginning of POS's field (since the end of
342 a field is actually also the beginning of the next input
343 field, this behavior is sometimes useful).
345 Either BEG or END may be 0, in which case the corresponding value
349 find_field (pos
, merge_at_boundary
, beg
, end
)
351 Lisp_Object merge_at_boundary
;
354 /* 1 if POS counts as the start of a field. */
355 int at_field_start
= 0;
356 /* 1 if POS counts as the end of a field. */
357 int at_field_end
= 0;
360 XSETFASTINT (pos
, PT
);
362 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
364 if (NILP (merge_at_boundary
) && XFASTINT (pos
) > BEGV
)
365 /* See if we need to handle the case where POS is at beginning of a
366 field, which can also be interpreted as the end of the previous
367 field. We decide which one by seeing which field the `field'
368 property sticks to. The case where if MERGE_AT_BOUNDARY is
369 non-nil (see function comment) is actually the more natural one;
370 then we avoid treating the beginning of a field specially. */
372 /* First see if POS is actually *at* a boundary. */
373 Lisp_Object after_field
, before_field
;
375 after_field
= Fget_text_property (pos
, Qfield
, Qnil
);
376 before_field
= Fget_text_property (make_number (XINT (pos
) - 1),
379 if (! EQ (after_field
, before_field
))
380 /* We are at a boundary, see which direction is inclusive. */
382 int stickiness
= text_property_stickiness (Qfield
, pos
);
386 else if (stickiness
< 0)
389 /* STICKINESS == 0 means that any inserted text will get a
390 `field' text-property of nil, so check to see if that
391 matches either of the adjacent characters (this being a
392 kind of "stickiness by default"). */
394 if (NILP (before_field
))
395 at_field_end
= 1; /* Sticks to the left. */
396 else if (NILP (after_field
))
397 at_field_start
= 1; /* Sticks to the right. */
405 /* POS is at the edge of a field, and we should consider it as
406 the beginning of the following field. */
407 *beg
= XFASTINT (pos
);
409 /* Find the previous field boundary. */
412 prev
= Fprevious_single_property_change (pos
, Qfield
, Qnil
, Qnil
);
413 *beg
= NILP (prev
) ? BEGV
: XFASTINT (prev
);
420 /* POS is at the edge of a field, and we should consider it as
421 the end of the previous field. */
422 *end
= XFASTINT (pos
);
424 /* Find the next field boundary. */
427 next
= Fnext_single_property_change (pos
, Qfield
, Qnil
, Qnil
);
428 *end
= NILP (next
) ? ZV
: XFASTINT (next
);
433 DEFUN ("delete-field", Fdelete_field
, Sdelete_field
, 0, 1, 0,
434 "Delete the field surrounding POS.\n\
435 A field is a region of text with the same `field' property.\n\
436 If POS is nil, the value of point is used for POS.")
441 find_field (pos
, Qnil
, &beg
, &end
);
443 del_range (beg
, end
);
447 DEFUN ("field-string", Ffield_string
, Sfield_string
, 0, 1, 0,
448 "Return the contents of the field surrounding POS as a string.\n\
449 A field is a region of text with the same `field' property.\n\
450 If POS is nil, the value of point is used for POS.")
455 find_field (pos
, Qnil
, &beg
, &end
);
456 return make_buffer_string (beg
, end
, 1);
459 DEFUN ("field-string-no-properties", Ffield_string_no_properties
, Sfield_string_no_properties
, 0, 1, 0,
460 "Return the contents of the field around POS, without text-properties.\n\
461 A field is a region of text with the same `field' property.\n\
462 If POS is nil, the value of point is used for POS.")
467 find_field (pos
, Qnil
, &beg
, &end
);
468 return make_buffer_string (beg
, end
, 0);
471 DEFUN ("field-beginning", Ffield_beginning
, Sfield_beginning
, 0, 2, 0,
472 "Return the beginning of the field surrounding POS.\n\
473 A field is a region of text with the same `field' property.\n\
474 If POS is nil, the value of point is used for POS.\n\
475 If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its\n\
476 field, then the beginning of the *previous* field is returned.")
477 (pos
, escape_from_edge
)
478 Lisp_Object pos
, escape_from_edge
;
481 find_field (pos
, escape_from_edge
, &beg
, 0);
482 return make_number (beg
);
485 DEFUN ("field-end", Ffield_end
, Sfield_end
, 0, 2, 0,
486 "Return the end of the field surrounding POS.\n\
487 A field is a region of text with the same `field' property.\n\
488 If POS is nil, the value of point is used for POS.\n\
489 If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,\n\
490 then the end of the *following* field is returned.")
491 (pos
, escape_from_edge
)
492 Lisp_Object pos
, escape_from_edge
;
495 find_field (pos
, escape_from_edge
, 0, &end
);
496 return make_number (end
);
499 DEFUN ("constrain-to-field", Fconstrain_to_field
, Sconstrain_to_field
, 2, 4, 0,
500 "Return the position closest to NEW-POS that is in the same field as OLD-POS.\n\
501 A field is a region of text with the same `field' property.\n\
502 If NEW-POS is nil, then the current point is used instead, and set to the\n\
503 constrained position if that is is different.\n\
505 If OLD-POS is at the boundary of two fields, then the allowable\n\
506 positions for NEW-POS depends on the value of the optional argument\n\
507 ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is\n\
508 constrained to the field that has the same `field' text-property\n\
509 as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE\n\
510 is non-nil, NEW-POS is constrained to the union of the two adjacent\n\
513 If the optional argument ONLY-IN-LINE is non-nil and constraining\n\
514 NEW-POS would move it to a different line, NEW-POS is returned\n\
515 unconstrained. This useful for commands that move by line, like\n\
516 \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries\n\
517 only in the case where they can still move to the right line.")
518 (new_pos
, old_pos
, escape_from_edge
, only_in_line
)
519 Lisp_Object new_pos
, old_pos
, escape_from_edge
, only_in_line
;
521 /* If non-zero, then the original point, before re-positioning. */
525 /* Use the current point, and afterwards, set it. */
528 XSETFASTINT (new_pos
, PT
);
531 if (!EQ (new_pos
, old_pos
) && !text_property_eq (Qfield
, new_pos
, old_pos
))
532 /* NEW_POS is not within the same field as OLD_POS; try to
533 move NEW_POS so that it is. */
536 Lisp_Object field_bound
;
538 CHECK_NUMBER_COERCE_MARKER (new_pos
, 0);
539 CHECK_NUMBER_COERCE_MARKER (old_pos
, 0);
541 fwd
= (XFASTINT (new_pos
) > XFASTINT (old_pos
));
544 field_bound
= Ffield_end (old_pos
, escape_from_edge
);
546 field_bound
= Ffield_beginning (old_pos
, escape_from_edge
);
548 if (/* If ONLY_IN_LINE is non-nil, we only constrain NEW_POS if doing
549 so would remain within the same line. */
551 /* In that case, see if ESCAPE_FROM_EDGE caused FIELD_BOUND
552 to jump to the other side of NEW_POS, which would mean
553 that NEW_POS is already acceptable, and that we don't
554 have to do the line-check. */
555 || ((XFASTINT (field_bound
) < XFASTINT (new_pos
)) ? !fwd
: fwd
)
556 /* If not, see if there's no newline intervening between
557 NEW_POS and FIELD_BOUND. */
558 || (find_before_next_newline (XFASTINT (new_pos
),
559 XFASTINT (field_bound
),
561 == XFASTINT (field_bound
)))
562 /* Constrain NEW_POS to FIELD_BOUND. */
563 new_pos
= field_bound
;
565 if (orig_point
&& XFASTINT (new_pos
) != orig_point
)
566 /* The NEW_POS argument was originally nil, so automatically set PT. */
567 SET_PT (XFASTINT (new_pos
));
573 DEFUN ("line-beginning-position", Fline_beginning_position
, Sline_beginning_position
,
575 "Return the character position of the first character on the current line.\n\
576 With argument N not nil or 1, move forward N - 1 lines first.\n\
577 If scan reaches end of buffer, return that position.\n\
578 The scan does not cross a field boundary unless it would move\n\
579 beyond there to a different line. And if N is nil or 1,\n\
580 and scan starts at a field boundary, the scan stops as soon as it starts.\n\n\
581 This function does not move point.")
585 register int orig
, orig_byte
, end
;
594 Fforward_line (make_number (XINT (n
) - 1));
597 SET_PT_BOTH (orig
, orig_byte
);
599 /* Return END constrained to the current input field. */
600 return Fconstrain_to_field (make_number (end
), make_number (orig
),
601 XINT (n
) != 1 ? Qt
: Qnil
,
605 DEFUN ("line-end-position", Fline_end_position
, Sline_end_position
,
607 "Return the character position of the last character on the current line.\n\
608 With argument N not nil or 1, move forward N - 1 lines first.\n\
609 If scan reaches end of buffer, return that position.\n\
610 This function does not move point.")
615 register int orig
= PT
;
622 end_pos
= find_before_next_newline (orig
, 0, XINT (n
) - (XINT (n
) <= 0));
624 /* Return END_POS constrained to the current input field. */
626 Fconstrain_to_field (make_number (end_pos
), make_number (orig
), Qnil
, Qt
);
630 save_excursion_save ()
632 register int visible
= (XBUFFER (XWINDOW (selected_window
)->buffer
)
635 return Fcons (Fpoint_marker (),
636 Fcons (Fcopy_marker (current_buffer
->mark
, Qnil
),
637 Fcons (visible
? Qt
: Qnil
,
638 current_buffer
->mark_active
)));
642 save_excursion_restore (info
)
645 Lisp_Object tem
, tem1
, omark
, nmark
;
646 struct gcpro gcpro1
, gcpro2
, gcpro3
;
648 tem
= Fmarker_buffer (Fcar (info
));
649 /* If buffer being returned to is now deleted, avoid error */
650 /* Otherwise could get error here while unwinding to top level
652 /* In that case, Fmarker_buffer returns nil now. */
656 omark
= nmark
= Qnil
;
657 GCPRO3 (info
, omark
, nmark
);
662 unchain_marker (tem
);
663 tem
= Fcar (Fcdr (info
));
664 omark
= Fmarker_position (current_buffer
->mark
);
665 Fset_marker (current_buffer
->mark
, tem
, Fcurrent_buffer ());
666 nmark
= Fmarker_position (tem
);
667 unchain_marker (tem
);
668 tem
= Fcdr (Fcdr (info
));
669 #if 0 /* We used to make the current buffer visible in the selected window
670 if that was true previously. That avoids some anomalies.
671 But it creates others, and it wasn't documented, and it is simpler
672 and cleaner never to alter the window/buffer connections. */
675 && current_buffer
!= XBUFFER (XWINDOW (selected_window
)->buffer
))
676 Fswitch_to_buffer (Fcurrent_buffer (), Qnil
);
679 tem1
= current_buffer
->mark_active
;
680 current_buffer
->mark_active
= Fcdr (tem
);
681 if (!NILP (Vrun_hooks
))
683 /* If mark is active now, and either was not active
684 or was at a different place, run the activate hook. */
685 if (! NILP (current_buffer
->mark_active
))
687 if (! EQ (omark
, nmark
))
688 call1 (Vrun_hooks
, intern ("activate-mark-hook"));
690 /* If mark has ceased to be active, run deactivate hook. */
691 else if (! NILP (tem1
))
692 call1 (Vrun_hooks
, intern ("deactivate-mark-hook"));
698 DEFUN ("save-excursion", Fsave_excursion
, Ssave_excursion
, 0, UNEVALLED
, 0,
699 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
700 Executes BODY just like `progn'.\n\
701 The values of point, mark and the current buffer are restored\n\
702 even in case of abnormal exit (throw or error).\n\
703 The state of activation of the mark is also restored.\n\
705 This construct does not save `deactivate-mark', and therefore\n\
706 functions that change the buffer will still cause deactivation\n\
707 of the mark at the end of the command. To prevent that, bind\n\
708 `deactivate-mark' with `let'.")
712 register Lisp_Object val
;
713 int count
= specpdl_ptr
- specpdl
;
715 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
718 return unbind_to (count
, val
);
721 DEFUN ("save-current-buffer", Fsave_current_buffer
, Ssave_current_buffer
, 0, UNEVALLED
, 0,
722 "Save the current buffer; execute BODY; restore the current buffer.\n\
723 Executes BODY just like `progn'.")
727 register Lisp_Object val
;
728 int count
= specpdl_ptr
- specpdl
;
730 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
733 return unbind_to (count
, val
);
736 DEFUN ("buffer-size", Fbufsize
, Sbufsize
, 0, 1, 0,
737 "Return the number of characters in the current buffer.\n\
738 If BUFFER, return the number of characters in that buffer instead.")
743 return make_number (Z
- BEG
);
746 CHECK_BUFFER (buffer
, 1);
747 return make_number (BUF_Z (XBUFFER (buffer
))
748 - BUF_BEG (XBUFFER (buffer
)));
752 DEFUN ("point-min", Fpoint_min
, Spoint_min
, 0, 0, 0,
753 "Return the minimum permissible value of point in the current buffer.\n\
754 This is 1, unless narrowing (a buffer restriction) is in effect.")
758 XSETFASTINT (temp
, BEGV
);
762 DEFUN ("point-min-marker", Fpoint_min_marker
, Spoint_min_marker
, 0, 0, 0,
763 "Return a marker to the minimum permissible value of point in this buffer.\n\
764 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
767 return buildmark (BEGV
, BEGV_BYTE
);
770 DEFUN ("point-max", Fpoint_max
, Spoint_max
, 0, 0, 0,
771 "Return the maximum permissible value of point in the current buffer.\n\
772 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
773 is in effect, in which case it is less.")
777 XSETFASTINT (temp
, ZV
);
781 DEFUN ("point-max-marker", Fpoint_max_marker
, Spoint_max_marker
, 0, 0, 0,
782 "Return a marker to the maximum permissible value of point in this buffer.\n\
783 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
784 is in effect, in which case it is less.")
787 return buildmark (ZV
, ZV_BYTE
);
790 DEFUN ("gap-position", Fgap_position
, Sgap_position
, 0, 0, 0,
791 "Return the position of the gap, in the current buffer.\n\
792 See also `gap-size'.")
796 XSETFASTINT (temp
, GPT
);
800 DEFUN ("gap-size", Fgap_size
, Sgap_size
, 0, 0, 0,
801 "Return the size of the current buffer's gap.\n\
802 See also `gap-position'.")
806 XSETFASTINT (temp
, GAP_SIZE
);
810 DEFUN ("position-bytes", Fposition_bytes
, Sposition_bytes
, 1, 1, 0,
811 "Return the byte position for character position POSITION.\n\
812 If POSITION is out of range, the value is nil.")
814 Lisp_Object position
;
816 CHECK_NUMBER_COERCE_MARKER (position
, 1);
817 if (XINT (position
) < BEG
|| XINT (position
) > Z
)
819 return make_number (CHAR_TO_BYTE (XINT (position
)));
822 DEFUN ("byte-to-position", Fbyte_to_position
, Sbyte_to_position
, 1, 1, 0,
823 "Return the character position for byte position BYTEPOS.\n\
824 If BYTEPOS is out of range, the value is nil.")
828 CHECK_NUMBER (bytepos
, 1);
829 if (XINT (bytepos
) < BEG_BYTE
|| XINT (bytepos
) > Z_BYTE
)
831 return make_number (BYTE_TO_CHAR (XINT (bytepos
)));
834 DEFUN ("following-char", Ffollowing_char
, Sfollowing_char
, 0, 0, 0,
835 "Return the character following point, as a number.\n\
836 At the end of the buffer or accessible region, return 0.\n\
837 If `enable-multibyte-characters' is nil or point is not\n\
838 at character boundary, multibyte form is ignored,\n\
839 and only one byte following point is returned as a character.")
844 XSETFASTINT (temp
, 0);
846 XSETFASTINT (temp
, FETCH_CHAR (PT_BYTE
));
850 DEFUN ("preceding-char", Fprevious_char
, Sprevious_char
, 0, 0, 0,
851 "Return the character preceding point, as a number.\n\
852 At the beginning of the buffer or accessible region, return 0.\n\
853 If `enable-multibyte-characters' is nil or point is not\n\
854 at character boundary, multi-byte form is ignored,\n\
855 and only one byte preceding point is returned as a character.")
860 XSETFASTINT (temp
, 0);
861 else if (!NILP (current_buffer
->enable_multibyte_characters
))
865 XSETFASTINT (temp
, FETCH_CHAR (pos
));
868 XSETFASTINT (temp
, FETCH_BYTE (PT_BYTE
- 1));
872 DEFUN ("bobp", Fbobp
, Sbobp
, 0, 0, 0,
873 "Return t if point is at the beginning of the buffer.\n\
874 If the buffer is narrowed, this means the beginning of the narrowed part.")
882 DEFUN ("eobp", Feobp
, Seobp
, 0, 0, 0,
883 "Return t if point is at the end of the buffer.\n\
884 If the buffer is narrowed, this means the end of the narrowed part.")
892 DEFUN ("bolp", Fbolp
, Sbolp
, 0, 0, 0,
893 "Return t if point is at the beginning of a line.")
896 if (PT
== BEGV
|| FETCH_BYTE (PT_BYTE
- 1) == '\n')
901 DEFUN ("eolp", Feolp
, Seolp
, 0, 0, 0,
902 "Return t if point is at the end of a line.\n\
903 `End of a line' includes point being at the end of the buffer.")
906 if (PT
== ZV
|| FETCH_BYTE (PT_BYTE
) == '\n')
911 DEFUN ("char-after", Fchar_after
, Schar_after
, 0, 1, 0,
912 "Return character in current buffer at position POS.\n\
913 POS is an integer or a buffer pointer.\n\
914 If POS is out of range, the value is nil.")
918 register int pos_byte
;
923 XSETFASTINT (pos
, PT
);
928 pos_byte
= marker_byte_position (pos
);
929 if (pos_byte
< BEGV_BYTE
|| pos_byte
>= ZV_BYTE
)
934 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
935 if (XINT (pos
) < BEGV
|| XINT (pos
) >= ZV
)
938 pos_byte
= CHAR_TO_BYTE (XINT (pos
));
941 return make_number (FETCH_CHAR (pos_byte
));
944 DEFUN ("char-before", Fchar_before
, Schar_before
, 0, 1, 0,
945 "Return character in current buffer preceding position POS.\n\
946 POS is an integer or a buffer pointer.\n\
947 If POS is out of range, the value is nil.")
951 register Lisp_Object val
;
952 register int pos_byte
;
957 XSETFASTINT (pos
, PT
);
962 pos_byte
= marker_byte_position (pos
);
964 if (pos_byte
<= BEGV_BYTE
|| pos_byte
> ZV_BYTE
)
969 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
971 if (XINT (pos
) <= BEGV
|| XINT (pos
) > ZV
)
974 pos_byte
= CHAR_TO_BYTE (XINT (pos
));
977 if (!NILP (current_buffer
->enable_multibyte_characters
))
980 XSETFASTINT (val
, FETCH_CHAR (pos_byte
));
985 XSETFASTINT (val
, FETCH_BYTE (pos_byte
));
990 DEFUN ("user-login-name", Fuser_login_name
, Suser_login_name
, 0, 1, 0,
991 "Return the name under which the user logged in, as a string.\n\
992 This is based on the effective uid, not the real uid.\n\
993 Also, if the environment variable LOGNAME or USER is set,\n\
994 that determines the value of this function.\n\n\
995 If optional argument UID is an integer, return the login name of the user\n\
996 with that uid, or nil if there is no such user.")
1002 /* Set up the user name info if we didn't do it before.
1003 (That can happen if Emacs is dumpable
1004 but you decide to run `temacs -l loadup' and not dump. */
1005 if (INTEGERP (Vuser_login_name
))
1009 return Vuser_login_name
;
1011 CHECK_NUMBER (uid
, 0);
1012 pw
= (struct passwd
*) getpwuid (XINT (uid
));
1013 return (pw
? build_string (pw
->pw_name
) : Qnil
);
1016 DEFUN ("user-real-login-name", Fuser_real_login_name
, Suser_real_login_name
,
1018 "Return the name of the user's real uid, as a string.\n\
1019 This ignores the environment variables LOGNAME and USER, so it differs from\n\
1020 `user-login-name' when running under `su'.")
1023 /* Set up the user name info if we didn't do it before.
1024 (That can happen if Emacs is dumpable
1025 but you decide to run `temacs -l loadup' and not dump. */
1026 if (INTEGERP (Vuser_login_name
))
1028 return Vuser_real_login_name
;
1031 DEFUN ("user-uid", Fuser_uid
, Suser_uid
, 0, 0, 0,
1032 "Return the effective uid of Emacs, as an integer.")
1035 return make_number (geteuid ());
1038 DEFUN ("user-real-uid", Fuser_real_uid
, Suser_real_uid
, 0, 0, 0,
1039 "Return the real uid of Emacs, as an integer.")
1042 return make_number (getuid ());
1045 DEFUN ("user-full-name", Fuser_full_name
, Suser_full_name
, 0, 1, 0,
1046 "Return the full name of the user logged in, as a string.\n\
1047 If the full name corresponding to Emacs's userid is not known,\n\
1048 return \"unknown\".\n\
1050 If optional argument UID is an integer, return the full name of the user\n\
1051 with that uid, or nil if there is no such user.\n\
1052 If UID is a string, return the full name of the user with that login\n\
1053 name, or nil if there is no such user.")
1058 register unsigned char *p
, *q
;
1059 extern char *index ();
1063 return Vuser_full_name
;
1064 else if (NUMBERP (uid
))
1065 pw
= (struct passwd
*) getpwuid (XINT (uid
));
1066 else if (STRINGP (uid
))
1067 pw
= (struct passwd
*) getpwnam (XSTRING (uid
)->data
);
1069 error ("Invalid UID specification");
1074 p
= (unsigned char *) USER_FULL_NAME
;
1075 /* Chop off everything after the first comma. */
1076 q
= (unsigned char *) index (p
, ',');
1077 full
= make_string (p
, q
? q
- p
: strlen (p
));
1079 #ifdef AMPERSAND_FULL_NAME
1080 p
= XSTRING (full
)->data
;
1081 q
= (unsigned char *) index (p
, '&');
1082 /* Substitute the login name for the &, upcasing the first character. */
1085 register unsigned char *r
;
1088 login
= Fuser_login_name (make_number (pw
->pw_uid
));
1089 r
= (unsigned char *) alloca (strlen (p
) + XSTRING (login
)->size
+ 1);
1090 bcopy (p
, r
, q
- p
);
1092 strcat (r
, XSTRING (login
)->data
);
1093 r
[q
- p
] = UPCASE (r
[q
- p
]);
1095 full
= build_string (r
);
1097 #endif /* AMPERSAND_FULL_NAME */
1102 DEFUN ("system-name", Fsystem_name
, Ssystem_name
, 0, 0, 0,
1103 "Return the name of the machine you are running on, as a string.")
1106 return Vsystem_name
;
1109 /* For the benefit of callers who don't want to include lisp.h */
1113 if (STRINGP (Vsystem_name
))
1114 return (char *) XSTRING (Vsystem_name
)->data
;
1119 DEFUN ("emacs-pid", Femacs_pid
, Semacs_pid
, 0, 0, 0,
1120 "Return the process ID of Emacs, as an integer.")
1123 return make_number (getpid ());
1126 DEFUN ("current-time", Fcurrent_time
, Scurrent_time
, 0, 0, 0,
1127 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
1128 The time is returned as a list of three integers. The first has the\n\
1129 most significant 16 bits of the seconds, while the second has the\n\
1130 least significant 16 bits. The third integer gives the microsecond\n\
1133 The microsecond count is zero on systems that do not provide\n\
1134 resolution finer than a second.")
1138 Lisp_Object result
[3];
1141 XSETINT (result
[0], (EMACS_SECS (t
) >> 16) & 0xffff);
1142 XSETINT (result
[1], (EMACS_SECS (t
) >> 0) & 0xffff);
1143 XSETINT (result
[2], EMACS_USECS (t
));
1145 return Flist (3, result
);
1150 lisp_time_argument (specified_time
, result
)
1151 Lisp_Object specified_time
;
1154 if (NILP (specified_time
))
1155 return time (result
) != -1;
1158 Lisp_Object high
, low
;
1159 high
= Fcar (specified_time
);
1160 CHECK_NUMBER (high
, 0);
1161 low
= Fcdr (specified_time
);
1164 CHECK_NUMBER (low
, 0);
1165 *result
= (XINT (high
) << 16) + (XINT (low
) & 0xffff);
1166 return *result
>> 16 == XINT (high
);
1170 /* Write information into buffer S of size MAXSIZE, according to the
1171 FORMAT of length FORMAT_LEN, using time information taken from *TP.
1172 Default to Universal Time if UT is nonzero, local time otherwise.
1173 Return the number of bytes written, not including the terminating
1174 '\0'. If S is NULL, nothing will be written anywhere; so to
1175 determine how many bytes would be written, use NULL for S and
1176 ((size_t) -1) for MAXSIZE.
1178 This function behaves like emacs_strftimeu, except it allows null
1181 emacs_memftimeu (s
, maxsize
, format
, format_len
, tp
, ut
)
1186 const struct tm
*tp
;
1191 /* Loop through all the null-terminated strings in the format
1192 argument. Normally there's just one null-terminated string, but
1193 there can be arbitrarily many, concatenated together, if the
1194 format contains '\0' bytes. emacs_strftimeu stops at the first
1195 '\0' byte so we must invoke it separately for each such string. */
1204 result
= emacs_strftimeu (s
, maxsize
, format
, tp
, ut
);
1208 if (result
== 0 && s
[0] != '\0')
1213 maxsize
-= result
+ 1;
1215 len
= strlen (format
);
1216 if (len
== format_len
)
1220 format_len
-= len
+ 1;
1225 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
1226 "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
1227 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
1228 `current-time' or `file-attributes'.\n\
1229 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
1230 as Universal Time; nil means describe TIME in the local time zone.\n\
1231 The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
1232 by text that describes the specified date and time in TIME:\n\
1234 %Y is the year, %y within the century, %C the century.\n\
1235 %G is the year corresponding to the ISO week, %g within the century.\n\
1236 %m is the numeric month.\n\
1237 %b and %h are the locale's abbreviated month name, %B the full name.\n\
1238 %d is the day of the month, zero-padded, %e is blank-padded.\n\
1239 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\
1240 %a is the locale's abbreviated name of the day of week, %A the full name.\n\
1241 %U is the week number starting on Sunday, %W starting on Monday,\n\
1242 %V according to ISO 8601.\n\
1243 %j is the day of the year.\n\
1245 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\
1246 only blank-padded, %l is like %I blank-padded.\n\
1247 %p is the locale's equivalent of either AM or PM.\n\
1248 %M is the minute.\n\
1249 %S is the second.\n\
1250 %Z is the time zone name, %z is the numeric form.\n\
1251 %s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\
1253 %c is the locale's date and time format.\n\
1254 %x is the locale's \"preferred\" date format.\n\
1255 %D is like \"%m/%d/%y\".\n\
1257 %R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\
1258 %X is the locale's \"preferred\" time format.\n\
1260 Finally, %n is a newline, %t is a tab, %% is a literal %.\n\
1262 Certain flags and modifiers are available with some format controls.\n\
1263 The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
1264 but padded with blanks; %-X is like %X, but without padding.\n\
1265 %NX (where N stands for an integer) is like %X,\n\
1266 but takes up at least N (a number) positions.\n\
1267 The modifiers are `E' and `O'. For certain characters X,\n\
1268 %EX is a locale's alternative version of %X;\n\
1269 %OX is like %X, but uses the locale's number symbols.\n\
1271 For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
1272 (format_string, time, universal)
1275 DEFUN ("format-time-string", Fformat_time_string
, Sformat_time_string
, 1, 3, 0,
1276 0 /* See immediately above */)
1277 (format_string
, time
, universal
)
1278 Lisp_Object format_string
, time
, universal
;
1283 int ut
= ! NILP (universal
);
1285 CHECK_STRING (format_string
, 1);
1287 if (! lisp_time_argument (time
, &value
))
1288 error ("Invalid time specification");
1290 format_string
= code_convert_string_norecord (format_string
,
1291 Vlocale_coding_system
, 1);
1293 /* This is probably enough. */
1294 size
= STRING_BYTES (XSTRING (format_string
)) * 6 + 50;
1296 tm
= ut
? gmtime (&value
) : localtime (&value
);
1298 error ("Specified time is not representable");
1300 synchronize_system_time_locale ();
1304 char *buf
= (char *) alloca (size
+ 1);
1308 result
= emacs_memftimeu (buf
, size
, XSTRING (format_string
)->data
,
1309 STRING_BYTES (XSTRING (format_string
)),
1311 if ((result
> 0 && result
< size
) || (result
== 0 && buf
[0] == '\0'))
1312 return code_convert_string_norecord (make_string (buf
, result
),
1313 Vlocale_coding_system
, 0);
1315 /* If buffer was too small, make it bigger and try again. */
1316 result
= emacs_memftimeu (NULL
, (size_t) -1,
1317 XSTRING (format_string
)->data
,
1318 STRING_BYTES (XSTRING (format_string
)),
1324 DEFUN ("decode-time", Fdecode_time
, Sdecode_time
, 0, 1, 0,
1325 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
1326 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
1327 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
1328 to use the current time. The list has the following nine members:\n\
1329 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
1330 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
1331 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
1332 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
1333 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
1334 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
1335 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
1336 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
1338 Lisp_Object specified_time
;
1342 struct tm
*decoded_time
;
1343 Lisp_Object list_args
[9];
1345 if (! lisp_time_argument (specified_time
, &time_spec
))
1346 error ("Invalid time specification");
1348 decoded_time
= localtime (&time_spec
);
1350 error ("Specified time is not representable");
1351 XSETFASTINT (list_args
[0], decoded_time
->tm_sec
);
1352 XSETFASTINT (list_args
[1], decoded_time
->tm_min
);
1353 XSETFASTINT (list_args
[2], decoded_time
->tm_hour
);
1354 XSETFASTINT (list_args
[3], decoded_time
->tm_mday
);
1355 XSETFASTINT (list_args
[4], decoded_time
->tm_mon
+ 1);
1356 XSETINT (list_args
[5], decoded_time
->tm_year
+ 1900);
1357 XSETFASTINT (list_args
[6], decoded_time
->tm_wday
);
1358 list_args
[7] = (decoded_time
->tm_isdst
)? Qt
: Qnil
;
1360 /* Make a copy, in case gmtime modifies the struct. */
1361 save_tm
= *decoded_time
;
1362 decoded_time
= gmtime (&time_spec
);
1363 if (decoded_time
== 0)
1364 list_args
[8] = Qnil
;
1366 XSETINT (list_args
[8], tm_diff (&save_tm
, decoded_time
));
1367 return Flist (9, list_args
);
1370 DEFUN ("encode-time", Fencode_time
, Sencode_time
, 6, MANY
, 0,
1371 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
1372 This is the reverse operation of `decode-time', which see.\n\
1373 ZONE defaults to the current time zone rule. This can\n\
1374 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
1375 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
1376 applied without consideration for daylight savings time.\n\
1378 You can pass more than 7 arguments; then the first six arguments\n\
1379 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
1380 The intervening arguments are ignored.\n\
1381 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
1383 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
1384 for example, a DAY of 0 means the day preceding the given month.\n\
1385 Year numbers less than 100 are treated just like other year numbers.\n\
1386 If you want them to stand for years in this century, you must do that yourself.")
1389 register Lisp_Object
*args
;
1393 Lisp_Object zone
= (nargs
> 6 ? args
[nargs
- 1] : Qnil
);
1395 CHECK_NUMBER (args
[0], 0); /* second */
1396 CHECK_NUMBER (args
[1], 1); /* minute */
1397 CHECK_NUMBER (args
[2], 2); /* hour */
1398 CHECK_NUMBER (args
[3], 3); /* day */
1399 CHECK_NUMBER (args
[4], 4); /* month */
1400 CHECK_NUMBER (args
[5], 5); /* year */
1402 tm
.tm_sec
= XINT (args
[0]);
1403 tm
.tm_min
= XINT (args
[1]);
1404 tm
.tm_hour
= XINT (args
[2]);
1405 tm
.tm_mday
= XINT (args
[3]);
1406 tm
.tm_mon
= XINT (args
[4]) - 1;
1407 tm
.tm_year
= XINT (args
[5]) - 1900;
1413 time
= mktime (&tm
);
1418 char **oldenv
= environ
, **newenv
;
1422 else if (STRINGP (zone
))
1423 tzstring
= (char *) XSTRING (zone
)->data
;
1424 else if (INTEGERP (zone
))
1426 int abszone
= abs (XINT (zone
));
1427 sprintf (tzbuf
, "XXX%s%d:%02d:%02d", "-" + (XINT (zone
) < 0),
1428 abszone
/ (60*60), (abszone
/60) % 60, abszone
% 60);
1432 error ("Invalid time zone specification");
1434 /* Set TZ before calling mktime; merely adjusting mktime's returned
1435 value doesn't suffice, since that would mishandle leap seconds. */
1436 set_time_zone_rule (tzstring
);
1438 time
= mktime (&tm
);
1440 /* Restore TZ to previous value. */
1444 #ifdef LOCALTIME_CACHE
1449 if (time
== (time_t) -1)
1450 error ("Specified time is not representable");
1452 return make_time (time
);
1455 DEFUN ("current-time-string", Fcurrent_time_string
, Scurrent_time_string
, 0, 1, 0,
1456 "Return the current time, as a human-readable string.\n\
1457 Programs can use this function to decode a time,\n\
1458 since the number of columns in each field is fixed.\n\
1459 The format is `Sun Sep 16 01:03:52 1973'.\n\
1460 However, see also the functions `decode-time' and `format-time-string'\n\
1461 which provide a much more powerful and general facility.\n\
1463 If an argument is given, it specifies a time to format\n\
1464 instead of the current time. The argument should have the form:\n\
1467 (HIGH LOW . IGNORED).\n\
1468 Thus, you can use times obtained from `current-time'\n\
1469 and from `file-attributes'.")
1471 Lisp_Object specified_time
;
1477 if (! lisp_time_argument (specified_time
, &value
))
1479 tem
= (char *) ctime (&value
);
1481 strncpy (buf
, tem
, 24);
1484 return build_string (buf
);
1487 #define TM_YEAR_BASE 1900
1489 /* Yield A - B, measured in seconds.
1490 This function is copied from the GNU C Library. */
1495 /* Compute intervening leap days correctly even if year is negative.
1496 Take care to avoid int overflow in leap day calculations,
1497 but it's OK to assume that A and B are close to each other. */
1498 int a4
= (a
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (a
->tm_year
& 3);
1499 int b4
= (b
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (b
->tm_year
& 3);
1500 int a100
= a4
/ 25 - (a4
% 25 < 0);
1501 int b100
= b4
/ 25 - (b4
% 25 < 0);
1502 int a400
= a100
>> 2;
1503 int b400
= b100
>> 2;
1504 int intervening_leap_days
= (a4
- b4
) - (a100
- b100
) + (a400
- b400
);
1505 int years
= a
->tm_year
- b
->tm_year
;
1506 int days
= (365 * years
+ intervening_leap_days
1507 + (a
->tm_yday
- b
->tm_yday
));
1508 return (60 * (60 * (24 * days
+ (a
->tm_hour
- b
->tm_hour
))
1509 + (a
->tm_min
- b
->tm_min
))
1510 + (a
->tm_sec
- b
->tm_sec
));
1513 DEFUN ("current-time-zone", Fcurrent_time_zone
, Scurrent_time_zone
, 0, 1, 0,
1514 "Return the offset and name for the local time zone.\n\
1515 This returns a list of the form (OFFSET NAME).\n\
1516 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1517 A negative value means west of Greenwich.\n\
1518 NAME is a string giving the name of the time zone.\n\
1519 If an argument is given, it specifies when the time zone offset is determined\n\
1520 instead of using the current time. The argument should have the form:\n\
1523 (HIGH LOW . IGNORED).\n\
1524 Thus, you can use times obtained from `current-time'\n\
1525 and from `file-attributes'.\n\
1527 Some operating systems cannot provide all this information to Emacs;\n\
1528 in this case, `current-time-zone' returns a list containing nil for\n\
1529 the data it can't find.")
1531 Lisp_Object specified_time
;
1537 if (lisp_time_argument (specified_time
, &value
)
1538 && (t
= gmtime (&value
)) != 0
1539 && (gmt
= *t
, t
= localtime (&value
)) != 0)
1541 int offset
= tm_diff (t
, &gmt
);
1546 s
= (char *)t
->tm_zone
;
1547 #else /* not HAVE_TM_ZONE */
1549 if (t
->tm_isdst
== 0 || t
->tm_isdst
== 1)
1550 s
= tzname
[t
->tm_isdst
];
1552 #endif /* not HAVE_TM_ZONE */
1555 /* No local time zone name is available; use "+-NNNN" instead. */
1556 int am
= (offset
< 0 ? -offset
: offset
) / 60;
1557 sprintf (buf
, "%c%02d%02d", (offset
< 0 ? '-' : '+'), am
/60, am
%60);
1560 return Fcons (make_number (offset
), Fcons (build_string (s
), Qnil
));
1563 return Fmake_list (make_number (2), Qnil
);
1566 /* This holds the value of `environ' produced by the previous
1567 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1568 has never been called. */
1569 static char **environbuf
;
1571 DEFUN ("set-time-zone-rule", Fset_time_zone_rule
, Sset_time_zone_rule
, 1, 1, 0,
1572 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
1573 If TZ is nil, use implementation-defined default time zone information.\n\
1574 If TZ is t, use Universal Time.")
1582 else if (EQ (tz
, Qt
))
1586 CHECK_STRING (tz
, 0);
1587 tzstring
= (char *) XSTRING (tz
)->data
;
1590 set_time_zone_rule (tzstring
);
1593 environbuf
= environ
;
1598 #ifdef LOCALTIME_CACHE
1600 /* These two values are known to load tz files in buggy implementations,
1601 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1602 Their values shouldn't matter in non-buggy implementations.
1603 We don't use string literals for these strings,
1604 since if a string in the environment is in readonly
1605 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1606 See Sun bugs 1113095 and 1114114, ``Timezone routines
1607 improperly modify environment''. */
1609 static char set_time_zone_rule_tz1
[] = "TZ=GMT+0";
1610 static char set_time_zone_rule_tz2
[] = "TZ=GMT+1";
1614 /* Set the local time zone rule to TZSTRING.
1615 This allocates memory into `environ', which it is the caller's
1616 responsibility to free. */
1618 set_time_zone_rule (tzstring
)
1622 char **from
, **to
, **newenv
;
1624 /* Make the ENVIRON vector longer with room for TZSTRING. */
1625 for (from
= environ
; *from
; from
++)
1627 envptrs
= from
- environ
+ 2;
1628 newenv
= to
= (char **) xmalloc (envptrs
* sizeof (char *)
1629 + (tzstring
? strlen (tzstring
) + 4 : 0));
1631 /* Add TZSTRING to the end of environ, as a value for TZ. */
1634 char *t
= (char *) (to
+ envptrs
);
1636 strcat (t
, tzstring
);
1640 /* Copy the old environ vector elements into NEWENV,
1641 but don't copy the TZ variable.
1642 So we have only one definition of TZ, which came from TZSTRING. */
1643 for (from
= environ
; *from
; from
++)
1644 if (strncmp (*from
, "TZ=", 3) != 0)
1650 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1651 the TZ variable is stored. If we do not have a TZSTRING,
1652 TO points to the vector slot which has the terminating null. */
1654 #ifdef LOCALTIME_CACHE
1656 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1657 "US/Pacific" that loads a tz file, then changes to a value like
1658 "XXX0" that does not load a tz file, and then changes back to
1659 its original value, the last change is (incorrectly) ignored.
1660 Also, if TZ changes twice in succession to values that do
1661 not load a tz file, tzset can dump core (see Sun bug#1225179).
1662 The following code works around these bugs. */
1666 /* Temporarily set TZ to a value that loads a tz file
1667 and that differs from tzstring. */
1669 *newenv
= (strcmp (tzstring
, set_time_zone_rule_tz1
+ 3) == 0
1670 ? set_time_zone_rule_tz2
: set_time_zone_rule_tz1
);
1676 /* The implied tzstring is unknown, so temporarily set TZ to
1677 two different values that each load a tz file. */
1678 *to
= set_time_zone_rule_tz1
;
1681 *to
= set_time_zone_rule_tz2
;
1686 /* Now TZ has the desired value, and tzset can be invoked safely. */
1693 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1694 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1695 type of object is Lisp_String). INHERIT is passed to
1696 INSERT_FROM_STRING_FUNC as the last argument. */
1699 general_insert_function (insert_func
, insert_from_string_func
,
1700 inherit
, nargs
, args
)
1701 void (*insert_func
) P_ ((unsigned char *, int));
1702 void (*insert_from_string_func
) P_ ((Lisp_Object
, int, int, int, int, int));
1704 register Lisp_Object
*args
;
1706 register int argnum
;
1707 register Lisp_Object val
;
1709 for (argnum
= 0; argnum
< nargs
; argnum
++)
1715 unsigned char workbuf
[4], *str
;
1718 if (!NILP (current_buffer
->enable_multibyte_characters
))
1719 len
= CHAR_STRING (XFASTINT (val
), workbuf
, str
);
1722 workbuf
[0] = (SINGLE_BYTE_CHAR_P (XINT (val
))
1724 : multibyte_char_to_unibyte (XINT (val
), Qnil
));
1728 (*insert_func
) (str
, len
);
1730 else if (STRINGP (val
))
1732 (*insert_from_string_func
) (val
, 0, 0,
1733 XSTRING (val
)->size
,
1734 STRING_BYTES (XSTRING (val
)),
1739 val
= wrong_type_argument (Qchar_or_string_p
, val
);
1753 /* Callers passing one argument to Finsert need not gcpro the
1754 argument "array", since the only element of the array will
1755 not be used after calling insert or insert_from_string, so
1756 we don't care if it gets trashed. */
1758 DEFUN ("insert", Finsert
, Sinsert
, 0, MANY
, 0,
1759 "Insert the arguments, either strings or characters, at point.\n\
1760 Point and before-insertion markers move forward to end up\n\
1761 after the inserted text.\n\
1762 Any other markers at the point of insertion remain before the text.\n\
1764 If the current buffer is multibyte, unibyte strings are converted\n\
1765 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1766 If the current buffer is unibyte, multibyte strings are converted\n\
1767 to unibyte for insertion.")
1770 register Lisp_Object
*args
;
1772 general_insert_function (insert
, insert_from_string
, 0, nargs
, args
);
1776 DEFUN ("insert-and-inherit", Finsert_and_inherit
, Sinsert_and_inherit
,
1778 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1779 Point and before-insertion markers move forward to end up\n\
1780 after the inserted text.\n\
1781 Any other markers at the point of insertion remain before the text.\n\
1783 If the current buffer is multibyte, unibyte strings are converted\n\
1784 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1785 If the current buffer is unibyte, multibyte strings are converted\n\
1786 to unibyte for insertion.")
1789 register Lisp_Object
*args
;
1791 general_insert_function (insert_and_inherit
, insert_from_string
, 1,
1796 DEFUN ("insert-before-markers", Finsert_before_markers
, Sinsert_before_markers
, 0, MANY
, 0,
1797 "Insert strings or characters at point, relocating markers after the text.\n\
1798 Point and markers move forward to end up after the inserted text.\n\
1800 If the current buffer is multibyte, unibyte strings are converted\n\
1801 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1802 If the current buffer is unibyte, multibyte strings are converted\n\
1803 to unibyte for insertion.")
1806 register Lisp_Object
*args
;
1808 general_insert_function (insert_before_markers
,
1809 insert_from_string_before_markers
, 0,
1814 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers
,
1815 Sinsert_and_inherit_before_markers
, 0, MANY
, 0,
1816 "Insert text at point, relocating markers and inheriting properties.\n\
1817 Point and markers move forward to end up after the inserted text.\n\
1819 If the current buffer is multibyte, unibyte strings are converted\n\
1820 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1821 If the current buffer is unibyte, multibyte strings are converted\n\
1822 to unibyte for insertion.")
1825 register Lisp_Object
*args
;
1827 general_insert_function (insert_before_markers_and_inherit
,
1828 insert_from_string_before_markers
, 1,
1833 DEFUN ("insert-char", Finsert_char
, Sinsert_char
, 2, 3, 0,
1834 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1835 Both arguments are required.\n\
1836 Point, and before-insertion markers, are relocated as in the function `insert'.\n\
1837 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1838 from adjoining text, if those properties are sticky.")
1839 (character
, count
, inherit
)
1840 Lisp_Object character
, count
, inherit
;
1842 register unsigned char *string
;
1843 register int strlen
;
1846 unsigned char workbuf
[4], *str
;
1848 CHECK_NUMBER (character
, 0);
1849 CHECK_NUMBER (count
, 1);
1851 if (!NILP (current_buffer
->enable_multibyte_characters
))
1852 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
1854 workbuf
[0] = XFASTINT (character
), str
= workbuf
, len
= 1;
1855 n
= XINT (count
) * len
;
1858 strlen
= min (n
, 256 * len
);
1859 string
= (unsigned char *) alloca (strlen
);
1860 for (i
= 0; i
< strlen
; i
++)
1861 string
[i
] = str
[i
% len
];
1865 if (!NILP (inherit
))
1866 insert_and_inherit (string
, strlen
);
1868 insert (string
, strlen
);
1873 if (!NILP (inherit
))
1874 insert_and_inherit (string
, n
);
1882 /* Making strings from buffer contents. */
1884 /* Return a Lisp_String containing the text of the current buffer from
1885 START to END. If text properties are in use and the current buffer
1886 has properties in the range specified, the resulting string will also
1887 have them, if PROPS is nonzero.
1889 We don't want to use plain old make_string here, because it calls
1890 make_uninit_string, which can cause the buffer arena to be
1891 compacted. make_string has no way of knowing that the data has
1892 been moved, and thus copies the wrong data into the string. This
1893 doesn't effect most of the other users of make_string, so it should
1894 be left as is. But we should use this function when conjuring
1895 buffer substrings. */
1898 make_buffer_string (start
, end
, props
)
1902 int start_byte
= CHAR_TO_BYTE (start
);
1903 int end_byte
= CHAR_TO_BYTE (end
);
1905 return make_buffer_string_both (start
, start_byte
, end
, end_byte
, props
);
1908 /* Return a Lisp_String containing the text of the current buffer from
1909 START / START_BYTE to END / END_BYTE.
1911 If text properties are in use and the current buffer
1912 has properties in the range specified, the resulting string will also
1913 have them, if PROPS is nonzero.
1915 We don't want to use plain old make_string here, because it calls
1916 make_uninit_string, which can cause the buffer arena to be
1917 compacted. make_string has no way of knowing that the data has
1918 been moved, and thus copies the wrong data into the string. This
1919 doesn't effect most of the other users of make_string, so it should
1920 be left as is. But we should use this function when conjuring
1921 buffer substrings. */
1924 make_buffer_string_both (start
, start_byte
, end
, end_byte
, props
)
1925 int start
, start_byte
, end
, end_byte
;
1928 Lisp_Object result
, tem
, tem1
;
1930 if (start
< GPT
&& GPT
< end
)
1933 if (! NILP (current_buffer
->enable_multibyte_characters
))
1934 result
= make_uninit_multibyte_string (end
- start
, end_byte
- start_byte
);
1936 result
= make_uninit_string (end
- start
);
1937 bcopy (BYTE_POS_ADDR (start_byte
), XSTRING (result
)->data
,
1938 end_byte
- start_byte
);
1940 /* If desired, update and copy the text properties. */
1943 update_buffer_properties (start
, end
);
1945 tem
= Fnext_property_change (make_number (start
), Qnil
, make_number (end
));
1946 tem1
= Ftext_properties_at (make_number (start
), Qnil
);
1948 if (XINT (tem
) != end
|| !NILP (tem1
))
1949 copy_intervals_to_string (result
, current_buffer
, start
,
1956 /* Call Vbuffer_access_fontify_functions for the range START ... END
1957 in the current buffer, if necessary. */
1960 update_buffer_properties (start
, end
)
1963 /* If this buffer has some access functions,
1964 call them, specifying the range of the buffer being accessed. */
1965 if (!NILP (Vbuffer_access_fontify_functions
))
1967 Lisp_Object args
[3];
1970 args
[0] = Qbuffer_access_fontify_functions
;
1971 XSETINT (args
[1], start
);
1972 XSETINT (args
[2], end
);
1974 /* But don't call them if we can tell that the work
1975 has already been done. */
1976 if (!NILP (Vbuffer_access_fontified_property
))
1978 tem
= Ftext_property_any (args
[1], args
[2],
1979 Vbuffer_access_fontified_property
,
1982 Frun_hook_with_args (3, args
);
1985 Frun_hook_with_args (3, args
);
1989 DEFUN ("buffer-substring", Fbuffer_substring
, Sbuffer_substring
, 2, 2, 0,
1990 "Return the contents of part of the current buffer as a string.\n\
1991 The two arguments START and END are character positions;\n\
1992 they can be in either order.\n\
1993 The string returned is multibyte if the buffer is multibyte.")
1995 Lisp_Object start
, end
;
1999 validate_region (&start
, &end
);
2003 return make_buffer_string (b
, e
, 1);
2006 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties
,
2007 Sbuffer_substring_no_properties
, 2, 2, 0,
2008 "Return the characters of part of the buffer, without the text properties.\n\
2009 The two arguments START and END are character positions;\n\
2010 they can be in either order.")
2012 Lisp_Object start
, end
;
2016 validate_region (&start
, &end
);
2020 return make_buffer_string (b
, e
, 0);
2023 DEFUN ("buffer-string", Fbuffer_string
, Sbuffer_string
, 0, 0, 0,
2024 "Return the contents of the current buffer as a string.\n\
2025 If narrowing is in effect, this function returns only the visible part\n\
2026 of the buffer. If in a mini-buffer, don't include the prompt in the\n\
2030 return make_buffer_string (BEGV
, ZV
, 1);
2033 DEFUN ("insert-buffer-substring", Finsert_buffer_substring
, Sinsert_buffer_substring
,
2035 "Insert before point a substring of the contents of buffer BUFFER.\n\
2036 BUFFER may be a buffer or a buffer name.\n\
2037 Arguments START and END are character numbers specifying the substring.\n\
2038 They default to the beginning and the end of BUFFER.")
2040 Lisp_Object buf
, start
, end
;
2042 register int b
, e
, temp
;
2043 register struct buffer
*bp
, *obuf
;
2046 buffer
= Fget_buffer (buf
);
2049 bp
= XBUFFER (buffer
);
2050 if (NILP (bp
->name
))
2051 error ("Selecting deleted buffer");
2057 CHECK_NUMBER_COERCE_MARKER (start
, 0);
2064 CHECK_NUMBER_COERCE_MARKER (end
, 1);
2069 temp
= b
, b
= e
, e
= temp
;
2071 if (!(BUF_BEGV (bp
) <= b
&& e
<= BUF_ZV (bp
)))
2072 args_out_of_range (start
, end
);
2074 obuf
= current_buffer
;
2075 set_buffer_internal_1 (bp
);
2076 update_buffer_properties (b
, e
);
2077 set_buffer_internal_1 (obuf
);
2079 insert_from_buffer (bp
, b
, e
- b
, 0);
2083 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings
, Scompare_buffer_substrings
,
2085 "Compare two substrings of two buffers; return result as number.\n\
2086 the value is -N if first string is less after N-1 chars,\n\
2087 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
2088 Each substring is represented as three arguments: BUFFER, START and END.\n\
2089 That makes six args in all, three for each substring.\n\n\
2090 The value of `case-fold-search' in the current buffer\n\
2091 determines whether case is significant or ignored.")
2092 (buffer1
, start1
, end1
, buffer2
, start2
, end2
)
2093 Lisp_Object buffer1
, start1
, end1
, buffer2
, start2
, end2
;
2095 register int begp1
, endp1
, begp2
, endp2
, temp
;
2096 register struct buffer
*bp1
, *bp2
;
2097 register Lisp_Object
*trt
2098 = (!NILP (current_buffer
->case_fold_search
)
2099 ? XCHAR_TABLE (current_buffer
->case_canon_table
)->contents
: 0);
2101 int i1
, i2
, i1_byte
, i2_byte
;
2103 /* Find the first buffer and its substring. */
2106 bp1
= current_buffer
;
2110 buf1
= Fget_buffer (buffer1
);
2113 bp1
= XBUFFER (buf1
);
2114 if (NILP (bp1
->name
))
2115 error ("Selecting deleted buffer");
2119 begp1
= BUF_BEGV (bp1
);
2122 CHECK_NUMBER_COERCE_MARKER (start1
, 1);
2123 begp1
= XINT (start1
);
2126 endp1
= BUF_ZV (bp1
);
2129 CHECK_NUMBER_COERCE_MARKER (end1
, 2);
2130 endp1
= XINT (end1
);
2134 temp
= begp1
, begp1
= endp1
, endp1
= temp
;
2136 if (!(BUF_BEGV (bp1
) <= begp1
2138 && endp1
<= BUF_ZV (bp1
)))
2139 args_out_of_range (start1
, end1
);
2141 /* Likewise for second substring. */
2144 bp2
= current_buffer
;
2148 buf2
= Fget_buffer (buffer2
);
2151 bp2
= XBUFFER (buf2
);
2152 if (NILP (bp2
->name
))
2153 error ("Selecting deleted buffer");
2157 begp2
= BUF_BEGV (bp2
);
2160 CHECK_NUMBER_COERCE_MARKER (start2
, 4);
2161 begp2
= XINT (start2
);
2164 endp2
= BUF_ZV (bp2
);
2167 CHECK_NUMBER_COERCE_MARKER (end2
, 5);
2168 endp2
= XINT (end2
);
2172 temp
= begp2
, begp2
= endp2
, endp2
= temp
;
2174 if (!(BUF_BEGV (bp2
) <= begp2
2176 && endp2
<= BUF_ZV (bp2
)))
2177 args_out_of_range (start2
, end2
);
2181 i1_byte
= buf_charpos_to_bytepos (bp1
, i1
);
2182 i2_byte
= buf_charpos_to_bytepos (bp2
, i2
);
2184 while (i1
< endp1
&& i2
< endp2
)
2186 /* When we find a mismatch, we must compare the
2187 characters, not just the bytes. */
2190 if (! NILP (bp1
->enable_multibyte_characters
))
2192 c1
= BUF_FETCH_MULTIBYTE_CHAR (bp1
, i1_byte
);
2193 BUF_INC_POS (bp1
, i1_byte
);
2198 c1
= BUF_FETCH_BYTE (bp1
, i1
);
2199 c1
= unibyte_char_to_multibyte (c1
);
2203 if (! NILP (bp2
->enable_multibyte_characters
))
2205 c2
= BUF_FETCH_MULTIBYTE_CHAR (bp2
, i2_byte
);
2206 BUF_INC_POS (bp2
, i2_byte
);
2211 c2
= BUF_FETCH_BYTE (bp2
, i2
);
2212 c2
= unibyte_char_to_multibyte (c2
);
2218 c1
= XINT (trt
[c1
]);
2219 c2
= XINT (trt
[c2
]);
2222 return make_number (- 1 - chars
);
2224 return make_number (chars
+ 1);
2229 /* The strings match as far as they go.
2230 If one is shorter, that one is less. */
2231 if (chars
< endp1
- begp1
)
2232 return make_number (chars
+ 1);
2233 else if (chars
< endp2
- begp2
)
2234 return make_number (- chars
- 1);
2236 /* Same length too => they are equal. */
2237 return make_number (0);
2241 subst_char_in_region_unwind (arg
)
2244 return current_buffer
->undo_list
= arg
;
2248 subst_char_in_region_unwind_1 (arg
)
2251 return current_buffer
->filename
= arg
;
2254 DEFUN ("subst-char-in-region", Fsubst_char_in_region
,
2255 Ssubst_char_in_region
, 4, 5, 0,
2256 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
2257 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
2258 and don't mark the buffer as really changed.\n\
2259 Both characters must have the same length of multi-byte form.")
2260 (start
, end
, fromchar
, tochar
, noundo
)
2261 Lisp_Object start
, end
, fromchar
, tochar
, noundo
;
2263 register int pos
, pos_byte
, stop
, i
, len
, end_byte
;
2265 unsigned char fromwork
[4], *fromstr
, towork
[4], *tostr
, *p
;
2266 int count
= specpdl_ptr
- specpdl
;
2267 #define COMBINING_NO 0
2268 #define COMBINING_BEFORE 1
2269 #define COMBINING_AFTER 2
2270 #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
2271 int maybe_byte_combining
= COMBINING_NO
;
2273 validate_region (&start
, &end
);
2274 CHECK_NUMBER (fromchar
, 2);
2275 CHECK_NUMBER (tochar
, 3);
2277 if (! NILP (current_buffer
->enable_multibyte_characters
))
2279 len
= CHAR_STRING (XFASTINT (fromchar
), fromwork
, fromstr
);
2280 if (CHAR_STRING (XFASTINT (tochar
), towork
, tostr
) != len
)
2281 error ("Characters in subst-char-in-region have different byte-lengths");
2282 if (!ASCII_BYTE_P (*tostr
))
2284 /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a
2285 complete multibyte character, it may be combined with the
2286 after bytes. If it is in the range 0xA0..0xFF, it may be
2287 combined with the before and after bytes. */
2288 if (!CHAR_HEAD_P (*tostr
))
2289 maybe_byte_combining
= COMBINING_BOTH
;
2290 else if (BYTES_BY_CHAR_HEAD (*tostr
) > len
)
2291 maybe_byte_combining
= COMBINING_AFTER
;
2297 fromwork
[0] = XFASTINT (fromchar
), fromstr
= fromwork
;
2298 towork
[0] = XFASTINT (tochar
), tostr
= towork
;
2302 pos_byte
= CHAR_TO_BYTE (pos
);
2303 stop
= CHAR_TO_BYTE (XINT (end
));
2306 /* If we don't want undo, turn off putting stuff on the list.
2307 That's faster than getting rid of things,
2308 and it prevents even the entry for a first change.
2309 Also inhibit locking the file. */
2312 record_unwind_protect (subst_char_in_region_unwind
,
2313 current_buffer
->undo_list
);
2314 current_buffer
->undo_list
= Qt
;
2315 /* Don't do file-locking. */
2316 record_unwind_protect (subst_char_in_region_unwind_1
,
2317 current_buffer
->filename
);
2318 current_buffer
->filename
= Qnil
;
2321 if (pos_byte
< GPT_BYTE
)
2322 stop
= min (stop
, GPT_BYTE
);
2325 int pos_byte_next
= pos_byte
;
2327 if (pos_byte
>= stop
)
2329 if (pos_byte
>= end_byte
) break;
2332 p
= BYTE_POS_ADDR (pos_byte
);
2333 INC_POS (pos_byte_next
);
2334 if (pos_byte_next
- pos_byte
== len
2335 && p
[0] == fromstr
[0]
2337 || (p
[1] == fromstr
[1]
2338 && (len
== 2 || (p
[2] == fromstr
[2]
2339 && (len
== 3 || p
[3] == fromstr
[3]))))))
2343 modify_region (current_buffer
, XINT (start
), XINT (end
));
2345 if (! NILP (noundo
))
2347 if (MODIFF
- 1 == SAVE_MODIFF
)
2349 if (MODIFF
- 1 == current_buffer
->auto_save_modified
)
2350 current_buffer
->auto_save_modified
++;
2356 /* Take care of the case where the new character
2357 combines with neighboring bytes. */
2358 if (maybe_byte_combining
2359 && (maybe_byte_combining
== COMBINING_AFTER
2360 ? (pos_byte_next
< Z_BYTE
2361 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next
)))
2362 : ((pos_byte_next
< Z_BYTE
2363 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next
)))
2364 || (pos_byte
> BEG_BYTE
2365 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte
- 1))))))
2367 Lisp_Object tem
, string
;
2369 struct gcpro gcpro1
;
2371 tem
= current_buffer
->undo_list
;
2374 /* Make a multibyte string containing this single character. */
2375 string
= make_multibyte_string (tostr
, 1, len
);
2376 /* replace_range is less efficient, because it moves the gap,
2377 but it handles combining correctly. */
2378 replace_range (pos
, pos
+ 1, string
,
2380 pos_byte_next
= CHAR_TO_BYTE (pos
);
2381 if (pos_byte_next
> pos_byte
)
2382 /* Before combining happened. We should not increment
2383 POS. So, to cancel the later increment of POS,
2387 INC_POS (pos_byte_next
);
2389 if (! NILP (noundo
))
2390 current_buffer
->undo_list
= tem
;
2397 record_change (pos
, 1);
2398 for (i
= 0; i
< len
; i
++) *p
++ = tostr
[i
];
2401 pos_byte
= pos_byte_next
;
2406 signal_after_change (XINT (start
),
2407 XINT (end
) - XINT (start
), XINT (end
) - XINT (start
));
2409 unbind_to (count
, Qnil
);
2413 DEFUN ("translate-region", Ftranslate_region
, Stranslate_region
, 3, 3, 0,
2414 "From START to END, translate characters according to TABLE.\n\
2415 TABLE is a string; the Nth character in it is the mapping\n\
2416 for the character with code N.\n\
2417 This function does not alter multibyte characters.\n\
2418 It returns the number of characters changed.")
2422 register Lisp_Object table
;
2424 register int pos_byte
, stop
; /* Limits of the region. */
2425 register unsigned char *tt
; /* Trans table. */
2426 register int nc
; /* New character. */
2427 int cnt
; /* Number of changes made. */
2428 int size
; /* Size of translate table. */
2430 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
2432 validate_region (&start
, &end
);
2433 CHECK_STRING (table
, 2);
2435 size
= STRING_BYTES (XSTRING (table
));
2436 tt
= XSTRING (table
)->data
;
2438 pos_byte
= CHAR_TO_BYTE (XINT (start
));
2439 stop
= CHAR_TO_BYTE (XINT (end
));
2440 modify_region (current_buffer
, XINT (start
), XINT (end
));
2444 for (; pos_byte
< stop
; )
2446 register unsigned char *p
= BYTE_POS_ADDR (pos_byte
);
2452 oc
= STRING_CHAR_AND_LENGTH (p
, stop
- pos_byte
, len
);
2455 pos_byte_next
= pos_byte
+ len
;
2456 if (oc
< size
&& len
== 1)
2461 /* Take care of the case where the new character
2462 combines with neighboring bytes. */
2463 if (!ASCII_BYTE_P (nc
)
2464 && (CHAR_HEAD_P (nc
)
2465 ? ! CHAR_HEAD_P (FETCH_BYTE (pos_byte
+ 1))
2466 : (pos_byte
> BEG_BYTE
2467 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte
- 1)))))
2471 string
= make_multibyte_string (tt
+ oc
, 1, 1);
2472 /* This is less efficient, because it moves the gap,
2473 but it handles combining correctly. */
2474 replace_range (pos
, pos
+ 1, string
,
2476 pos_byte_next
= CHAR_TO_BYTE (pos
);
2477 if (pos_byte_next
> pos_byte
)
2478 /* Before combining happened. We should not
2479 increment POS. So, to cancel the later
2480 increment of POS, we decrease it now. */
2483 INC_POS (pos_byte_next
);
2487 record_change (pos
, 1);
2489 signal_after_change (pos
, 1, 1);
2494 pos_byte
= pos_byte_next
;
2498 return make_number (cnt
);
2501 DEFUN ("delete-region", Fdelete_region
, Sdelete_region
, 2, 2, "r",
2502 "Delete the text between point and mark.\n\
2503 When called from a program, expects two arguments,\n\
2504 positions (integers or markers) specifying the stretch to be deleted.")
2506 Lisp_Object start
, end
;
2508 validate_region (&start
, &end
);
2509 del_range (XINT (start
), XINT (end
));
2513 DEFUN ("widen", Fwiden
, Swiden
, 0, 0, "",
2514 "Remove restrictions (narrowing) from current buffer.\n\
2515 This allows the buffer's full text to be seen and edited.")
2518 if (BEG
!= BEGV
|| Z
!= ZV
)
2519 current_buffer
->clip_changed
= 1;
2521 BEGV_BYTE
= BEG_BYTE
;
2522 SET_BUF_ZV_BOTH (current_buffer
, Z
, Z_BYTE
);
2523 /* Changing the buffer bounds invalidates any recorded current column. */
2524 invalidate_current_column ();
2528 DEFUN ("narrow-to-region", Fnarrow_to_region
, Snarrow_to_region
, 2, 2, "r",
2529 "Restrict editing in this buffer to the current region.\n\
2530 The rest of the text becomes temporarily invisible and untouchable\n\
2531 but is not deleted; if you save the buffer in a file, the invisible\n\
2532 text is included in the file. \\[widen] makes all visible again.\n\
2533 See also `save-restriction'.\n\
2535 When calling from a program, pass two arguments; positions (integers\n\
2536 or markers) bounding the text that should remain visible.")
2538 register Lisp_Object start
, end
;
2540 CHECK_NUMBER_COERCE_MARKER (start
, 0);
2541 CHECK_NUMBER_COERCE_MARKER (end
, 1);
2543 if (XINT (start
) > XINT (end
))
2546 tem
= start
; start
= end
; end
= tem
;
2549 if (!(BEG
<= XINT (start
) && XINT (start
) <= XINT (end
) && XINT (end
) <= Z
))
2550 args_out_of_range (start
, end
);
2552 if (BEGV
!= XFASTINT (start
) || ZV
!= XFASTINT (end
))
2553 current_buffer
->clip_changed
= 1;
2555 SET_BUF_BEGV (current_buffer
, XFASTINT (start
));
2556 SET_BUF_ZV (current_buffer
, XFASTINT (end
));
2557 if (PT
< XFASTINT (start
))
2558 SET_PT (XFASTINT (start
));
2559 if (PT
> XFASTINT (end
))
2560 SET_PT (XFASTINT (end
));
2561 /* Changing the buffer bounds invalidates any recorded current column. */
2562 invalidate_current_column ();
2567 save_restriction_save ()
2569 register Lisp_Object bottom
, top
;
2570 /* Note: I tried using markers here, but it does not win
2571 because insertion at the end of the saved region
2572 does not advance mh and is considered "outside" the saved region. */
2573 XSETFASTINT (bottom
, BEGV
- BEG
);
2574 XSETFASTINT (top
, Z
- ZV
);
2576 return Fcons (Fcurrent_buffer (), Fcons (bottom
, top
));
2580 save_restriction_restore (data
)
2583 register struct buffer
*buf
;
2584 register int newhead
, newtail
;
2585 register Lisp_Object tem
;
2588 buf
= XBUFFER (XCAR (data
));
2593 newhead
= XINT (tem
);
2595 newtail
= XINT (tem
);
2596 if (newhead
+ newtail
> BUF_Z (buf
) - BUF_BEG (buf
))
2602 obegv
= BUF_BEGV (buf
);
2605 SET_BUF_BEGV (buf
, BUF_BEG (buf
) + newhead
);
2606 SET_BUF_ZV (buf
, BUF_Z (buf
) - newtail
);
2608 if (obegv
!= BUF_BEGV (buf
) || ozv
!= BUF_ZV (buf
))
2609 current_buffer
->clip_changed
= 1;
2611 /* If point is outside the new visible range, move it inside. */
2612 SET_BUF_PT_BOTH (buf
,
2613 clip_to_bounds (BUF_BEGV (buf
), BUF_PT (buf
), BUF_ZV (buf
)),
2614 clip_to_bounds (BUF_BEGV_BYTE (buf
), BUF_PT_BYTE (buf
),
2615 BUF_ZV_BYTE (buf
)));
2620 DEFUN ("save-restriction", Fsave_restriction
, Ssave_restriction
, 0, UNEVALLED
, 0,
2621 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2622 The buffer's restrictions make parts of the beginning and end invisible.\n\
2623 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2624 This special form, `save-restriction', saves the current buffer's restrictions\n\
2625 when it is entered, and restores them when it is exited.\n\
2626 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2627 The old restrictions settings are restored\n\
2628 even in case of abnormal exit (throw or error).\n\
2630 The value returned is the value of the last form in BODY.\n\
2632 `save-restriction' can get confused if, within the BODY, you widen\n\
2633 and then make changes outside the area within the saved restrictions.\n\
2634 See Info node `(elisp)Narrowing' for details and an appropriate technique.\n\
2636 Note: if you are using both `save-excursion' and `save-restriction',\n\
2637 use `save-excursion' outermost:\n\
2638 (save-excursion (save-restriction ...))")
2642 register Lisp_Object val
;
2643 int count
= specpdl_ptr
- specpdl
;
2645 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
2646 val
= Fprogn (body
);
2647 return unbind_to (count
, val
);
2652 /* Buffer for the most recent text displayed by Fmessage. */
2653 static char *message_text
;
2655 /* Allocated length of that buffer. */
2656 static int message_length
;
2658 #endif /* not HAVE_MENUS */
2660 DEFUN ("message", Fmessage
, Smessage
, 1, MANY
, 0,
2661 "Print a one-line message at the bottom of the screen.\n\
2662 The first argument is a format control string, and the rest are data\n\
2663 to be formatted under control of the string. See `format' for details.\n\
2665 If the first argument is nil, clear any existing message; let the\n\
2666 minibuffer contents show.")
2678 register Lisp_Object val
;
2679 val
= Fformat (nargs
, args
);
2680 message3 (val
, STRING_BYTES (XSTRING (val
)), STRING_MULTIBYTE (val
));
2685 DEFUN ("message-box", Fmessage_box
, Smessage_box
, 1, MANY
, 0,
2686 "Display a message, in a dialog box if possible.\n\
2687 If a dialog box is not available, use the echo area.\n\
2688 The first argument is a format control string, and the rest are data\n\
2689 to be formatted under control of the string. See `format' for details.\n\
2691 If the first argument is nil, clear any existing message; let the\n\
2692 minibuffer contents show.")
2704 register Lisp_Object val
;
2705 val
= Fformat (nargs
, args
);
2708 Lisp_Object pane
, menu
, obj
;
2709 struct gcpro gcpro1
;
2710 pane
= Fcons (Fcons (build_string ("OK"), Qt
), Qnil
);
2712 menu
= Fcons (val
, pane
);
2713 obj
= Fx_popup_dialog (Qt
, menu
);
2717 #else /* not HAVE_MENUS */
2718 /* Copy the data so that it won't move when we GC. */
2721 message_text
= (char *)xmalloc (80);
2722 message_length
= 80;
2724 if (STRING_BYTES (XSTRING (val
)) > message_length
)
2726 message_length
= STRING_BYTES (XSTRING (val
));
2727 message_text
= (char *)xrealloc (message_text
, message_length
);
2729 bcopy (XSTRING (val
)->data
, message_text
, STRING_BYTES (XSTRING (val
)));
2730 message2 (message_text
, STRING_BYTES (XSTRING (val
)),
2731 STRING_MULTIBYTE (val
));
2733 #endif /* not HAVE_MENUS */
2737 extern Lisp_Object last_nonmenu_event
;
2740 DEFUN ("message-or-box", Fmessage_or_box
, Smessage_or_box
, 1, MANY
, 0,
2741 "Display a message in a dialog box or in the echo area.\n\
2742 If this command was invoked with the mouse, use a dialog box.\n\
2743 Otherwise, use the echo area.\n\
2744 The first argument is a format control string, and the rest are data\n\
2745 to be formatted under control of the string. See `format' for details.\n\
2747 If the first argument is nil, clear any existing message; let the\n\
2748 minibuffer contents show.")
2754 if ((NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2755 && NILP (use_dialog_box
))
2756 return Fmessage_box (nargs
, args
);
2758 return Fmessage (nargs
, args
);
2761 DEFUN ("current-message", Fcurrent_message
, Scurrent_message
, 0, 0, 0,
2762 "Return the string currently displayed in the echo area, or nil if none.")
2765 return current_message ();
2769 DEFUN ("propertize", Fpropertize
, Spropertize
, 3, MANY
, 0,
2770 "Return a copy of STRING with text properties added.\n\
2771 First argument is the string to copy.\n\
2772 Remaining arguments are sequences of PROPERTY VALUE pairs for text\n\
2773 properties to add to the result ")
2778 Lisp_Object properties
, string
;
2779 struct gcpro gcpro1
, gcpro2
;
2782 /* Number of args must be odd. */
2783 if ((nargs
& 1) == 0 || nargs
< 3)
2784 error ("Wrong number of arguments");
2786 properties
= string
= Qnil
;
2787 GCPRO2 (properties
, string
);
2789 /* First argument must be a string. */
2790 CHECK_STRING (args
[0], 0);
2791 string
= Fcopy_sequence (args
[0]);
2793 for (i
= 1; i
< nargs
; i
+= 2)
2795 CHECK_SYMBOL (args
[i
], i
);
2796 properties
= Fcons (args
[i
], Fcons (args
[i
+ 1], properties
));
2799 Fadd_text_properties (make_number (0),
2800 make_number (XSTRING (string
)->size
),
2801 properties
, string
);
2802 RETURN_UNGCPRO (string
);
2806 /* Number of bytes that STRING will occupy when put into the result.
2807 MULTIBYTE is nonzero if the result should be multibyte. */
2809 #define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \
2810 (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \
2811 ? count_size_as_multibyte (XSTRING (STRING)->data, \
2812 STRING_BYTES (XSTRING (STRING))) \
2813 : STRING_BYTES (XSTRING (STRING)))
2815 DEFUN ("format", Fformat
, Sformat
, 1, MANY
, 0,
2816 "Format a string out of a control-string and arguments.\n\
2817 The first argument is a control string.\n\
2818 The other arguments are substituted into it to make the result, a string.\n\
2819 It may contain %-sequences meaning to substitute the next argument.\n\
2820 %s means print a string argument. Actually, prints any object, with `princ'.\n\
2821 %d means print as number in decimal (%o octal, %x hex).\n\
2822 %e means print a number in exponential notation.\n\
2823 %f means print a number in decimal-point notation.\n\
2824 %g means print a number in exponential notation\n\
2825 or decimal-point notation, whichever uses fewer characters.\n\
2826 %c means print a number as a single character.\n\
2827 %S means print any object as an s-expression (using `prin1').\n\
2828 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2829 Use %% to put a single % into the output.")
2832 register Lisp_Object
*args
;
2834 register int n
; /* The number of the next arg to substitute */
2835 register int total
; /* An estimate of the final length */
2837 register unsigned char *format
, *end
;
2839 /* Nonzero if the output should be a multibyte string,
2840 which is true if any of the inputs is one. */
2842 /* When we make a multibyte string, we must pay attention to the
2843 byte combining problem, i.e., a byte may be combined with a
2844 multibyte charcter of the previous string. This flag tells if we
2845 must consider such a situation or not. */
2846 int maybe_combine_byte
;
2847 unsigned char *this_format
;
2855 extern char *index ();
2857 /* It should not be necessary to GCPRO ARGS, because
2858 the caller in the interpreter should take care of that. */
2860 /* Try to determine whether the result should be multibyte.
2861 This is not always right; sometimes the result needs to be multibyte
2862 because of an object that we will pass through prin1,
2863 and in that case, we won't know it here. */
2864 for (n
= 0; n
< nargs
; n
++)
2865 if (STRINGP (args
[n
]) && STRING_MULTIBYTE (args
[n
]))
2868 CHECK_STRING (args
[0], 0);
2870 /* If we start out planning a unibyte result,
2871 and later find it has to be multibyte, we jump back to retry. */
2874 format
= XSTRING (args
[0])->data
;
2875 end
= format
+ STRING_BYTES (XSTRING (args
[0]));
2878 /* Make room in result for all the non-%-codes in the control string. */
2879 total
= 5 + CONVERTED_BYTE_SIZE (multibyte
, args
[0]);
2881 /* Add to TOTAL enough space to hold the converted arguments. */
2884 while (format
!= end
)
2885 if (*format
++ == '%')
2887 int minlen
, thissize
= 0;
2888 unsigned char *this_format_start
= format
- 1;
2890 /* Process a numeric arg and skip it. */
2891 minlen
= atoi (format
);
2895 while ((*format
>= '0' && *format
<= '9')
2896 || *format
== '-' || *format
== ' ' || *format
== '.')
2899 if (format
- this_format_start
+ 1 > longest_format
)
2900 longest_format
= format
- this_format_start
+ 1;
2903 error ("Format string ends in middle of format specifier");
2906 else if (++n
>= nargs
)
2907 error ("Not enough arguments for format string");
2908 else if (*format
== 'S')
2910 /* For `S', prin1 the argument and then treat like a string. */
2911 register Lisp_Object tem
;
2912 tem
= Fprin1_to_string (args
[n
], Qnil
);
2913 if (STRING_MULTIBYTE (tem
) && ! multibyte
)
2921 else if (SYMBOLP (args
[n
]))
2923 XSETSTRING (args
[n
], XSYMBOL (args
[n
])->name
);
2924 if (STRING_MULTIBYTE (args
[n
]) && ! multibyte
)
2931 else if (STRINGP (args
[n
]))
2934 if (*format
!= 's' && *format
!= 'S')
2935 error ("Format specifier doesn't match argument type");
2936 thissize
= CONVERTED_BYTE_SIZE (multibyte
, args
[n
]);
2938 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
2939 else if (INTEGERP (args
[n
]) && *format
!= 's')
2941 #ifdef LISP_FLOAT_TYPE
2942 /* The following loop assumes the Lisp type indicates
2943 the proper way to pass the argument.
2944 So make sure we have a flonum if the argument should
2946 if (*format
== 'e' || *format
== 'f' || *format
== 'g')
2947 args
[n
] = Ffloat (args
[n
]);
2950 if (*format
!= 'd' && *format
!= 'o' && *format
!= 'x'
2951 && *format
!= 'i' && *format
!= 'X' && *format
!= 'c')
2952 error ("Invalid format operation %%%c", *format
);
2956 && (! SINGLE_BYTE_CHAR_P (XINT (args
[n
]))
2957 || XINT (args
[n
]) == 0))
2964 args
[n
] = Fchar_to_string (args
[n
]);
2965 thissize
= STRING_BYTES (XSTRING (args
[n
]));
2968 #ifdef LISP_FLOAT_TYPE
2969 else if (FLOATP (args
[n
]) && *format
!= 's')
2971 if (! (*format
== 'e' || *format
== 'f' || *format
== 'g'))
2972 args
[n
] = Ftruncate (args
[n
], Qnil
);
2978 /* Anything but a string, convert to a string using princ. */
2979 register Lisp_Object tem
;
2980 tem
= Fprin1_to_string (args
[n
], Qt
);
2981 if (STRING_MULTIBYTE (tem
) & ! multibyte
)
2990 if (thissize
< minlen
)
2993 total
+= thissize
+ 4;
2996 /* Now we can no longer jump to retry.
2997 TOTAL and LONGEST_FORMAT are known for certain. */
2999 this_format
= (unsigned char *) alloca (longest_format
+ 1);
3001 /* Allocate the space for the result.
3002 Note that TOTAL is an overestimate. */
3004 buf
= (char *) alloca (total
+ 1);
3006 buf
= (char *) xmalloc (total
+ 1);
3012 /* Scan the format and store result in BUF. */
3013 format
= XSTRING (args
[0])->data
;
3014 maybe_combine_byte
= 0;
3015 while (format
!= end
)
3021 unsigned char *this_format_start
= format
;
3025 /* Process a numeric arg and skip it. */
3026 minlen
= atoi (format
);
3028 minlen
= - minlen
, negative
= 1;
3030 while ((*format
>= '0' && *format
<= '9')
3031 || *format
== '-' || *format
== ' ' || *format
== '.')
3034 if (*format
++ == '%')
3043 if (STRINGP (args
[n
]))
3045 int padding
, nbytes
;
3046 int width
= strwidth (XSTRING (args
[n
])->data
,
3047 STRING_BYTES (XSTRING (args
[n
])));
3050 /* If spec requires it, pad on right with spaces. */
3051 padding
= minlen
- width
;
3053 while (padding
-- > 0)
3061 && !ASCII_BYTE_P (*((unsigned char *) p
- 1))
3062 && STRING_MULTIBYTE (args
[n
])
3063 && !CHAR_HEAD_P (XSTRING (args
[n
])->data
[0]))
3064 maybe_combine_byte
= 1;
3065 nbytes
= copy_text (XSTRING (args
[n
])->data
, p
,
3066 STRING_BYTES (XSTRING (args
[n
])),
3067 STRING_MULTIBYTE (args
[n
]), multibyte
);
3069 nchars
+= XSTRING (args
[n
])->size
;
3072 while (padding
-- > 0)
3078 /* If this argument has text properties, record where
3079 in the result string it appears. */
3080 if (XSTRING (args
[n
])->intervals
)
3084 int nbytes
= nargs
* sizeof *info
;
3085 info
= (struct info
*) alloca (nbytes
);
3086 bzero (info
, nbytes
);
3089 info
[n
].start
= start
;
3090 info
[n
].end
= nchars
;
3093 else if (INTEGERP (args
[n
]) || FLOATP (args
[n
]))
3097 bcopy (this_format_start
, this_format
,
3098 format
- this_format_start
);
3099 this_format
[format
- this_format_start
] = 0;
3101 if (INTEGERP (args
[n
]))
3102 sprintf (p
, this_format
, XINT (args
[n
]));
3104 sprintf (p
, this_format
, XFLOAT_DATA (args
[n
]));
3108 && !ASCII_BYTE_P (*((unsigned char *) p
- 1))
3109 && !CHAR_HEAD_P (*((unsigned char *) p
)))
3110 maybe_combine_byte
= 1;
3111 this_nchars
= strlen (p
);
3113 nchars
+= this_nchars
;
3116 else if (STRING_MULTIBYTE (args
[0]))
3118 /* Copy a whole multibyte character. */
3121 && !ASCII_BYTE_P (*((unsigned char *) p
- 1))
3122 && !CHAR_HEAD_P (*format
))
3123 maybe_combine_byte
= 1;
3125 while (! CHAR_HEAD_P (*format
)) *p
++ = *format
++;
3130 /* Convert a single-byte character to multibyte. */
3131 int len
= copy_text (format
, p
, 1, 0, 1);
3138 *p
++ = *format
++, nchars
++;
3141 if (maybe_combine_byte
)
3142 nchars
= multibyte_chars_in_text (buf
, p
- buf
);
3143 val
= make_specified_string (buf
, nchars
, p
- buf
, multibyte
);
3145 /* If we allocated BUF with malloc, free it too. */
3149 /* If the format string has text properties, or any of the string
3150 arguments has text properties, set up text properties of the
3153 if (XSTRING (args
[0])->intervals
|| info
)
3155 Lisp_Object len
, new_len
, props
;
3156 struct gcpro gcpro1
;
3158 /* Add text properties from the format string. */
3159 len
= make_number (XSTRING (args
[0])->size
);
3160 props
= text_property_list (args
[0], make_number (0), len
, Qnil
);
3165 new_len
= make_number (XSTRING (val
)->size
);
3166 extend_property_ranges (props
, len
, new_len
);
3167 add_text_properties_from_list (val
, props
, make_number (0));
3170 /* Add text properties from arguments. */
3172 for (n
= 1; n
< nargs
; ++n
)
3175 len
= make_number (XSTRING (args
[n
])->size
);
3176 new_len
= make_number (info
[n
].end
- info
[n
].start
);
3177 props
= text_property_list (args
[n
], make_number (0), len
, Qnil
);
3178 extend_property_ranges (props
, len
, new_len
);
3179 add_text_properties_from_list (val
, props
,
3180 make_number (info
[n
].start
));
3193 format1 (string1
, arg0
, arg1
, arg2
, arg3
, arg4
)
3194 EMACS_INT arg0
, arg1
, arg2
, arg3
, arg4
;
3208 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, (char **) args
);
3210 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, &string1
+ 1);
3212 return build_string (buf
);
3215 DEFUN ("char-equal", Fchar_equal
, Schar_equal
, 2, 2, 0,
3216 "Return t if two characters match, optionally ignoring case.\n\
3217 Both arguments must be characters (i.e. integers).\n\
3218 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
3220 register Lisp_Object c1
, c2
;
3223 CHECK_NUMBER (c1
, 0);
3224 CHECK_NUMBER (c2
, 1);
3226 if (XINT (c1
) == XINT (c2
))
3228 if (NILP (current_buffer
->case_fold_search
))
3231 /* Do these in separate statements,
3232 then compare the variables.
3233 because of the way DOWNCASE uses temp variables. */
3234 i1
= DOWNCASE (XFASTINT (c1
));
3235 i2
= DOWNCASE (XFASTINT (c2
));
3236 return (i1
== i2
? Qt
: Qnil
);
3239 /* Transpose the markers in two regions of the current buffer, and
3240 adjust the ones between them if necessary (i.e.: if the regions
3243 START1, END1 are the character positions of the first region.
3244 START1_BYTE, END1_BYTE are the byte positions.
3245 START2, END2 are the character positions of the second region.
3246 START2_BYTE, END2_BYTE are the byte positions.
3248 Traverses the entire marker list of the buffer to do so, adding an
3249 appropriate amount to some, subtracting from some, and leaving the
3250 rest untouched. Most of this is copied from adjust_markers in insdel.c.
3252 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
3255 transpose_markers (start1
, end1
, start2
, end2
,
3256 start1_byte
, end1_byte
, start2_byte
, end2_byte
)
3257 register int start1
, end1
, start2
, end2
;
3258 register int start1_byte
, end1_byte
, start2_byte
, end2_byte
;
3260 register int amt1
, amt1_byte
, amt2
, amt2_byte
, diff
, diff_byte
, mpos
;
3261 register Lisp_Object marker
;
3263 /* Update point as if it were a marker. */
3267 TEMP_SET_PT_BOTH (PT
+ (end2
- end1
),
3268 PT_BYTE
+ (end2_byte
- end1_byte
));
3269 else if (PT
< start2
)
3270 TEMP_SET_PT_BOTH (PT
+ (end2
- start2
) - (end1
- start1
),
3271 (PT_BYTE
+ (end2_byte
- start2_byte
)
3272 - (end1_byte
- start1_byte
)));
3274 TEMP_SET_PT_BOTH (PT
- (start2
- start1
),
3275 PT_BYTE
- (start2_byte
- start1_byte
));
3277 /* We used to adjust the endpoints here to account for the gap, but that
3278 isn't good enough. Even if we assume the caller has tried to move the
3279 gap out of our way, it might still be at start1 exactly, for example;
3280 and that places it `inside' the interval, for our purposes. The amount
3281 of adjustment is nontrivial if there's a `denormalized' marker whose
3282 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
3283 the dirty work to Fmarker_position, below. */
3285 /* The difference between the region's lengths */
3286 diff
= (end2
- start2
) - (end1
- start1
);
3287 diff_byte
= (end2_byte
- start2_byte
) - (end1_byte
- start1_byte
);
3289 /* For shifting each marker in a region by the length of the other
3290 region plus the distance between the regions. */
3291 amt1
= (end2
- start2
) + (start2
- end1
);
3292 amt2
= (end1
- start1
) + (start2
- end1
);
3293 amt1_byte
= (end2_byte
- start2_byte
) + (start2_byte
- end1_byte
);
3294 amt2_byte
= (end1_byte
- start1_byte
) + (start2_byte
- end1_byte
);
3296 for (marker
= BUF_MARKERS (current_buffer
); !NILP (marker
);
3297 marker
= XMARKER (marker
)->chain
)
3299 mpos
= marker_byte_position (marker
);
3300 if (mpos
>= start1_byte
&& mpos
< end2_byte
)
3302 if (mpos
< end1_byte
)
3304 else if (mpos
< start2_byte
)
3308 XMARKER (marker
)->bytepos
= mpos
;
3310 mpos
= XMARKER (marker
)->charpos
;
3311 if (mpos
>= start1
&& mpos
< end2
)
3315 else if (mpos
< start2
)
3320 XMARKER (marker
)->charpos
= mpos
;
3324 DEFUN ("transpose-regions", Ftranspose_regions
, Stranspose_regions
, 4, 5, 0,
3325 "Transpose region START1 to END1 with START2 to END2.\n\
3326 The regions may not be overlapping, because the size of the buffer is\n\
3327 never changed in a transposition.\n\
3329 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update\n\
3330 any markers that happen to be located in the regions.\n\
3332 Transposing beyond buffer boundaries is an error.")
3333 (startr1
, endr1
, startr2
, endr2
, leave_markers
)
3334 Lisp_Object startr1
, endr1
, startr2
, endr2
, leave_markers
;
3336 register int start1
, end1
, start2
, end2
;
3337 int start1_byte
, start2_byte
, len1_byte
, len2_byte
;
3338 int gap
, len1
, len_mid
, len2
;
3339 unsigned char *start1_addr
, *start2_addr
, *temp
;
3340 int combined_before_bytes_1
, combined_after_bytes_1
;
3341 int combined_before_bytes_2
, combined_after_bytes_2
;
3342 struct gcpro gcpro1
, gcpro2
;
3344 INTERVAL cur_intv
, tmp_interval1
, tmp_interval_mid
, tmp_interval2
;
3345 cur_intv
= BUF_INTERVALS (current_buffer
);
3347 validate_region (&startr1
, &endr1
);
3348 validate_region (&startr2
, &endr2
);
3350 start1
= XFASTINT (startr1
);
3351 end1
= XFASTINT (endr1
);
3352 start2
= XFASTINT (startr2
);
3353 end2
= XFASTINT (endr2
);
3356 /* Swap the regions if they're reversed. */
3359 register int glumph
= start1
;
3367 len1
= end1
- start1
;
3368 len2
= end2
- start2
;
3371 error ("Transposed regions overlap");
3372 else if (start1
== end1
|| start2
== end2
)
3373 error ("Transposed region has length 0");
3375 /* The possibilities are:
3376 1. Adjacent (contiguous) regions, or separate but equal regions
3377 (no, really equal, in this case!), or
3378 2. Separate regions of unequal size.
3380 The worst case is usually No. 2. It means that (aside from
3381 potential need for getting the gap out of the way), there also
3382 needs to be a shifting of the text between the two regions. So
3383 if they are spread far apart, we are that much slower... sigh. */
3385 /* It must be pointed out that the really studly thing to do would
3386 be not to move the gap at all, but to leave it in place and work
3387 around it if necessary. This would be extremely efficient,
3388 especially considering that people are likely to do
3389 transpositions near where they are working interactively, which
3390 is exactly where the gap would be found. However, such code
3391 would be much harder to write and to read. So, if you are
3392 reading this comment and are feeling squirrely, by all means have
3393 a go! I just didn't feel like doing it, so I will simply move
3394 the gap the minimum distance to get it out of the way, and then
3395 deal with an unbroken array. */
3397 /* Make sure the gap won't interfere, by moving it out of the text
3398 we will operate on. */
3399 if (start1
< gap
&& gap
< end2
)
3401 if (gap
- start1
< end2
- gap
)
3407 start1_byte
= CHAR_TO_BYTE (start1
);
3408 start2_byte
= CHAR_TO_BYTE (start2
);
3409 len1_byte
= CHAR_TO_BYTE (end1
) - start1_byte
;
3410 len2_byte
= CHAR_TO_BYTE (end2
) - start2_byte
;
3414 combined_before_bytes_2
3415 = count_combining_before (BYTE_POS_ADDR (start2_byte
),
3416 len2_byte
, start1
, start1_byte
);
3417 combined_before_bytes_1
3418 = count_combining_before (BYTE_POS_ADDR (start1_byte
),
3419 len1_byte
, end2
, start2_byte
+ len2_byte
);
3420 combined_after_bytes_1
3421 = count_combining_after (BYTE_POS_ADDR (start1_byte
),
3422 len1_byte
, end2
, start2_byte
+ len2_byte
);
3423 combined_after_bytes_2
= 0;
3427 combined_before_bytes_2
3428 = count_combining_before (BYTE_POS_ADDR (start2_byte
),
3429 len2_byte
, start1
, start1_byte
);
3430 combined_before_bytes_1
3431 = count_combining_before (BYTE_POS_ADDR (start1_byte
),
3432 len1_byte
, start2
, start2_byte
);
3433 combined_after_bytes_2
3434 = count_combining_after (BYTE_POS_ADDR (start2_byte
),
3435 len2_byte
, end1
, start1_byte
+ len1_byte
);
3436 combined_after_bytes_1
3437 = count_combining_after (BYTE_POS_ADDR (start1_byte
),
3438 len1_byte
, end2
, start2_byte
+ len2_byte
);
3441 /* If any combining is going to happen, do this the stupid way,
3442 because replace handles combining properly. */
3443 if (combined_before_bytes_1
|| combined_before_bytes_2
3444 || combined_after_bytes_1
|| combined_after_bytes_2
)
3446 Lisp_Object text1
, text2
;
3448 text1
= text2
= Qnil
;
3449 GCPRO2 (text1
, text2
);
3451 text1
= make_buffer_string_both (start1
, start1_byte
,
3452 end1
, start1_byte
+ len1_byte
, 1);
3453 text2
= make_buffer_string_both (start2
, start2_byte
,
3454 end2
, start2_byte
+ len2_byte
, 1);
3456 transpose_markers (start1
, end1
, start2
, end2
,
3457 start1_byte
, start1_byte
+ len1_byte
,
3458 start2_byte
, start2_byte
+ len2_byte
);
3460 replace_range (start2
, end2
, text1
, 1, 0, 0);
3461 replace_range (start1
, end1
, text2
, 1, 0, 0);
3467 /* Hmmm... how about checking to see if the gap is large
3468 enough to use as the temporary storage? That would avoid an
3469 allocation... interesting. Later, don't fool with it now. */
3471 /* Working without memmove, for portability (sigh), so must be
3472 careful of overlapping subsections of the array... */
3474 if (end1
== start2
) /* adjacent regions */
3476 modify_region (current_buffer
, start1
, end2
);
3477 record_change (start1
, len1
+ len2
);
3479 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
3480 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
3481 Fset_text_properties (make_number (start1
), make_number (end2
),
3484 /* First region smaller than second. */
3485 if (len1_byte
< len2_byte
)
3487 /* We use alloca only if it is small,
3488 because we want to avoid stack overflow. */
3489 if (len2_byte
> 20000)
3490 temp
= (unsigned char *) xmalloc (len2_byte
);
3492 temp
= (unsigned char *) alloca (len2_byte
);
3494 /* Don't precompute these addresses. We have to compute them
3495 at the last minute, because the relocating allocator might
3496 have moved the buffer around during the xmalloc. */
3497 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3498 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3500 bcopy (start2_addr
, temp
, len2_byte
);
3501 bcopy (start1_addr
, start1_addr
+ len2_byte
, len1_byte
);
3502 bcopy (temp
, start1_addr
, len2_byte
);
3503 if (len2_byte
> 20000)
3507 /* First region not smaller than second. */
3509 if (len1_byte
> 20000)
3510 temp
= (unsigned char *) xmalloc (len1_byte
);
3512 temp
= (unsigned char *) alloca (len1_byte
);
3513 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3514 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3515 bcopy (start1_addr
, temp
, len1_byte
);
3516 bcopy (start2_addr
, start1_addr
, len2_byte
);
3517 bcopy (temp
, start1_addr
+ len2_byte
, len1_byte
);
3518 if (len1_byte
> 20000)
3521 graft_intervals_into_buffer (tmp_interval1
, start1
+ len2
,
3522 len1
, current_buffer
, 0);
3523 graft_intervals_into_buffer (tmp_interval2
, start1
,
3524 len2
, current_buffer
, 0);
3526 /* Non-adjacent regions, because end1 != start2, bleagh... */
3529 len_mid
= start2_byte
- (start1_byte
+ len1_byte
);
3531 if (len1_byte
== len2_byte
)
3532 /* Regions are same size, though, how nice. */
3534 modify_region (current_buffer
, start1
, end1
);
3535 modify_region (current_buffer
, start2
, end2
);
3536 record_change (start1
, len1
);
3537 record_change (start2
, len2
);
3538 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
3539 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
3540 Fset_text_properties (make_number (start1
), make_number (end1
),
3542 Fset_text_properties (make_number (start2
), make_number (end2
),
3545 if (len1_byte
> 20000)
3546 temp
= (unsigned char *) xmalloc (len1_byte
);
3548 temp
= (unsigned char *) alloca (len1_byte
);
3549 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3550 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3551 bcopy (start1_addr
, temp
, len1_byte
);
3552 bcopy (start2_addr
, start1_addr
, len2_byte
);
3553 bcopy (temp
, start2_addr
, len1_byte
);
3554 if (len1_byte
> 20000)
3556 graft_intervals_into_buffer (tmp_interval1
, start2
,
3557 len1
, current_buffer
, 0);
3558 graft_intervals_into_buffer (tmp_interval2
, start1
,
3559 len2
, current_buffer
, 0);
3562 else if (len1_byte
< len2_byte
) /* Second region larger than first */
3563 /* Non-adjacent & unequal size, area between must also be shifted. */
3565 modify_region (current_buffer
, start1
, end2
);
3566 record_change (start1
, (end2
- start1
));
3567 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
3568 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
3569 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
3570 Fset_text_properties (make_number (start1
), make_number (end2
),
3573 /* holds region 2 */
3574 if (len2_byte
> 20000)
3575 temp
= (unsigned char *) xmalloc (len2_byte
);
3577 temp
= (unsigned char *) alloca (len2_byte
);
3578 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3579 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3580 bcopy (start2_addr
, temp
, len2_byte
);
3581 bcopy (start1_addr
, start1_addr
+ len_mid
+ len2_byte
, len1_byte
);
3582 safe_bcopy (start1_addr
+ len1_byte
, start1_addr
+ len2_byte
, len_mid
);
3583 bcopy (temp
, start1_addr
, len2_byte
);
3584 if (len2_byte
> 20000)
3586 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
3587 len1
, current_buffer
, 0);
3588 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
3589 len_mid
, current_buffer
, 0);
3590 graft_intervals_into_buffer (tmp_interval2
, start1
,
3591 len2
, current_buffer
, 0);
3594 /* Second region smaller than first. */
3596 record_change (start1
, (end2
- start1
));
3597 modify_region (current_buffer
, start1
, end2
);
3599 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
3600 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
3601 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
3602 Fset_text_properties (make_number (start1
), make_number (end2
),
3605 /* holds region 1 */
3606 if (len1_byte
> 20000)
3607 temp
= (unsigned char *) xmalloc (len1_byte
);
3609 temp
= (unsigned char *) alloca (len1_byte
);
3610 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3611 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3612 bcopy (start1_addr
, temp
, len1_byte
);
3613 bcopy (start2_addr
, start1_addr
, len2_byte
);
3614 bcopy (start1_addr
+ len1_byte
, start1_addr
+ len2_byte
, len_mid
);
3615 bcopy (temp
, start1_addr
+ len2_byte
+ len_mid
, len1_byte
);
3616 if (len1_byte
> 20000)
3618 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
3619 len1
, current_buffer
, 0);
3620 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
3621 len_mid
, current_buffer
, 0);
3622 graft_intervals_into_buffer (tmp_interval2
, start1
,
3623 len2
, current_buffer
, 0);
3627 /* When doing multiple transpositions, it might be nice
3628 to optimize this. Perhaps the markers in any one buffer
3629 should be organized in some sorted data tree. */
3630 if (NILP (leave_markers
))
3632 transpose_markers (start1
, end1
, start2
, end2
,
3633 start1_byte
, start1_byte
+ len1_byte
,
3634 start2_byte
, start2_byte
+ len2_byte
);
3635 fix_overlays_in_range (start1
, end2
);
3647 Qbuffer_access_fontify_functions
3648 = intern ("buffer-access-fontify-functions");
3649 staticpro (&Qbuffer_access_fontify_functions
);
3651 DEFVAR_LISP ("buffer-access-fontify-functions",
3652 &Vbuffer_access_fontify_functions
,
3653 "List of functions called by `buffer-substring' to fontify if necessary.\n\
3654 Each function is called with two arguments which specify the range\n\
3655 of the buffer being accessed.");
3656 Vbuffer_access_fontify_functions
= Qnil
;
3660 extern Lisp_Object Vprin1_to_string_buffer
;
3661 obuf
= Fcurrent_buffer ();
3662 /* Do this here, because init_buffer_once is too early--it won't work. */
3663 Fset_buffer (Vprin1_to_string_buffer
);
3664 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
3665 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
3670 DEFVAR_LISP ("buffer-access-fontified-property",
3671 &Vbuffer_access_fontified_property
,
3672 "Property which (if non-nil) indicates text has been fontified.\n\
3673 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
3674 functions if all the text being accessed has this property.");
3675 Vbuffer_access_fontified_property
= Qnil
;
3677 DEFVAR_LISP ("system-name", &Vsystem_name
,
3678 "The name of the machine Emacs is running on.");
3680 DEFVAR_LISP ("user-full-name", &Vuser_full_name
,
3681 "The full name of the user logged in.");
3683 DEFVAR_LISP ("user-login-name", &Vuser_login_name
,
3684 "The user's name, taken from environment variables if possible.");
3686 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name
,
3687 "The user's name, based upon the real uid only.");
3689 defsubr (&Spropertize
);
3690 defsubr (&Schar_equal
);
3691 defsubr (&Sgoto_char
);
3692 defsubr (&Sstring_to_char
);
3693 defsubr (&Schar_to_string
);
3694 defsubr (&Sbuffer_substring
);
3695 defsubr (&Sbuffer_substring_no_properties
);
3696 defsubr (&Sbuffer_string
);
3698 defsubr (&Spoint_marker
);
3699 defsubr (&Smark_marker
);
3701 defsubr (&Sregion_beginning
);
3702 defsubr (&Sregion_end
);
3704 staticpro (&Qfield
);
3705 Qfield
= intern ("field");
3706 defsubr (&Sfield_beginning
);
3707 defsubr (&Sfield_end
);
3708 defsubr (&Sfield_string
);
3709 defsubr (&Sfield_string_no_properties
);
3710 defsubr (&Sdelete_field
);
3711 defsubr (&Sconstrain_to_field
);
3713 defsubr (&Sline_beginning_position
);
3714 defsubr (&Sline_end_position
);
3716 /* defsubr (&Smark); */
3717 /* defsubr (&Sset_mark); */
3718 defsubr (&Ssave_excursion
);
3719 defsubr (&Ssave_current_buffer
);
3721 defsubr (&Sbufsize
);
3722 defsubr (&Spoint_max
);
3723 defsubr (&Spoint_min
);
3724 defsubr (&Spoint_min_marker
);
3725 defsubr (&Spoint_max_marker
);
3726 defsubr (&Sgap_position
);
3727 defsubr (&Sgap_size
);
3728 defsubr (&Sposition_bytes
);
3729 defsubr (&Sbyte_to_position
);
3735 defsubr (&Sfollowing_char
);
3736 defsubr (&Sprevious_char
);
3737 defsubr (&Schar_after
);
3738 defsubr (&Schar_before
);
3740 defsubr (&Sinsert_before_markers
);
3741 defsubr (&Sinsert_and_inherit
);
3742 defsubr (&Sinsert_and_inherit_before_markers
);
3743 defsubr (&Sinsert_char
);
3745 defsubr (&Suser_login_name
);
3746 defsubr (&Suser_real_login_name
);
3747 defsubr (&Suser_uid
);
3748 defsubr (&Suser_real_uid
);
3749 defsubr (&Suser_full_name
);
3750 defsubr (&Semacs_pid
);
3751 defsubr (&Scurrent_time
);
3752 defsubr (&Sformat_time_string
);
3753 defsubr (&Sdecode_time
);
3754 defsubr (&Sencode_time
);
3755 defsubr (&Scurrent_time_string
);
3756 defsubr (&Scurrent_time_zone
);
3757 defsubr (&Sset_time_zone_rule
);
3758 defsubr (&Ssystem_name
);
3759 defsubr (&Smessage
);
3760 defsubr (&Smessage_box
);
3761 defsubr (&Smessage_or_box
);
3762 defsubr (&Scurrent_message
);
3765 defsubr (&Sinsert_buffer_substring
);
3766 defsubr (&Scompare_buffer_substrings
);
3767 defsubr (&Ssubst_char_in_region
);
3768 defsubr (&Stranslate_region
);
3769 defsubr (&Sdelete_region
);
3771 defsubr (&Snarrow_to_region
);
3772 defsubr (&Ssave_restriction
);
3773 defsubr (&Stranspose_regions
);