1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985,86,87,89,93,94,95,96,97,98 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. */
22 #include <sys/types.h>
41 #include "intervals.h"
48 #define min(a, b) ((a) < (b) ? (a) : (b))
49 #define max(a, b) ((a) > (b) ? (a) : (b))
55 extern char **environ
;
56 extern Lisp_Object
make_time ();
57 extern void insert_from_buffer ();
58 static int tm_diff ();
59 static void update_buffer_properties ();
60 size_t emacs_strftime ();
61 void set_time_zone_rule ();
63 Lisp_Object Vbuffer_access_fontify_functions
;
64 Lisp_Object Qbuffer_access_fontify_functions
;
65 Lisp_Object Vbuffer_access_fontified_property
;
67 Lisp_Object
Fuser_full_name ();
69 /* Some static data, and a function to initialize it for each run */
71 Lisp_Object Vsystem_name
;
72 Lisp_Object Vuser_real_login_name
; /* login name of current user ID */
73 Lisp_Object Vuser_full_name
; /* full name of current user */
74 Lisp_Object Vuser_login_name
; /* user name from LOGNAME or USER */
80 register unsigned char *p
, *q
, *r
;
81 struct passwd
*pw
; /* password entry for the current user */
84 /* Set up system_name even when dumping. */
88 /* Don't bother with this on initial start when just dumping out */
91 #endif /* not CANNOT_DUMP */
93 pw
= (struct passwd
*) getpwuid (getuid ());
95 /* We let the real user name default to "root" because that's quite
96 accurate on MSDOG and because it lets Emacs find the init file.
97 (The DVX libraries override the Djgpp libraries here.) */
98 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "root");
100 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "unknown");
103 /* Get the effective user name, by consulting environment variables,
104 or the effective uid if those are unset. */
105 user_name
= (char *) getenv ("LOGNAME");
108 user_name
= (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
109 #else /* WINDOWSNT */
110 user_name
= (char *) getenv ("USER");
111 #endif /* WINDOWSNT */
114 pw
= (struct passwd
*) getpwuid (geteuid ());
115 user_name
= (char *) (pw
? pw
->pw_name
: "unknown");
117 Vuser_login_name
= build_string (user_name
);
119 /* If the user name claimed in the environment vars differs from
120 the real uid, use the claimed name to find the full name. */
121 tem
= Fstring_equal (Vuser_login_name
, Vuser_real_login_name
);
122 Vuser_full_name
= Fuser_full_name (NILP (tem
)? make_number (geteuid())
125 p
= (unsigned char *) getenv ("NAME");
127 Vuser_full_name
= build_string (p
);
128 else if (NILP (Vuser_full_name
))
129 Vuser_full_name
= build_string ("unknown");
132 DEFUN ("char-to-string", Fchar_to_string
, Schar_to_string
, 1, 1, 0,
133 "Convert arg CHAR to a string containing that character.")
135 Lisp_Object character
;
138 unsigned char workbuf
[4], *str
;
140 CHECK_NUMBER (character
, 0);
142 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
143 return make_string_from_bytes (str
, 1, len
);
146 DEFUN ("string-to-char", Fstring_to_char
, Sstring_to_char
, 1, 1, 0,
147 "Convert arg STRING to a character, the first character of that string.\n\
148 A multibyte character is handled correctly.")
150 register Lisp_Object string
;
152 register Lisp_Object val
;
153 register struct Lisp_String
*p
;
154 CHECK_STRING (string
, 0);
155 p
= XSTRING (string
);
158 if (STRING_MULTIBYTE (string
))
159 XSETFASTINT (val
, STRING_CHAR (p
->data
, STRING_BYTES (p
)));
161 XSETFASTINT (val
, p
->data
[0]);
164 XSETFASTINT (val
, 0);
169 buildmark (charpos
, bytepos
)
170 int charpos
, bytepos
;
172 register Lisp_Object mark
;
173 mark
= Fmake_marker ();
174 set_marker_both (mark
, Qnil
, charpos
, bytepos
);
178 DEFUN ("point", Fpoint
, Spoint
, 0, 0, 0,
179 "Return value of point, as an integer.\n\
180 Beginning of buffer is position (point-min)")
184 XSETFASTINT (temp
, PT
);
188 DEFUN ("point-marker", Fpoint_marker
, Spoint_marker
, 0, 0, 0,
189 "Return value of point, as a marker object.")
192 return buildmark (PT
, PT_BYTE
);
196 clip_to_bounds (lower
, num
, upper
)
197 int lower
, num
, upper
;
201 else if (num
> upper
)
207 DEFUN ("goto-char", Fgoto_char
, Sgoto_char
, 1, 1, "NGoto char: ",
208 "Set point to POSITION, a number or marker.\n\
209 Beginning of buffer is position (point-min), end is (point-max).\n\
210 If the position is in the middle of a multibyte form,\n\
211 the actual point is set at the head of the multibyte form\n\
212 except in the case that `enable-multibyte-characters' is nil.")
214 register Lisp_Object position
;
219 if (MARKERP (position
)
220 && current_buffer
== XMARKER (position
)->buffer
)
222 pos
= marker_position (position
);
224 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
226 SET_PT_BOTH (ZV
, ZV_BYTE
);
228 SET_PT_BOTH (pos
, marker_byte_position (position
));
233 CHECK_NUMBER_COERCE_MARKER (position
, 0);
235 pos
= clip_to_bounds (BEGV
, XINT (position
), ZV
);
241 region_limit (beginningp
)
244 extern Lisp_Object Vmark_even_if_inactive
; /* Defined in callint.c. */
245 register Lisp_Object m
;
246 if (!NILP (Vtransient_mark_mode
) && NILP (Vmark_even_if_inactive
)
247 && NILP (current_buffer
->mark_active
))
248 Fsignal (Qmark_inactive
, Qnil
);
249 m
= Fmarker_position (current_buffer
->mark
);
250 if (NILP (m
)) error ("There is no region now");
251 if ((PT
< XFASTINT (m
)) == beginningp
)
252 return (make_number (PT
));
257 DEFUN ("region-beginning", Fregion_beginning
, Sregion_beginning
, 0, 0, 0,
258 "Return position of beginning of region, as an integer.")
261 return (region_limit (1));
264 DEFUN ("region-end", Fregion_end
, Sregion_end
, 0, 0, 0,
265 "Return position of end of region, as an integer.")
268 return (region_limit (0));
271 DEFUN ("mark-marker", Fmark_marker
, Smark_marker
, 0, 0, 0,
272 "Return this buffer's mark, as a marker object.\n\
273 Watch out! Moving this marker changes the mark position.\n\
274 If you set the marker not to point anywhere, the buffer will have no mark.")
277 return current_buffer
->mark
;
280 DEFUN ("line-beginning-position", Fline_beginning_position
, Sline_beginning_position
,
282 "Return the character position of the first character on the current line.\n\
283 With argument N not nil or 1, move forward N - 1 lines first.\n\
284 If scan reaches end of buffer, return that position.\n\
285 This function does not move point.")
289 register int orig
, orig_byte
, end
;
298 Fforward_line (make_number (XINT (n
) - 1));
300 SET_PT_BOTH (orig
, orig_byte
);
302 return make_number (end
);
305 DEFUN ("line-end-position", Fline_end_position
, Sline_end_position
,
307 "Return the character position of the last character on the current line.\n\
308 With argument N not nil or 1, move forward N - 1 lines first.\n\
309 If scan reaches end of buffer, return that position.\n\
310 This function does not move point.")
319 return make_number (find_before_next_newline
320 (PT
, 0, XINT (n
) - (XINT (n
) <= 0)));
324 save_excursion_save ()
326 register int visible
= (XBUFFER (XWINDOW (selected_window
)->buffer
)
329 return Fcons (Fpoint_marker (),
330 Fcons (Fcopy_marker (current_buffer
->mark
, Qnil
),
331 Fcons (visible
? Qt
: Qnil
,
332 current_buffer
->mark_active
)));
336 save_excursion_restore (info
)
339 Lisp_Object tem
, tem1
, omark
, nmark
;
340 struct gcpro gcpro1
, gcpro2
, gcpro3
;
342 tem
= Fmarker_buffer (Fcar (info
));
343 /* If buffer being returned to is now deleted, avoid error */
344 /* Otherwise could get error here while unwinding to top level
346 /* In that case, Fmarker_buffer returns nil now. */
350 omark
= nmark
= Qnil
;
351 GCPRO3 (info
, omark
, nmark
);
356 unchain_marker (tem
);
357 tem
= Fcar (Fcdr (info
));
358 omark
= Fmarker_position (current_buffer
->mark
);
359 Fset_marker (current_buffer
->mark
, tem
, Fcurrent_buffer ());
360 nmark
= Fmarker_position (tem
);
361 unchain_marker (tem
);
362 tem
= Fcdr (Fcdr (info
));
363 #if 0 /* We used to make the current buffer visible in the selected window
364 if that was true previously. That avoids some anomalies.
365 But it creates others, and it wasn't documented, and it is simpler
366 and cleaner never to alter the window/buffer connections. */
369 && current_buffer
!= XBUFFER (XWINDOW (selected_window
)->buffer
))
370 Fswitch_to_buffer (Fcurrent_buffer (), Qnil
);
373 tem1
= current_buffer
->mark_active
;
374 current_buffer
->mark_active
= Fcdr (tem
);
375 if (!NILP (Vrun_hooks
))
377 /* If mark is active now, and either was not active
378 or was at a different place, run the activate hook. */
379 if (! NILP (current_buffer
->mark_active
))
381 if (! EQ (omark
, nmark
))
382 call1 (Vrun_hooks
, intern ("activate-mark-hook"));
384 /* If mark has ceased to be active, run deactivate hook. */
385 else if (! NILP (tem1
))
386 call1 (Vrun_hooks
, intern ("deactivate-mark-hook"));
392 DEFUN ("save-excursion", Fsave_excursion
, Ssave_excursion
, 0, UNEVALLED
, 0,
393 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
394 Executes BODY just like `progn'.\n\
395 The values of point, mark and the current buffer are restored\n\
396 even in case of abnormal exit (throw or error).\n\
397 The state of activation of the mark is also restored.\n\
399 This construct does not save `deactivate-mark', and therefore\n\
400 functions that change the buffer will still cause deactivation\n\
401 of the mark at the end of the command. To prevent that, bind\n\
402 `deactivate-mark' with `let'.")
406 register Lisp_Object val
;
407 int count
= specpdl_ptr
- specpdl
;
409 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
412 return unbind_to (count
, val
);
415 DEFUN ("save-current-buffer", Fsave_current_buffer
, Ssave_current_buffer
, 0, UNEVALLED
, 0,
416 "Save the current buffer; execute BODY; restore the current buffer.\n\
417 Executes BODY just like `progn'.")
421 register Lisp_Object val
;
422 int count
= specpdl_ptr
- specpdl
;
424 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
427 return unbind_to (count
, val
);
430 DEFUN ("buffer-size", Fbufsize
, Sbufsize
, 0, 0, 0,
431 "Return the number of characters in the current buffer.")
435 XSETFASTINT (temp
, Z
- BEG
);
439 DEFUN ("point-min", Fpoint_min
, Spoint_min
, 0, 0, 0,
440 "Return the minimum permissible value of point in the current buffer.\n\
441 This is 1, unless narrowing (a buffer restriction) is in effect.")
445 XSETFASTINT (temp
, BEGV
);
449 DEFUN ("point-min-marker", Fpoint_min_marker
, Spoint_min_marker
, 0, 0, 0,
450 "Return a marker to the minimum permissible value of point in this buffer.\n\
451 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
454 return buildmark (BEGV
, BEGV_BYTE
);
457 DEFUN ("point-max", Fpoint_max
, Spoint_max
, 0, 0, 0,
458 "Return the maximum permissible value of point in the current buffer.\n\
459 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
460 is in effect, in which case it is less.")
464 XSETFASTINT (temp
, ZV
);
468 DEFUN ("point-max-marker", Fpoint_max_marker
, Spoint_max_marker
, 0, 0, 0,
469 "Return a marker to the maximum permissible value of point in this buffer.\n\
470 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
471 is in effect, in which case it is less.")
474 return buildmark (ZV
, ZV_BYTE
);
477 DEFUN ("gap-position", Fgap_position
, Sgap_position
, 0, 0, 0,
478 "Return the position of the gap, in the current buffer.\n\
479 See also `gap-size'.")
483 XSETFASTINT (temp
, GPT
);
487 DEFUN ("gap-size", Fgap_size
, Sgap_size
, 0, 0, 0,
488 "Return the size of the current buffer's gap.\n\
489 See also `gap-position'.")
493 XSETFASTINT (temp
, GAP_SIZE
);
497 DEFUN ("position-bytes", Fposition_bytes
, Sposition_bytes
, 1, 1, 0,
498 "Return the byte position for character position POSITION.\n\
499 If POSITION is out of range, the value is nil.")
501 Lisp_Object position
;
503 CHECK_NUMBER_COERCE_MARKER (position
, 1);
504 if (XINT (position
) < BEG
|| XINT (position
) > Z
)
506 return make_number (CHAR_TO_BYTE (XINT (position
)));
509 DEFUN ("byte-to-position", Fbyte_to_position
, Sbyte_to_position
, 1, 1, 0,
510 "Return the character position for byte position BYTEPOS.\n\
511 If BYTEPOS is out of range, the value is nil.")
515 CHECK_NUMBER (bytepos
, 1);
516 if (XINT (bytepos
) < BEG_BYTE
|| XINT (bytepos
) > Z_BYTE
)
518 return make_number (BYTE_TO_CHAR (XINT (bytepos
)));
521 DEFUN ("following-char", Ffollowing_char
, Sfollowing_char
, 0, 0, 0,
522 "Return the character following point, as a number.\n\
523 At the end of the buffer or accessible region, return 0.\n\
524 If `enable-multibyte-characters' is nil or point is not\n\
525 at character boundary, multibyte form is ignored,\n\
526 and only one byte following point is returned as a character.")
531 XSETFASTINT (temp
, 0);
533 XSETFASTINT (temp
, FETCH_CHAR (PT_BYTE
));
537 DEFUN ("preceding-char", Fprevious_char
, Sprevious_char
, 0, 0, 0,
538 "Return the character preceding point, as a number.\n\
539 At the beginning of the buffer or accessible region, return 0.\n\
540 If `enable-multibyte-characters' is nil or point is not\n\
541 at character boundary, multi-byte form is ignored,\n\
542 and only one byte preceding point is returned as a character.")
547 XSETFASTINT (temp
, 0);
548 else if (!NILP (current_buffer
->enable_multibyte_characters
))
552 XSETFASTINT (temp
, FETCH_CHAR (pos
));
555 XSETFASTINT (temp
, FETCH_BYTE (PT_BYTE
- 1));
559 DEFUN ("bobp", Fbobp
, Sbobp
, 0, 0, 0,
560 "Return t if point is at the beginning of the buffer.\n\
561 If the buffer is narrowed, this means the beginning of the narrowed part.")
569 DEFUN ("eobp", Feobp
, Seobp
, 0, 0, 0,
570 "Return t if point is at the end of the buffer.\n\
571 If the buffer is narrowed, this means the end of the narrowed part.")
579 DEFUN ("bolp", Fbolp
, Sbolp
, 0, 0, 0,
580 "Return t if point is at the beginning of a line.")
583 if (PT
== BEGV
|| FETCH_BYTE (PT_BYTE
- 1) == '\n')
588 DEFUN ("eolp", Feolp
, Seolp
, 0, 0, 0,
589 "Return t if point is at the end of a line.\n\
590 `End of a line' includes point being at the end of the buffer.")
593 if (PT
== ZV
|| FETCH_BYTE (PT_BYTE
) == '\n')
598 DEFUN ("char-after", Fchar_after
, Schar_after
, 0, 1, 0,
599 "Return character in current buffer at position POS.\n\
600 POS is an integer or a buffer pointer.\n\
601 If POS is out of range, the value is nil.")
605 register int pos_byte
;
606 register Lisp_Object val
;
611 XSETFASTINT (pos
, PT
);
616 pos_byte
= marker_byte_position (pos
);
617 if (pos_byte
< BEGV_BYTE
|| pos_byte
>= ZV_BYTE
)
622 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
623 if (XINT (pos
) < BEGV
|| XINT (pos
) >= ZV
)
626 pos_byte
= CHAR_TO_BYTE (XINT (pos
));
629 return make_number (FETCH_CHAR (pos_byte
));
632 DEFUN ("char-before", Fchar_before
, Schar_before
, 0, 1, 0,
633 "Return character in current buffer preceding position POS.\n\
634 POS is an integer or a buffer pointer.\n\
635 If POS is out of range, the value is nil.")
639 register Lisp_Object val
;
640 register int pos_byte
;
645 XSETFASTINT (pos
, PT
);
650 pos_byte
= marker_byte_position (pos
);
652 if (pos_byte
<= BEGV_BYTE
|| pos_byte
> ZV_BYTE
)
657 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
659 if (XINT (pos
) <= BEGV
|| XINT (pos
) > ZV
)
662 pos_byte
= CHAR_TO_BYTE (XINT (pos
));
665 if (!NILP (current_buffer
->enable_multibyte_characters
))
668 XSETFASTINT (val
, FETCH_CHAR (pos_byte
));
673 XSETFASTINT (val
, FETCH_BYTE (pos_byte
));
678 DEFUN ("user-login-name", Fuser_login_name
, Suser_login_name
, 0, 1, 0,
679 "Return the name under which the user logged in, as a string.\n\
680 This is based on the effective uid, not the real uid.\n\
681 Also, if the environment variable LOGNAME or USER is set,\n\
682 that determines the value of this function.\n\n\
683 If optional argument UID is an integer, return the login name of the user\n\
684 with that uid, or nil if there is no such user.")
690 /* Set up the user name info if we didn't do it before.
691 (That can happen if Emacs is dumpable
692 but you decide to run `temacs -l loadup' and not dump. */
693 if (INTEGERP (Vuser_login_name
))
697 return Vuser_login_name
;
699 CHECK_NUMBER (uid
, 0);
700 pw
= (struct passwd
*) getpwuid (XINT (uid
));
701 return (pw
? build_string (pw
->pw_name
) : Qnil
);
704 DEFUN ("user-real-login-name", Fuser_real_login_name
, Suser_real_login_name
,
706 "Return the name of the user's real uid, as a string.\n\
707 This ignores the environment variables LOGNAME and USER, so it differs from\n\
708 `user-login-name' when running under `su'.")
711 /* Set up the user name info if we didn't do it before.
712 (That can happen if Emacs is dumpable
713 but you decide to run `temacs -l loadup' and not dump. */
714 if (INTEGERP (Vuser_login_name
))
716 return Vuser_real_login_name
;
719 DEFUN ("user-uid", Fuser_uid
, Suser_uid
, 0, 0, 0,
720 "Return the effective uid of Emacs, as an integer.")
723 return make_number (geteuid ());
726 DEFUN ("user-real-uid", Fuser_real_uid
, Suser_real_uid
, 0, 0, 0,
727 "Return the real uid of Emacs, as an integer.")
730 return make_number (getuid ());
733 DEFUN ("user-full-name", Fuser_full_name
, Suser_full_name
, 0, 1, 0,
734 "Return the full name of the user logged in, as a string.\n\
735 If the full name corresponding to Emacs's userid is not known,\n\
736 return \"unknown\".\n\
738 If optional argument UID is an integer, return the full name of the user\n\
739 with that uid, or nil if there is no such user.\n\
740 If UID is a string, return the full name of the user with that login\n\
741 name, or nil if there is no such user.")
746 register unsigned char *p
, *q
;
747 extern char *index ();
751 return Vuser_full_name
;
752 else if (NUMBERP (uid
))
753 pw
= (struct passwd
*) getpwuid (XINT (uid
));
754 else if (STRINGP (uid
))
755 pw
= (struct passwd
*) getpwnam (XSTRING (uid
)->data
);
757 error ("Invalid UID specification");
762 p
= (unsigned char *) USER_FULL_NAME
;
763 /* Chop off everything after the first comma. */
764 q
= (unsigned char *) index (p
, ',');
765 full
= make_string (p
, q
? q
- p
: strlen (p
));
767 #ifdef AMPERSAND_FULL_NAME
768 p
= XSTRING (full
)->data
;
769 q
= (unsigned char *) index (p
, '&');
770 /* Substitute the login name for the &, upcasing the first character. */
773 register unsigned char *r
;
776 login
= Fuser_login_name (make_number (pw
->pw_uid
));
777 r
= (unsigned char *) alloca (strlen (p
) + XSTRING (login
)->size
+ 1);
780 strcat (r
, XSTRING (login
)->data
);
781 r
[q
- p
] = UPCASE (r
[q
- p
]);
783 full
= build_string (r
);
785 #endif /* AMPERSAND_FULL_NAME */
790 DEFUN ("system-name", Fsystem_name
, Ssystem_name
, 0, 0, 0,
791 "Return the name of the machine you are running on, as a string.")
797 /* For the benefit of callers who don't want to include lisp.h */
801 if (STRINGP (Vsystem_name
))
802 return (char *) XSTRING (Vsystem_name
)->data
;
807 DEFUN ("emacs-pid", Femacs_pid
, Semacs_pid
, 0, 0, 0,
808 "Return the process ID of Emacs, as an integer.")
811 return make_number (getpid ());
814 DEFUN ("current-time", Fcurrent_time
, Scurrent_time
, 0, 0, 0,
815 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
816 The time is returned as a list of three integers. The first has the\n\
817 most significant 16 bits of the seconds, while the second has the\n\
818 least significant 16 bits. The third integer gives the microsecond\n\
821 The microsecond count is zero on systems that do not provide\n\
822 resolution finer than a second.")
826 Lisp_Object result
[3];
829 XSETINT (result
[0], (EMACS_SECS (t
) >> 16) & 0xffff);
830 XSETINT (result
[1], (EMACS_SECS (t
) >> 0) & 0xffff);
831 XSETINT (result
[2], EMACS_USECS (t
));
833 return Flist (3, result
);
838 lisp_time_argument (specified_time
, result
)
839 Lisp_Object specified_time
;
842 if (NILP (specified_time
))
843 return time (result
) != -1;
846 Lisp_Object high
, low
;
847 high
= Fcar (specified_time
);
848 CHECK_NUMBER (high
, 0);
849 low
= Fcdr (specified_time
);
852 CHECK_NUMBER (low
, 0);
853 *result
= (XINT (high
) << 16) + (XINT (low
) & 0xffff);
854 return *result
>> 16 == XINT (high
);
858 /* Write information into buffer S of size MAXSIZE, according to the
859 FORMAT of length FORMAT_LEN, using time information taken from *TP.
860 Return the number of bytes written, not including the terminating
861 '\0'. If S is NULL, nothing will be written anywhere; so to
862 determine how many bytes would be written, use NULL for S and
863 ((size_t) -1) for MAXSIZE.
865 This function behaves like emacs_strftime, except it allows null
868 emacs_memftime (s
, maxsize
, format
, format_len
, tp
)
877 /* Loop through all the null-terminated strings in the format
878 argument. Normally there's just one null-terminated string, but
879 there can be arbitrarily many, concatenated together, if the
880 format contains '\0' bytes. emacs_strftime stops at the first
881 '\0' byte so we must invoke it separately for each such string. */
890 result
= emacs_strftime (s
, maxsize
, format
, tp
);
894 if (result
== 0 && s
[0] != '\0')
899 maxsize
-= result
+ 1;
901 len
= strlen (format
);
902 if (len
== format_len
)
906 format_len
-= len
+ 1;
911 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
912 "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
913 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
914 `current-time' or `file-attributes'.\n\
915 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
916 as Universal Time; nil means describe TIME in the local time zone.\n\
917 The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
918 by text that describes the specified date and time in TIME:\n\
920 %Y is the year, %y within the century, %C the century.\n\
921 %G is the year corresponding to the ISO week, %g within the century.\n\
922 %m is the numeric month.\n\
923 %b and %h are the locale's abbreviated month name, %B the full name.\n\
924 %d is the day of the month, zero-padded, %e is blank-padded.\n\
925 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\
926 %a is the locale's abbreviated name of the day of week, %A the full name.\n\
927 %U is the week number starting on Sunday, %W starting on Monday,\n\
928 %V according to ISO 8601.\n\
929 %j is the day of the year.\n\
931 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\
932 only blank-padded, %l is like %I blank-padded.\n\
933 %p is the locale's equivalent of either AM or PM.\n\
936 %Z is the time zone name, %z is the numeric form.\n\
937 %s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\
939 %c is the locale's date and time format.\n\
940 %x is the locale's \"preferred\" date format.\n\
941 %D is like \"%m/%d/%y\".\n\
943 %R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\
944 %X is the locale's \"preferred\" time format.\n\
946 Finally, %n is a newline, %t is a tab, %% is a literal %.\n\
948 Certain flags and modifiers are available with some format controls.\n\
949 The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
950 but padded with blanks; %-X is like %X, but without padding.\n\
951 %NX (where N stands for an integer) is like %X,\n\
952 but takes up at least N (a number) positions.\n\
953 The modifiers are `E' and `O'. For certain characters X,\n\
954 %EX is a locale's alternative version of %X;\n\
955 %OX is like %X, but uses the locale's number symbols.\n\
957 For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
958 (format_string, time, universal)
961 DEFUN ("format-time-string", Fformat_time_string
, Sformat_time_string
, 1, 3, 0,
962 0 /* See immediately above */)
963 (format_string
, time
, universal
)
964 Lisp_Object format_string
, time
, universal
;
970 CHECK_STRING (format_string
, 1);
972 if (! lisp_time_argument (time
, &value
))
973 error ("Invalid time specification");
975 /* This is probably enough. */
976 size
= STRING_BYTES (XSTRING (format_string
)) * 6 + 50;
978 tm
= NILP (universal
) ? localtime (&value
) : gmtime (&value
);
980 error ("Specified time is not representable");
984 char *buf
= (char *) alloca (size
+ 1);
988 result
= emacs_memftime (buf
, size
, XSTRING (format_string
)->data
,
989 STRING_BYTES (XSTRING (format_string
)),
991 if ((result
> 0 && result
< size
) || (result
== 0 && buf
[0] == '\0'))
992 return make_string (buf
, result
);
994 /* If buffer was too small, make it bigger and try again. */
995 result
= emacs_memftime (NULL
, (size_t) -1,
996 XSTRING (format_string
)->data
,
997 STRING_BYTES (XSTRING (format_string
)),
1003 DEFUN ("decode-time", Fdecode_time
, Sdecode_time
, 0, 1, 0,
1004 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
1005 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
1006 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
1007 to use the current time. The list has the following nine members:\n\
1008 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
1009 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
1010 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
1011 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
1012 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
1013 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
1014 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
1015 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
1017 Lisp_Object specified_time
;
1021 struct tm
*decoded_time
;
1022 Lisp_Object list_args
[9];
1024 if (! lisp_time_argument (specified_time
, &time_spec
))
1025 error ("Invalid time specification");
1027 decoded_time
= localtime (&time_spec
);
1029 error ("Specified time is not representable");
1030 XSETFASTINT (list_args
[0], decoded_time
->tm_sec
);
1031 XSETFASTINT (list_args
[1], decoded_time
->tm_min
);
1032 XSETFASTINT (list_args
[2], decoded_time
->tm_hour
);
1033 XSETFASTINT (list_args
[3], decoded_time
->tm_mday
);
1034 XSETFASTINT (list_args
[4], decoded_time
->tm_mon
+ 1);
1035 XSETINT (list_args
[5], decoded_time
->tm_year
+ 1900);
1036 XSETFASTINT (list_args
[6], decoded_time
->tm_wday
);
1037 list_args
[7] = (decoded_time
->tm_isdst
)? Qt
: Qnil
;
1039 /* Make a copy, in case gmtime modifies the struct. */
1040 save_tm
= *decoded_time
;
1041 decoded_time
= gmtime (&time_spec
);
1042 if (decoded_time
== 0)
1043 list_args
[8] = Qnil
;
1045 XSETINT (list_args
[8], tm_diff (&save_tm
, decoded_time
));
1046 return Flist (9, list_args
);
1049 DEFUN ("encode-time", Fencode_time
, Sencode_time
, 6, MANY
, 0,
1050 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
1051 This is the reverse operation of `decode-time', which see.\n\
1052 ZONE defaults to the current time zone rule. This can\n\
1053 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
1054 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
1055 applied without consideration for daylight savings time.\n\
1057 You can pass more than 7 arguments; then the first six arguments\n\
1058 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
1059 The intervening arguments are ignored.\n\
1060 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
1062 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
1063 for example, a DAY of 0 means the day preceding the given month.\n\
1064 Year numbers less than 100 are treated just like other year numbers.\n\
1065 If you want them to stand for years in this century, you must do that yourself.")
1068 register Lisp_Object
*args
;
1072 Lisp_Object zone
= (nargs
> 6 ? args
[nargs
- 1] : Qnil
);
1074 CHECK_NUMBER (args
[0], 0); /* second */
1075 CHECK_NUMBER (args
[1], 1); /* minute */
1076 CHECK_NUMBER (args
[2], 2); /* hour */
1077 CHECK_NUMBER (args
[3], 3); /* day */
1078 CHECK_NUMBER (args
[4], 4); /* month */
1079 CHECK_NUMBER (args
[5], 5); /* year */
1081 tm
.tm_sec
= XINT (args
[0]);
1082 tm
.tm_min
= XINT (args
[1]);
1083 tm
.tm_hour
= XINT (args
[2]);
1084 tm
.tm_mday
= XINT (args
[3]);
1085 tm
.tm_mon
= XINT (args
[4]) - 1;
1086 tm
.tm_year
= XINT (args
[5]) - 1900;
1092 time
= mktime (&tm
);
1097 char **oldenv
= environ
, **newenv
;
1101 else if (STRINGP (zone
))
1102 tzstring
= (char *) XSTRING (zone
)->data
;
1103 else if (INTEGERP (zone
))
1105 int abszone
= abs (XINT (zone
));
1106 sprintf (tzbuf
, "XXX%s%d:%02d:%02d", "-" + (XINT (zone
) < 0),
1107 abszone
/ (60*60), (abszone
/60) % 60, abszone
% 60);
1111 error ("Invalid time zone specification");
1113 /* Set TZ before calling mktime; merely adjusting mktime's returned
1114 value doesn't suffice, since that would mishandle leap seconds. */
1115 set_time_zone_rule (tzstring
);
1117 time
= mktime (&tm
);
1119 /* Restore TZ to previous value. */
1123 #ifdef LOCALTIME_CACHE
1128 if (time
== (time_t) -1)
1129 error ("Specified time is not representable");
1131 return make_time (time
);
1134 DEFUN ("current-time-string", Fcurrent_time_string
, Scurrent_time_string
, 0, 1, 0,
1135 "Return the current time, as a human-readable string.\n\
1136 Programs can use this function to decode a time,\n\
1137 since the number of columns in each field is fixed.\n\
1138 The format is `Sun Sep 16 01:03:52 1973'.\n\
1139 However, see also the functions `decode-time' and `format-time-string'\n\
1140 which provide a much more powerful and general facility.\n\
1142 If an argument is given, it specifies a time to format\n\
1143 instead of the current time. The argument should have the form:\n\
1146 (HIGH LOW . IGNORED).\n\
1147 Thus, you can use times obtained from `current-time'\n\
1148 and from `file-attributes'.")
1150 Lisp_Object specified_time
;
1156 if (! lisp_time_argument (specified_time
, &value
))
1158 tem
= (char *) ctime (&value
);
1160 strncpy (buf
, tem
, 24);
1163 return build_string (buf
);
1166 #define TM_YEAR_BASE 1900
1168 /* Yield A - B, measured in seconds.
1169 This function is copied from the GNU C Library. */
1174 /* Compute intervening leap days correctly even if year is negative.
1175 Take care to avoid int overflow in leap day calculations,
1176 but it's OK to assume that A and B are close to each other. */
1177 int a4
= (a
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (a
->tm_year
& 3);
1178 int b4
= (b
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (b
->tm_year
& 3);
1179 int a100
= a4
/ 25 - (a4
% 25 < 0);
1180 int b100
= b4
/ 25 - (b4
% 25 < 0);
1181 int a400
= a100
>> 2;
1182 int b400
= b100
>> 2;
1183 int intervening_leap_days
= (a4
- b4
) - (a100
- b100
) + (a400
- b400
);
1184 int years
= a
->tm_year
- b
->tm_year
;
1185 int days
= (365 * years
+ intervening_leap_days
1186 + (a
->tm_yday
- b
->tm_yday
));
1187 return (60 * (60 * (24 * days
+ (a
->tm_hour
- b
->tm_hour
))
1188 + (a
->tm_min
- b
->tm_min
))
1189 + (a
->tm_sec
- b
->tm_sec
));
1192 DEFUN ("current-time-zone", Fcurrent_time_zone
, Scurrent_time_zone
, 0, 1, 0,
1193 "Return the offset and name for the local time zone.\n\
1194 This returns a list of the form (OFFSET NAME).\n\
1195 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1196 A negative value means west of Greenwich.\n\
1197 NAME is a string giving the name of the time zone.\n\
1198 If an argument is given, it specifies when the time zone offset is determined\n\
1199 instead of using the current time. The argument should have the form:\n\
1202 (HIGH LOW . IGNORED).\n\
1203 Thus, you can use times obtained from `current-time'\n\
1204 and from `file-attributes'.\n\
1206 Some operating systems cannot provide all this information to Emacs;\n\
1207 in this case, `current-time-zone' returns a list containing nil for\n\
1208 the data it can't find.")
1210 Lisp_Object specified_time
;
1216 if (lisp_time_argument (specified_time
, &value
)
1217 && (t
= gmtime (&value
)) != 0
1218 && (gmt
= *t
, t
= localtime (&value
)) != 0)
1220 int offset
= tm_diff (t
, &gmt
);
1225 s
= (char *)t
->tm_zone
;
1226 #else /* not HAVE_TM_ZONE */
1228 if (t
->tm_isdst
== 0 || t
->tm_isdst
== 1)
1229 s
= tzname
[t
->tm_isdst
];
1231 #endif /* not HAVE_TM_ZONE */
1234 /* No local time zone name is available; use "+-NNNN" instead. */
1235 int am
= (offset
< 0 ? -offset
: offset
) / 60;
1236 sprintf (buf
, "%c%02d%02d", (offset
< 0 ? '-' : '+'), am
/60, am
%60);
1239 return Fcons (make_number (offset
), Fcons (build_string (s
), Qnil
));
1242 return Fmake_list (make_number (2), Qnil
);
1245 /* This holds the value of `environ' produced by the previous
1246 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1247 has never been called. */
1248 static char **environbuf
;
1250 DEFUN ("set-time-zone-rule", Fset_time_zone_rule
, Sset_time_zone_rule
, 1, 1, 0,
1251 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
1252 If TZ is nil, use implementation-defined default time zone information.\n\
1253 If TZ is t, use Universal Time.")
1261 else if (EQ (tz
, Qt
))
1265 CHECK_STRING (tz
, 0);
1266 tzstring
= (char *) XSTRING (tz
)->data
;
1269 set_time_zone_rule (tzstring
);
1272 environbuf
= environ
;
1277 #ifdef LOCALTIME_CACHE
1279 /* These two values are known to load tz files in buggy implementations,
1280 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1281 Their values shouldn't matter in non-buggy implementations.
1282 We don't use string literals for these strings,
1283 since if a string in the environment is in readonly
1284 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1285 See Sun bugs 1113095 and 1114114, ``Timezone routines
1286 improperly modify environment''. */
1288 static char set_time_zone_rule_tz1
[] = "TZ=GMT+0";
1289 static char set_time_zone_rule_tz2
[] = "TZ=GMT+1";
1293 /* Set the local time zone rule to TZSTRING.
1294 This allocates memory into `environ', which it is the caller's
1295 responsibility to free. */
1297 set_time_zone_rule (tzstring
)
1301 char **from
, **to
, **newenv
;
1303 /* Make the ENVIRON vector longer with room for TZSTRING. */
1304 for (from
= environ
; *from
; from
++)
1306 envptrs
= from
- environ
+ 2;
1307 newenv
= to
= (char **) xmalloc (envptrs
* sizeof (char *)
1308 + (tzstring
? strlen (tzstring
) + 4 : 0));
1310 /* Add TZSTRING to the end of environ, as a value for TZ. */
1313 char *t
= (char *) (to
+ envptrs
);
1315 strcat (t
, tzstring
);
1319 /* Copy the old environ vector elements into NEWENV,
1320 but don't copy the TZ variable.
1321 So we have only one definition of TZ, which came from TZSTRING. */
1322 for (from
= environ
; *from
; from
++)
1323 if (strncmp (*from
, "TZ=", 3) != 0)
1329 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1330 the TZ variable is stored. If we do not have a TZSTRING,
1331 TO points to the vector slot which has the terminating null. */
1333 #ifdef LOCALTIME_CACHE
1335 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1336 "US/Pacific" that loads a tz file, then changes to a value like
1337 "XXX0" that does not load a tz file, and then changes back to
1338 its original value, the last change is (incorrectly) ignored.
1339 Also, if TZ changes twice in succession to values that do
1340 not load a tz file, tzset can dump core (see Sun bug#1225179).
1341 The following code works around these bugs. */
1345 /* Temporarily set TZ to a value that loads a tz file
1346 and that differs from tzstring. */
1348 *newenv
= (strcmp (tzstring
, set_time_zone_rule_tz1
+ 3) == 0
1349 ? set_time_zone_rule_tz2
: set_time_zone_rule_tz1
);
1355 /* The implied tzstring is unknown, so temporarily set TZ to
1356 two different values that each load a tz file. */
1357 *to
= set_time_zone_rule_tz1
;
1360 *to
= set_time_zone_rule_tz2
;
1365 /* Now TZ has the desired value, and tzset can be invoked safely. */
1372 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1373 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1374 type of object is Lisp_String). INHERIT is passed to
1375 INSERT_FROM_STRING_FUNC as the last argument. */
1378 general_insert_function (insert_func
, insert_from_string_func
,
1379 inherit
, nargs
, args
)
1380 void (*insert_func
) P_ ((unsigned char *, int));
1381 void (*insert_from_string_func
) P_ ((Lisp_Object
, int, int, int, int, int));
1383 register Lisp_Object
*args
;
1385 register int argnum
;
1386 register Lisp_Object val
;
1388 for (argnum
= 0; argnum
< nargs
; argnum
++)
1394 unsigned char workbuf
[4], *str
;
1397 if (!NILP (current_buffer
->enable_multibyte_characters
))
1398 len
= CHAR_STRING (XFASTINT (val
), workbuf
, str
);
1401 workbuf
[0] = (SINGLE_BYTE_CHAR_P (XINT (val
))
1403 : multibyte_char_to_unibyte (XINT (val
), Qnil
));
1407 (*insert_func
) (str
, len
);
1409 else if (STRINGP (val
))
1411 (*insert_from_string_func
) (val
, 0, 0,
1412 XSTRING (val
)->size
,
1413 STRING_BYTES (XSTRING (val
)),
1418 val
= wrong_type_argument (Qchar_or_string_p
, val
);
1432 /* Callers passing one argument to Finsert need not gcpro the
1433 argument "array", since the only element of the array will
1434 not be used after calling insert or insert_from_string, so
1435 we don't care if it gets trashed. */
1437 DEFUN ("insert", Finsert
, Sinsert
, 0, MANY
, 0,
1438 "Insert the arguments, either strings or characters, at point.\n\
1439 Point and before-insertion markers move forward to end up\n\
1440 after the inserted text.\n\
1441 Any other markers at the point of insertion remain before the text.\n\
1443 If the current buffer is multibyte, unibyte strings are converted\n\
1444 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1445 If the current buffer is unibyte, multibyte strings are converted\n\
1446 to unibyte for insertion.")
1449 register Lisp_Object
*args
;
1451 general_insert_function (insert
, insert_from_string
, 0, nargs
, args
);
1455 DEFUN ("insert-and-inherit", Finsert_and_inherit
, Sinsert_and_inherit
,
1457 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1458 Point and before-insertion markers move forward to end up\n\
1459 after the inserted text.\n\
1460 Any other markers at the point of insertion remain before the text.\n\
1462 If the current buffer is multibyte, unibyte strings are converted\n\
1463 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1464 If the current buffer is unibyte, multibyte strings are converted\n\
1465 to unibyte for insertion.")
1468 register Lisp_Object
*args
;
1470 general_insert_function (insert_and_inherit
, insert_from_string
, 1,
1475 DEFUN ("insert-before-markers", Finsert_before_markers
, Sinsert_before_markers
, 0, MANY
, 0,
1476 "Insert strings or characters at point, relocating markers after the text.\n\
1477 Point and markers move forward to end up after the inserted text.\n\
1479 If the current buffer is multibyte, unibyte strings are converted\n\
1480 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1481 If the current buffer is unibyte, multibyte strings are converted\n\
1482 to unibyte for insertion.")
1485 register Lisp_Object
*args
;
1487 general_insert_function (insert_before_markers
,
1488 insert_from_string_before_markers
, 0,
1493 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers
,
1494 Sinsert_and_inherit_before_markers
, 0, MANY
, 0,
1495 "Insert text at point, relocating markers and inheriting properties.\n\
1496 Point and markers move forward to end up after the inserted text.\n\
1498 If the current buffer is multibyte, unibyte strings are converted\n\
1499 to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
1500 If the current buffer is unibyte, multibyte strings are converted\n\
1501 to unibyte for insertion.")
1504 register Lisp_Object
*args
;
1506 general_insert_function (insert_before_markers_and_inherit
,
1507 insert_from_string_before_markers
, 1,
1512 DEFUN ("insert-char", Finsert_char
, Sinsert_char
, 2, 3, 0,
1513 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1514 Both arguments are required.\n\
1515 Point, and before-insertion markers, are relocated as in the function `insert'.\n\
1516 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1517 from adjoining text, if those properties are sticky.")
1518 (character
, count
, inherit
)
1519 Lisp_Object character
, count
, inherit
;
1521 register unsigned char *string
;
1522 register int strlen
;
1525 unsigned char workbuf
[4], *str
;
1527 CHECK_NUMBER (character
, 0);
1528 CHECK_NUMBER (count
, 1);
1530 if (!NILP (current_buffer
->enable_multibyte_characters
))
1531 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
1533 workbuf
[0] = XFASTINT (character
), str
= workbuf
, len
= 1;
1534 n
= XINT (count
) * len
;
1537 strlen
= min (n
, 256 * len
);
1538 string
= (unsigned char *) alloca (strlen
);
1539 for (i
= 0; i
< strlen
; i
++)
1540 string
[i
] = str
[i
% len
];
1544 if (!NILP (inherit
))
1545 insert_and_inherit (string
, strlen
);
1547 insert (string
, strlen
);
1552 if (!NILP (inherit
))
1553 insert_and_inherit (string
, n
);
1561 /* Making strings from buffer contents. */
1563 /* Return a Lisp_String containing the text of the current buffer from
1564 START to END. If text properties are in use and the current buffer
1565 has properties in the range specified, the resulting string will also
1566 have them, if PROPS is nonzero.
1568 We don't want to use plain old make_string here, because it calls
1569 make_uninit_string, which can cause the buffer arena to be
1570 compacted. make_string has no way of knowing that the data has
1571 been moved, and thus copies the wrong data into the string. This
1572 doesn't effect most of the other users of make_string, so it should
1573 be left as is. But we should use this function when conjuring
1574 buffer substrings. */
1577 make_buffer_string (start
, end
, props
)
1581 int start_byte
= CHAR_TO_BYTE (start
);
1582 int end_byte
= CHAR_TO_BYTE (end
);
1584 return make_buffer_string_both (start
, start_byte
, end
, end_byte
, props
);
1587 /* Return a Lisp_String containing the text of the current buffer from
1588 START / START_BYTE to END / END_BYTE.
1590 If text properties are in use and the current buffer
1591 has properties in the range specified, the resulting string will also
1592 have them, if PROPS is nonzero.
1594 We don't want to use plain old make_string here, because it calls
1595 make_uninit_string, which can cause the buffer arena to be
1596 compacted. make_string has no way of knowing that the data has
1597 been moved, and thus copies the wrong data into the string. This
1598 doesn't effect most of the other users of make_string, so it should
1599 be left as is. But we should use this function when conjuring
1600 buffer substrings. */
1603 make_buffer_string_both (start
, start_byte
, end
, end_byte
, props
)
1604 int start
, start_byte
, end
, end_byte
;
1607 Lisp_Object result
, tem
, tem1
;
1609 if (start
< GPT
&& GPT
< end
)
1612 if (! NILP (current_buffer
->enable_multibyte_characters
))
1613 result
= make_uninit_multibyte_string (end
- start
, end_byte
- start_byte
);
1615 result
= make_uninit_string (end
- start
);
1616 bcopy (BYTE_POS_ADDR (start_byte
), XSTRING (result
)->data
,
1617 end_byte
- start_byte
);
1619 /* If desired, update and copy the text properties. */
1620 #ifdef USE_TEXT_PROPERTIES
1623 update_buffer_properties (start
, end
);
1625 tem
= Fnext_property_change (make_number (start
), Qnil
, make_number (end
));
1626 tem1
= Ftext_properties_at (make_number (start
), Qnil
);
1628 if (XINT (tem
) != end
|| !NILP (tem1
))
1629 copy_intervals_to_string (result
, current_buffer
, start
,
1637 /* Call Vbuffer_access_fontify_functions for the range START ... END
1638 in the current buffer, if necessary. */
1641 update_buffer_properties (start
, end
)
1644 #ifdef USE_TEXT_PROPERTIES
1645 /* If this buffer has some access functions,
1646 call them, specifying the range of the buffer being accessed. */
1647 if (!NILP (Vbuffer_access_fontify_functions
))
1649 Lisp_Object args
[3];
1652 args
[0] = Qbuffer_access_fontify_functions
;
1653 XSETINT (args
[1], start
);
1654 XSETINT (args
[2], end
);
1656 /* But don't call them if we can tell that the work
1657 has already been done. */
1658 if (!NILP (Vbuffer_access_fontified_property
))
1660 tem
= Ftext_property_any (args
[1], args
[2],
1661 Vbuffer_access_fontified_property
,
1664 Frun_hook_with_args (3, args
);
1667 Frun_hook_with_args (3, args
);
1672 DEFUN ("buffer-substring", Fbuffer_substring
, Sbuffer_substring
, 2, 2, 0,
1673 "Return the contents of part of the current buffer as a string.\n\
1674 The two arguments START and END are character positions;\n\
1675 they can be in either order.\n\
1676 The string returned is multibyte if the buffer is multibyte.")
1678 Lisp_Object start
, end
;
1682 validate_region (&start
, &end
);
1686 return make_buffer_string (b
, e
, 1);
1689 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties
,
1690 Sbuffer_substring_no_properties
, 2, 2, 0,
1691 "Return the characters of part of the buffer, without the text properties.\n\
1692 The two arguments START and END are character positions;\n\
1693 they can be in either order.")
1695 Lisp_Object start
, end
;
1699 validate_region (&start
, &end
);
1703 return make_buffer_string (b
, e
, 0);
1706 DEFUN ("buffer-string", Fbuffer_string
, Sbuffer_string
, 0, 0, 0,
1707 "Return the contents of the current buffer as a string.\n\
1708 If narrowing is in effect, this function returns only the visible part\n\
1712 return make_buffer_string (BEGV
, ZV
, 1);
1715 DEFUN ("insert-buffer-substring", Finsert_buffer_substring
, Sinsert_buffer_substring
,
1717 "Insert before point a substring of the contents of buffer BUFFER.\n\
1718 BUFFER may be a buffer or a buffer name.\n\
1719 Arguments START and END are character numbers specifying the substring.\n\
1720 They default to the beginning and the end of BUFFER.")
1722 Lisp_Object buf
, start
, end
;
1724 register int b
, e
, temp
;
1725 register struct buffer
*bp
, *obuf
;
1728 buffer
= Fget_buffer (buf
);
1731 bp
= XBUFFER (buffer
);
1732 if (NILP (bp
->name
))
1733 error ("Selecting deleted buffer");
1739 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1746 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1751 temp
= b
, b
= e
, e
= temp
;
1753 if (!(BUF_BEGV (bp
) <= b
&& e
<= BUF_ZV (bp
)))
1754 args_out_of_range (start
, end
);
1756 obuf
= current_buffer
;
1757 set_buffer_internal_1 (bp
);
1758 update_buffer_properties (b
, e
);
1759 set_buffer_internal_1 (obuf
);
1761 insert_from_buffer (bp
, b
, e
- b
, 0);
1765 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings
, Scompare_buffer_substrings
,
1767 "Compare two substrings of two buffers; return result as number.\n\
1768 the value is -N if first string is less after N-1 chars,\n\
1769 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
1770 Each substring is represented as three arguments: BUFFER, START and END.\n\
1771 That makes six args in all, three for each substring.\n\n\
1772 The value of `case-fold-search' in the current buffer\n\
1773 determines whether case is significant or ignored.")
1774 (buffer1
, start1
, end1
, buffer2
, start2
, end2
)
1775 Lisp_Object buffer1
, start1
, end1
, buffer2
, start2
, end2
;
1777 register int begp1
, endp1
, begp2
, endp2
, temp
;
1778 register struct buffer
*bp1
, *bp2
;
1779 register Lisp_Object
*trt
1780 = (!NILP (current_buffer
->case_fold_search
)
1781 ? XCHAR_TABLE (current_buffer
->case_canon_table
)->contents
: 0);
1783 int i1
, i2
, i1_byte
, i2_byte
;
1785 /* Find the first buffer and its substring. */
1788 bp1
= current_buffer
;
1792 buf1
= Fget_buffer (buffer1
);
1795 bp1
= XBUFFER (buf1
);
1796 if (NILP (bp1
->name
))
1797 error ("Selecting deleted buffer");
1801 begp1
= BUF_BEGV (bp1
);
1804 CHECK_NUMBER_COERCE_MARKER (start1
, 1);
1805 begp1
= XINT (start1
);
1808 endp1
= BUF_ZV (bp1
);
1811 CHECK_NUMBER_COERCE_MARKER (end1
, 2);
1812 endp1
= XINT (end1
);
1816 temp
= begp1
, begp1
= endp1
, endp1
= temp
;
1818 if (!(BUF_BEGV (bp1
) <= begp1
1820 && endp1
<= BUF_ZV (bp1
)))
1821 args_out_of_range (start1
, end1
);
1823 /* Likewise for second substring. */
1826 bp2
= current_buffer
;
1830 buf2
= Fget_buffer (buffer2
);
1833 bp2
= XBUFFER (buf2
);
1834 if (NILP (bp2
->name
))
1835 error ("Selecting deleted buffer");
1839 begp2
= BUF_BEGV (bp2
);
1842 CHECK_NUMBER_COERCE_MARKER (start2
, 4);
1843 begp2
= XINT (start2
);
1846 endp2
= BUF_ZV (bp2
);
1849 CHECK_NUMBER_COERCE_MARKER (end2
, 5);
1850 endp2
= XINT (end2
);
1854 temp
= begp2
, begp2
= endp2
, endp2
= temp
;
1856 if (!(BUF_BEGV (bp2
) <= begp2
1858 && endp2
<= BUF_ZV (bp2
)))
1859 args_out_of_range (start2
, end2
);
1863 i1_byte
= buf_charpos_to_bytepos (bp1
, i1
);
1864 i2_byte
= buf_charpos_to_bytepos (bp2
, i2
);
1866 while (i1
< endp1
&& i2
< endp2
)
1868 /* When we find a mismatch, we must compare the
1869 characters, not just the bytes. */
1872 if (! NILP (bp1
->enable_multibyte_characters
))
1874 c1
= BUF_FETCH_MULTIBYTE_CHAR (bp1
, i1_byte
);
1875 BUF_INC_POS (bp1
, i1_byte
);
1880 c1
= BUF_FETCH_BYTE (bp1
, i1
);
1881 c1
= unibyte_char_to_multibyte (c1
);
1885 if (! NILP (bp2
->enable_multibyte_characters
))
1887 c2
= BUF_FETCH_MULTIBYTE_CHAR (bp2
, i2_byte
);
1888 BUF_INC_POS (bp2
, i2_byte
);
1893 c2
= BUF_FETCH_BYTE (bp2
, i2
);
1894 c2
= unibyte_char_to_multibyte (c2
);
1900 c1
= XINT (trt
[c1
]);
1901 c2
= XINT (trt
[c2
]);
1904 return make_number (- 1 - chars
);
1906 return make_number (chars
+ 1);
1911 /* The strings match as far as they go.
1912 If one is shorter, that one is less. */
1913 if (chars
< endp1
- begp1
)
1914 return make_number (chars
+ 1);
1915 else if (chars
< endp2
- begp2
)
1916 return make_number (- chars
- 1);
1918 /* Same length too => they are equal. */
1919 return make_number (0);
1923 subst_char_in_region_unwind (arg
)
1926 return current_buffer
->undo_list
= arg
;
1930 subst_char_in_region_unwind_1 (arg
)
1933 return current_buffer
->filename
= arg
;
1936 DEFUN ("subst-char-in-region", Fsubst_char_in_region
,
1937 Ssubst_char_in_region
, 4, 5, 0,
1938 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1939 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1940 and don't mark the buffer as really changed.\n\
1941 Both characters must have the same length of multi-byte form.")
1942 (start
, end
, fromchar
, tochar
, noundo
)
1943 Lisp_Object start
, end
, fromchar
, tochar
, noundo
;
1945 register int pos
, pos_byte
, stop
, i
, len
, end_byte
;
1947 unsigned char fromwork
[4], *fromstr
, towork
[4], *tostr
, *p
;
1948 int count
= specpdl_ptr
- specpdl
;
1949 int maybe_byte_combining
= 0;
1951 validate_region (&start
, &end
);
1952 CHECK_NUMBER (fromchar
, 2);
1953 CHECK_NUMBER (tochar
, 3);
1955 if (! NILP (current_buffer
->enable_multibyte_characters
))
1957 len
= CHAR_STRING (XFASTINT (fromchar
), fromwork
, fromstr
);
1958 if (CHAR_STRING (XFASTINT (tochar
), towork
, tostr
) != len
)
1959 error ("Characters in subst-char-in-region have different byte-lengths");
1961 /* If *TOSTR is in the range 0x80..0x9F, it may be combined
1962 with the after bytes. If it is in the range 0xA0..0xFF, it
1963 may be combined with the before bytes. */
1964 maybe_byte_combining
= !ASCII_BYTE_P (*tostr
);
1969 fromwork
[0] = XFASTINT (fromchar
), fromstr
= fromwork
;
1970 towork
[0] = XFASTINT (tochar
), tostr
= towork
;
1974 pos_byte
= CHAR_TO_BYTE (pos
);
1975 stop
= CHAR_TO_BYTE (XINT (end
));
1978 /* If we don't want undo, turn off putting stuff on the list.
1979 That's faster than getting rid of things,
1980 and it prevents even the entry for a first change.
1981 Also inhibit locking the file. */
1984 record_unwind_protect (subst_char_in_region_unwind
,
1985 current_buffer
->undo_list
);
1986 current_buffer
->undo_list
= Qt
;
1987 /* Don't do file-locking. */
1988 record_unwind_protect (subst_char_in_region_unwind_1
,
1989 current_buffer
->filename
);
1990 current_buffer
->filename
= Qnil
;
1993 if (pos_byte
< GPT_BYTE
)
1994 stop
= min (stop
, GPT_BYTE
);
1997 int pos_byte_next
= pos_byte
;
1999 if (pos_byte
>= stop
)
2001 if (pos_byte
>= end_byte
) break;
2004 p
= BYTE_POS_ADDR (pos_byte
);
2005 INC_POS (pos_byte_next
);
2006 if (pos_byte_next
- pos_byte
== len
2007 && p
[0] == fromstr
[0]
2009 || (p
[1] == fromstr
[1]
2010 && (len
== 2 || (p
[2] == fromstr
[2]
2011 && (len
== 3 || p
[3] == fromstr
[3]))))))
2015 modify_region (current_buffer
, XINT (start
), XINT (end
));
2017 if (! NILP (noundo
))
2019 if (MODIFF
- 1 == SAVE_MODIFF
)
2021 if (MODIFF
- 1 == current_buffer
->auto_save_modified
)
2022 current_buffer
->auto_save_modified
++;
2028 /* Take care of the case where the new character
2029 combines with neighboring bytes. */
2030 if (maybe_byte_combining
2031 && (CHAR_HEAD_P (*tostr
)
2032 ? ! CHAR_HEAD_P (FETCH_BYTE (pos_byte
+ 1))
2033 : (pos_byte
> BEG_BYTE
2034 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte
- 1)))))
2036 Lisp_Object tem
, string
;
2038 struct gcpro gcpro1
;
2040 tem
= current_buffer
->undo_list
;
2043 /* Make a multibyte string containing this single-byte
2045 string
= make_multibyte_string (tostr
, 1, 1);
2046 /* replace_range is less efficient, because it moves the gap,
2047 but it handles combining correctly. */
2048 replace_range (pos
, pos
+ 1, string
,
2050 pos_byte_next
= CHAR_TO_BYTE (pos
);
2051 if (pos_byte_next
> pos_byte
)
2052 /* Before combining happened. We should not increment
2053 POS. So, to cancel the later increment of POS,
2057 INC_POS (pos_byte_next
);
2059 if (! NILP (noundo
))
2060 current_buffer
->undo_list
= tem
;
2067 record_change (pos
, 1);
2068 for (i
= 0; i
< len
; i
++) *p
++ = tostr
[i
];
2071 pos_byte
= pos_byte_next
;
2076 signal_after_change (XINT (start
),
2077 XINT (end
) - XINT (start
), XINT (end
) - XINT (start
));
2079 unbind_to (count
, Qnil
);
2083 DEFUN ("translate-region", Ftranslate_region
, Stranslate_region
, 3, 3, 0,
2084 "From START to END, translate characters according to TABLE.\n\
2085 TABLE is a string; the Nth character in it is the mapping\n\
2086 for the character with code N.\n\
2087 This function does not alter multibyte characters.\n\
2088 It returns the number of characters changed.")
2092 register Lisp_Object table
;
2094 register int pos_byte
, stop
; /* Limits of the region. */
2095 register unsigned char *tt
; /* Trans table. */
2096 register int nc
; /* New character. */
2097 int cnt
; /* Number of changes made. */
2098 int size
; /* Size of translate table. */
2101 validate_region (&start
, &end
);
2102 CHECK_STRING (table
, 2);
2104 size
= STRING_BYTES (XSTRING (table
));
2105 tt
= XSTRING (table
)->data
;
2107 pos_byte
= CHAR_TO_BYTE (XINT (start
));
2108 stop
= CHAR_TO_BYTE (XINT (end
));
2109 modify_region (current_buffer
, XINT (start
), XINT (end
));
2113 for (; pos_byte
< stop
; )
2115 register unsigned char *p
= BYTE_POS_ADDR (pos_byte
);
2120 oc
= STRING_CHAR_AND_LENGTH (p
, stop
- pos_byte
, len
);
2121 pos_byte_next
= pos_byte
+ len
;
2122 if (oc
< size
&& len
== 1)
2127 /* Take care of the case where the new character
2128 combines with neighboring bytes. */
2129 if (!ASCII_BYTE_P (nc
)
2130 && (CHAR_HEAD_P (nc
)
2131 ? ! CHAR_HEAD_P (FETCH_BYTE (pos_byte
+ 1))
2132 : (pos_byte
> BEG_BYTE
2133 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte
- 1)))))
2137 string
= make_multibyte_string (tt
+ oc
, 1, 1);
2138 /* This is less efficient, because it moves the gap,
2139 but it handles combining correctly. */
2140 replace_range (pos
, pos
+ 1, string
,
2142 pos_byte_next
= CHAR_TO_BYTE (pos
);
2143 if (pos_byte_next
> pos_byte
)
2144 /* Before combining happened. We should not
2145 increment POS. So, to cancel the later
2146 increment of POS, we decrease it now. */
2149 INC_POS (pos_byte_next
);
2153 record_change (pos
, 1);
2155 signal_after_change (pos
, 1, 1);
2160 pos_byte
= pos_byte_next
;
2164 return make_number (cnt
);
2167 DEFUN ("delete-region", Fdelete_region
, Sdelete_region
, 2, 2, "r",
2168 "Delete the text between point and mark.\n\
2169 When called from a program, expects two arguments,\n\
2170 positions (integers or markers) specifying the stretch to be deleted.")
2172 Lisp_Object start
, end
;
2174 validate_region (&start
, &end
);
2175 del_range (XINT (start
), XINT (end
));
2179 DEFUN ("widen", Fwiden
, Swiden
, 0, 0, "",
2180 "Remove restrictions (narrowing) from current buffer.\n\
2181 This allows the buffer's full text to be seen and edited.")
2184 if (BEG
!= BEGV
|| Z
!= ZV
)
2185 current_buffer
->clip_changed
= 1;
2187 BEGV_BYTE
= BEG_BYTE
;
2188 SET_BUF_ZV_BOTH (current_buffer
, Z
, Z_BYTE
);
2189 /* Changing the buffer bounds invalidates any recorded current column. */
2190 invalidate_current_column ();
2194 DEFUN ("narrow-to-region", Fnarrow_to_region
, Snarrow_to_region
, 2, 2, "r",
2195 "Restrict editing in this buffer to the current region.\n\
2196 The rest of the text becomes temporarily invisible and untouchable\n\
2197 but is not deleted; if you save the buffer in a file, the invisible\n\
2198 text is included in the file. \\[widen] makes all visible again.\n\
2199 See also `save-restriction'.\n\
2201 When calling from a program, pass two arguments; positions (integers\n\
2202 or markers) bounding the text that should remain visible.")
2204 register Lisp_Object start
, end
;
2206 CHECK_NUMBER_COERCE_MARKER (start
, 0);
2207 CHECK_NUMBER_COERCE_MARKER (end
, 1);
2209 if (XINT (start
) > XINT (end
))
2212 tem
= start
; start
= end
; end
= tem
;
2215 if (!(BEG
<= XINT (start
) && XINT (start
) <= XINT (end
) && XINT (end
) <= Z
))
2216 args_out_of_range (start
, end
);
2218 if (BEGV
!= XFASTINT (start
) || ZV
!= XFASTINT (end
))
2219 current_buffer
->clip_changed
= 1;
2221 SET_BUF_BEGV (current_buffer
, XFASTINT (start
));
2222 SET_BUF_ZV (current_buffer
, XFASTINT (end
));
2223 if (PT
< XFASTINT (start
))
2224 SET_PT (XFASTINT (start
));
2225 if (PT
> XFASTINT (end
))
2226 SET_PT (XFASTINT (end
));
2227 /* Changing the buffer bounds invalidates any recorded current column. */
2228 invalidate_current_column ();
2233 save_restriction_save ()
2235 register Lisp_Object bottom
, top
;
2236 /* Note: I tried using markers here, but it does not win
2237 because insertion at the end of the saved region
2238 does not advance mh and is considered "outside" the saved region. */
2239 XSETFASTINT (bottom
, BEGV
- BEG
);
2240 XSETFASTINT (top
, Z
- ZV
);
2242 return Fcons (Fcurrent_buffer (), Fcons (bottom
, top
));
2246 save_restriction_restore (data
)
2249 register struct buffer
*buf
;
2250 register int newhead
, newtail
;
2251 register Lisp_Object tem
;
2254 buf
= XBUFFER (XCONS (data
)->car
);
2256 data
= XCONS (data
)->cdr
;
2258 tem
= XCONS (data
)->car
;
2259 newhead
= XINT (tem
);
2260 tem
= XCONS (data
)->cdr
;
2261 newtail
= XINT (tem
);
2262 if (newhead
+ newtail
> BUF_Z (buf
) - BUF_BEG (buf
))
2268 obegv
= BUF_BEGV (buf
);
2271 SET_BUF_BEGV (buf
, BUF_BEG (buf
) + newhead
);
2272 SET_BUF_ZV (buf
, BUF_Z (buf
) - newtail
);
2274 if (obegv
!= BUF_BEGV (buf
) || ozv
!= BUF_ZV (buf
))
2275 current_buffer
->clip_changed
= 1;
2277 /* If point is outside the new visible range, move it inside. */
2278 SET_BUF_PT_BOTH (buf
,
2279 clip_to_bounds (BUF_BEGV (buf
), BUF_PT (buf
), BUF_ZV (buf
)),
2280 clip_to_bounds (BUF_BEGV_BYTE (buf
), BUF_PT_BYTE (buf
),
2281 BUF_ZV_BYTE (buf
)));
2286 DEFUN ("save-restriction", Fsave_restriction
, Ssave_restriction
, 0, UNEVALLED
, 0,
2287 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2288 The buffer's restrictions make parts of the beginning and end invisible.\n\
2289 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2290 This special form, `save-restriction', saves the current buffer's restrictions\n\
2291 when it is entered, and restores them when it is exited.\n\
2292 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2293 The old restrictions settings are restored\n\
2294 even in case of abnormal exit (throw or error).\n\
2296 The value returned is the value of the last form in BODY.\n\
2298 `save-restriction' can get confused if, within the BODY, you widen\n\
2299 and then make changes outside the area within the saved restrictions.\n\
2300 See Info node `(elisp)Narrowing' for details and an appropriate technique.\n\
2302 Note: if you are using both `save-excursion' and `save-restriction',\n\
2303 use `save-excursion' outermost:\n\
2304 (save-excursion (save-restriction ...))")
2308 register Lisp_Object val
;
2309 int count
= specpdl_ptr
- specpdl
;
2311 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
2312 val
= Fprogn (body
);
2313 return unbind_to (count
, val
);
2316 /* Buffer for the most recent text displayed by Fmessage. */
2317 static char *message_text
;
2319 /* Allocated length of that buffer. */
2320 static int message_length
;
2322 DEFUN ("message", Fmessage
, Smessage
, 1, MANY
, 0,
2323 "Print a one-line message at the bottom of the screen.\n\
2324 The first argument is a format control string, and the rest are data\n\
2325 to be formatted under control of the string. See `format' for details.\n\
2327 If the first argument is nil, clear any existing message; let the\n\
2328 minibuffer contents show.")
2340 register Lisp_Object val
;
2341 val
= Fformat (nargs
, args
);
2342 /* Copy the data so that it won't move when we GC. */
2345 message_text
= (char *)xmalloc (80);
2346 message_length
= 80;
2348 if (STRING_BYTES (XSTRING (val
)) > message_length
)
2350 message_length
= STRING_BYTES (XSTRING (val
));
2351 message_text
= (char *)xrealloc (message_text
, message_length
);
2353 bcopy (XSTRING (val
)->data
, message_text
, STRING_BYTES (XSTRING (val
)));
2354 message2 (message_text
, STRING_BYTES (XSTRING (val
)),
2355 STRING_MULTIBYTE (val
));
2360 DEFUN ("message-box", Fmessage_box
, Smessage_box
, 1, MANY
, 0,
2361 "Display a message, in a dialog box if possible.\n\
2362 If a dialog box is not available, use the echo area.\n\
2363 The first argument is a format control string, and the rest are data\n\
2364 to be formatted under control of the string. See `format' for details.\n\
2366 If the first argument is nil, clear any existing message; let the\n\
2367 minibuffer contents show.")
2379 register Lisp_Object val
;
2380 val
= Fformat (nargs
, args
);
2383 Lisp_Object pane
, menu
, obj
;
2384 struct gcpro gcpro1
;
2385 pane
= Fcons (Fcons (build_string ("OK"), Qt
), Qnil
);
2387 menu
= Fcons (val
, pane
);
2388 obj
= Fx_popup_dialog (Qt
, menu
);
2392 #else /* not HAVE_MENUS */
2393 /* Copy the data so that it won't move when we GC. */
2396 message_text
= (char *)xmalloc (80);
2397 message_length
= 80;
2399 if (STRING_BYTES (XSTRING (val
)) > message_length
)
2401 message_length
= STRING_BYTES (XSTRING (val
));
2402 message_text
= (char *)xrealloc (message_text
, message_length
);
2404 bcopy (XSTRING (val
)->data
, message_text
, STRING_BYTES (XSTRING (val
)));
2405 message2 (message_text
, STRING_BYTES (XSTRING (val
)),
2406 STRING_MULTIBYTE (val
));
2408 #endif /* not HAVE_MENUS */
2412 extern Lisp_Object last_nonmenu_event
;
2415 DEFUN ("message-or-box", Fmessage_or_box
, Smessage_or_box
, 1, MANY
, 0,
2416 "Display a message in a dialog box or in the echo area.\n\
2417 If this command was invoked with the mouse, use a dialog box.\n\
2418 Otherwise, use the echo area.\n\
2419 The first argument is a format control string, and the rest are data\n\
2420 to be formatted under control of the string. See `format' for details.\n\
2422 If the first argument is nil, clear any existing message; let the\n\
2423 minibuffer contents show.")
2429 if (NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2430 return Fmessage_box (nargs
, args
);
2432 return Fmessage (nargs
, args
);
2435 DEFUN ("current-message", Fcurrent_message
, Scurrent_message
, 0, 0, 0,
2436 "Return the string currently displayed in the echo area, or nil if none.")
2439 return (echo_area_glyphs
2440 ? make_string (echo_area_glyphs
, echo_area_glyphs_length
)
2444 /* Number of bytes that STRING will occupy when put into the result.
2445 MULTIBYTE is nonzero if the result should be multibyte. */
2447 #define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \
2448 (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \
2449 ? count_size_as_multibyte (XSTRING (STRING)->data, \
2450 STRING_BYTES (XSTRING (STRING))) \
2451 : STRING_BYTES (XSTRING (STRING)))
2453 DEFUN ("format", Fformat
, Sformat
, 1, MANY
, 0,
2454 "Format a string out of a control-string and arguments.\n\
2455 The first argument is a control string.\n\
2456 The other arguments are substituted into it to make the result, a string.\n\
2457 It may contain %-sequences meaning to substitute the next argument.\n\
2458 %s means print a string argument. Actually, prints any object, with `princ'.\n\
2459 %d means print as number in decimal (%o octal, %x hex).\n\
2460 %e means print a number in exponential notation.\n\
2461 %f means print a number in decimal-point notation.\n\
2462 %g means print a number in exponential notation\n\
2463 or decimal-point notation, whichever uses fewer characters.\n\
2464 %c means print a number as a single character.\n\
2465 %S means print any object as an s-expression (using `prin1').\n\
2466 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2467 Use %% to put a single % into the output.")
2470 register Lisp_Object
*args
;
2472 register int n
; /* The number of the next arg to substitute */
2473 register int total
; /* An estimate of the final length */
2475 register unsigned char *format
, *end
;
2477 /* Nonzero if the output should be a multibyte string,
2478 which is true if any of the inputs is one. */
2480 /* When we make a multibyte string, we must pay attention to the
2481 byte combining problem, i.e., a byte may be combined with a
2482 multibyte charcter of the previous string. This flag tells if we
2483 must consider such a situation or not. */
2484 int maybe_combine_byte
;
2485 unsigned char *this_format
;
2489 extern char *index ();
2491 /* It should not be necessary to GCPRO ARGS, because
2492 the caller in the interpreter should take care of that. */
2494 /* Try to determine whether the result should be multibyte.
2495 This is not always right; sometimes the result needs to be multibyte
2496 because of an object that we will pass through prin1,
2497 and in that case, we won't know it here. */
2498 for (n
= 0; n
< nargs
; n
++)
2499 if (STRINGP (args
[n
]) && STRING_MULTIBYTE (args
[n
]))
2502 CHECK_STRING (args
[0], 0);
2504 /* If we start out planning a unibyte result,
2505 and later find it has to be multibyte, we jump back to retry. */
2508 format
= XSTRING (args
[0])->data
;
2509 end
= format
+ STRING_BYTES (XSTRING (args
[0]));
2512 /* Make room in result for all the non-%-codes in the control string. */
2513 total
= 5 + CONVERTED_BYTE_SIZE (multibyte
, args
[0]);
2515 /* Add to TOTAL enough space to hold the converted arguments. */
2518 while (format
!= end
)
2519 if (*format
++ == '%')
2521 int minlen
, thissize
= 0;
2522 unsigned char *this_format_start
= format
- 1;
2524 /* Process a numeric arg and skip it. */
2525 minlen
= atoi (format
);
2529 while ((*format
>= '0' && *format
<= '9')
2530 || *format
== '-' || *format
== ' ' || *format
== '.')
2533 if (format
- this_format_start
+ 1 > longest_format
)
2534 longest_format
= format
- this_format_start
+ 1;
2537 error ("Format string ends in middle of format specifier");
2540 else if (++n
>= nargs
)
2541 error ("Not enough arguments for format string");
2542 else if (*format
== 'S')
2544 /* For `S', prin1 the argument and then treat like a string. */
2545 register Lisp_Object tem
;
2546 tem
= Fprin1_to_string (args
[n
], Qnil
);
2547 if (STRING_MULTIBYTE (tem
) && ! multibyte
)
2555 else if (SYMBOLP (args
[n
]))
2557 XSETSTRING (args
[n
], XSYMBOL (args
[n
])->name
);
2558 if (STRING_MULTIBYTE (args
[n
]) && ! multibyte
)
2565 else if (STRINGP (args
[n
]))
2568 if (*format
!= 's' && *format
!= 'S')
2569 error ("Format specifier doesn't match argument type");
2570 thissize
= CONVERTED_BYTE_SIZE (multibyte
, args
[n
]);
2572 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
2573 else if (INTEGERP (args
[n
]) && *format
!= 's')
2575 #ifdef LISP_FLOAT_TYPE
2576 /* The following loop assumes the Lisp type indicates
2577 the proper way to pass the argument.
2578 So make sure we have a flonum if the argument should
2580 if (*format
== 'e' || *format
== 'f' || *format
== 'g')
2581 args
[n
] = Ffloat (args
[n
]);
2584 if (*format
!= 'd' && *format
!= 'o' && *format
!= 'x'
2585 && *format
!= 'i' && *format
!= 'X' && *format
!= 'c')
2586 error ("Invalid format operation %%%c", *format
);
2590 && (! SINGLE_BYTE_CHAR_P (XINT (args
[n
]))
2591 || XINT (args
[n
]) == 0))
2598 args
[n
] = Fchar_to_string (args
[n
]);
2599 thissize
= STRING_BYTES (XSTRING (args
[n
]));
2602 #ifdef LISP_FLOAT_TYPE
2603 else if (FLOATP (args
[n
]) && *format
!= 's')
2605 if (! (*format
== 'e' || *format
== 'f' || *format
== 'g'))
2606 args
[n
] = Ftruncate (args
[n
], Qnil
);
2612 /* Anything but a string, convert to a string using princ. */
2613 register Lisp_Object tem
;
2614 tem
= Fprin1_to_string (args
[n
], Qt
);
2615 if (STRING_MULTIBYTE (tem
) & ! multibyte
)
2624 if (thissize
< minlen
)
2627 total
+= thissize
+ 4;
2630 /* Now we can no longer jump to retry.
2631 TOTAL and LONGEST_FORMAT are known for certain. */
2633 this_format
= (unsigned char *) alloca (longest_format
+ 1);
2635 /* Allocate the space for the result.
2636 Note that TOTAL is an overestimate. */
2638 buf
= (char *) alloca (total
+ 1);
2640 buf
= (char *) xmalloc (total
+ 1);
2646 /* Scan the format and store result in BUF. */
2647 format
= XSTRING (args
[0])->data
;
2648 maybe_combine_byte
= 0;
2649 while (format
!= end
)
2655 unsigned char *this_format_start
= format
;
2659 /* Process a numeric arg and skip it. */
2660 minlen
= atoi (format
);
2662 minlen
= - minlen
, negative
= 1;
2664 while ((*format
>= '0' && *format
<= '9')
2665 || *format
== '-' || *format
== ' ' || *format
== '.')
2668 if (*format
++ == '%')
2677 if (STRINGP (args
[n
]))
2679 int padding
, nbytes
;
2680 int width
= strwidth (XSTRING (args
[n
])->data
,
2681 STRING_BYTES (XSTRING (args
[n
])));
2683 /* If spec requires it, pad on right with spaces. */
2684 padding
= minlen
- width
;
2686 while (padding
-- > 0)
2694 && !ASCII_BYTE_P (*((unsigned char *) p
- 1))
2695 && STRING_MULTIBYTE (args
[n
])
2696 && !CHAR_HEAD_P (XSTRING (args
[n
])->data
[0]))
2697 maybe_combine_byte
= 1;
2698 nbytes
= copy_text (XSTRING (args
[n
])->data
, p
,
2699 STRING_BYTES (XSTRING (args
[n
])),
2700 STRING_MULTIBYTE (args
[n
]), multibyte
);
2702 nchars
+= XSTRING (args
[n
])->size
;
2705 while (padding
-- > 0)
2711 else if (INTEGERP (args
[n
]) || FLOATP (args
[n
]))
2715 bcopy (this_format_start
, this_format
,
2716 format
- this_format_start
);
2717 this_format
[format
- this_format_start
] = 0;
2719 if (INTEGERP (args
[n
]))
2720 sprintf (p
, this_format
, XINT (args
[n
]));
2722 sprintf (p
, this_format
, XFLOAT (args
[n
])->data
);
2726 && !ASCII_BYTE_P (*((unsigned char *) p
- 1))
2727 && !CHAR_HEAD_P (*((unsigned char *) p
)))
2728 maybe_combine_byte
= 1;
2729 this_nchars
= strlen (p
);
2731 nchars
+= this_nchars
;
2734 else if (STRING_MULTIBYTE (args
[0]))
2736 /* Copy a whole multibyte character. */
2739 && !ASCII_BYTE_P (*((unsigned char *) p
- 1))
2740 && !CHAR_HEAD_P (*format
))
2741 maybe_combine_byte
= 1;
2743 while (! CHAR_HEAD_P (*format
)) *p
++ = *format
++;
2748 /* Convert a single-byte character to multibyte. */
2749 int len
= copy_text (format
, p
, 1, 0, 1);
2756 *p
++ = *format
++, nchars
++;
2759 if (maybe_combine_byte
)
2760 nchars
= multibyte_chars_in_text (buf
, p
- buf
);
2761 val
= make_specified_string (buf
, nchars
, p
- buf
, multibyte
);
2763 /* If we allocated BUF with malloc, free it too. */
2773 format1 (string1
, arg0
, arg1
, arg2
, arg3
, arg4
)
2774 EMACS_INT arg0
, arg1
, arg2
, arg3
, arg4
;
2788 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, (char **) args
);
2790 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, &string1
+ 1);
2792 return build_string (buf
);
2795 DEFUN ("char-equal", Fchar_equal
, Schar_equal
, 2, 2, 0,
2796 "Return t if two characters match, optionally ignoring case.\n\
2797 Both arguments must be characters (i.e. integers).\n\
2798 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2800 register Lisp_Object c1
, c2
;
2803 CHECK_NUMBER (c1
, 0);
2804 CHECK_NUMBER (c2
, 1);
2806 if (XINT (c1
) == XINT (c2
))
2808 if (NILP (current_buffer
->case_fold_search
))
2811 /* Do these in separate statements,
2812 then compare the variables.
2813 because of the way DOWNCASE uses temp variables. */
2814 i1
= DOWNCASE (XFASTINT (c1
));
2815 i2
= DOWNCASE (XFASTINT (c2
));
2816 return (i1
== i2
? Qt
: Qnil
);
2819 /* Transpose the markers in two regions of the current buffer, and
2820 adjust the ones between them if necessary (i.e.: if the regions
2823 START1, END1 are the character positions of the first region.
2824 START1_BYTE, END1_BYTE are the byte positions.
2825 START2, END2 are the character positions of the second region.
2826 START2_BYTE, END2_BYTE are the byte positions.
2828 Traverses the entire marker list of the buffer to do so, adding an
2829 appropriate amount to some, subtracting from some, and leaving the
2830 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2832 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
2835 transpose_markers (start1
, end1
, start2
, end2
,
2836 start1_byte
, end1_byte
, start2_byte
, end2_byte
)
2837 register int start1
, end1
, start2
, end2
;
2838 register int start1_byte
, end1_byte
, start2_byte
, end2_byte
;
2840 register int amt1
, amt1_byte
, amt2
, amt2_byte
, diff
, diff_byte
, mpos
;
2841 register Lisp_Object marker
;
2843 /* Update point as if it were a marker. */
2847 TEMP_SET_PT_BOTH (PT
+ (end2
- end1
),
2848 PT_BYTE
+ (end2_byte
- end1_byte
));
2849 else if (PT
< start2
)
2850 TEMP_SET_PT_BOTH (PT
+ (end2
- start2
) - (end1
- start1
),
2851 (PT_BYTE
+ (end2_byte
- start2_byte
)
2852 - (end1_byte
- start1_byte
)));
2854 TEMP_SET_PT_BOTH (PT
- (start2
- start1
),
2855 PT_BYTE
- (start2_byte
- start1_byte
));
2857 /* We used to adjust the endpoints here to account for the gap, but that
2858 isn't good enough. Even if we assume the caller has tried to move the
2859 gap out of our way, it might still be at start1 exactly, for example;
2860 and that places it `inside' the interval, for our purposes. The amount
2861 of adjustment is nontrivial if there's a `denormalized' marker whose
2862 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2863 the dirty work to Fmarker_position, below. */
2865 /* The difference between the region's lengths */
2866 diff
= (end2
- start2
) - (end1
- start1
);
2867 diff_byte
= (end2_byte
- start2_byte
) - (end1_byte
- start1_byte
);
2869 /* For shifting each marker in a region by the length of the other
2870 region plus the distance between the regions. */
2871 amt1
= (end2
- start2
) + (start2
- end1
);
2872 amt2
= (end1
- start1
) + (start2
- end1
);
2873 amt1_byte
= (end2_byte
- start2_byte
) + (start2_byte
- end1_byte
);
2874 amt2_byte
= (end1_byte
- start1_byte
) + (start2_byte
- end1_byte
);
2876 for (marker
= BUF_MARKERS (current_buffer
); !NILP (marker
);
2877 marker
= XMARKER (marker
)->chain
)
2879 mpos
= marker_byte_position (marker
);
2880 if (mpos
>= start1_byte
&& mpos
< end2_byte
)
2882 if (mpos
< end1_byte
)
2884 else if (mpos
< start2_byte
)
2888 XMARKER (marker
)->bytepos
= mpos
;
2890 mpos
= XMARKER (marker
)->charpos
;
2891 if (mpos
>= start1
&& mpos
< end2
)
2895 else if (mpos
< start2
)
2900 XMARKER (marker
)->charpos
= mpos
;
2904 DEFUN ("transpose-regions", Ftranspose_regions
, Stranspose_regions
, 4, 5, 0,
2905 "Transpose region START1 to END1 with START2 to END2.\n\
2906 The regions may not be overlapping, because the size of the buffer is\n\
2907 never changed in a transposition.\n\
2909 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update\n\
2910 any markers that happen to be located in the regions.\n\
2912 Transposing beyond buffer boundaries is an error.")
2913 (startr1
, endr1
, startr2
, endr2
, leave_markers
)
2914 Lisp_Object startr1
, endr1
, startr2
, endr2
, leave_markers
;
2916 register int start1
, end1
, start2
, end2
;
2917 int start1_byte
, start2_byte
, len1_byte
, len2_byte
;
2918 int gap
, len1
, len_mid
, len2
;
2919 unsigned char *start1_addr
, *start2_addr
, *temp
;
2920 int combined_before_bytes_1
, combined_after_bytes_1
;
2921 int combined_before_bytes_2
, combined_after_bytes_2
;
2922 struct gcpro gcpro1
, gcpro2
;
2924 #ifdef USE_TEXT_PROPERTIES
2925 INTERVAL cur_intv
, tmp_interval1
, tmp_interval_mid
, tmp_interval2
;
2926 cur_intv
= BUF_INTERVALS (current_buffer
);
2927 #endif /* USE_TEXT_PROPERTIES */
2929 validate_region (&startr1
, &endr1
);
2930 validate_region (&startr2
, &endr2
);
2932 start1
= XFASTINT (startr1
);
2933 end1
= XFASTINT (endr1
);
2934 start2
= XFASTINT (startr2
);
2935 end2
= XFASTINT (endr2
);
2938 /* Swap the regions if they're reversed. */
2941 register int glumph
= start1
;
2949 len1
= end1
- start1
;
2950 len2
= end2
- start2
;
2953 error ("Transposed regions overlap");
2954 else if (start1
== end1
|| start2
== end2
)
2955 error ("Transposed region has length 0");
2957 /* The possibilities are:
2958 1. Adjacent (contiguous) regions, or separate but equal regions
2959 (no, really equal, in this case!), or
2960 2. Separate regions of unequal size.
2962 The worst case is usually No. 2. It means that (aside from
2963 potential need for getting the gap out of the way), there also
2964 needs to be a shifting of the text between the two regions. So
2965 if they are spread far apart, we are that much slower... sigh. */
2967 /* It must be pointed out that the really studly thing to do would
2968 be not to move the gap at all, but to leave it in place and work
2969 around it if necessary. This would be extremely efficient,
2970 especially considering that people are likely to do
2971 transpositions near where they are working interactively, which
2972 is exactly where the gap would be found. However, such code
2973 would be much harder to write and to read. So, if you are
2974 reading this comment and are feeling squirrely, by all means have
2975 a go! I just didn't feel like doing it, so I will simply move
2976 the gap the minimum distance to get it out of the way, and then
2977 deal with an unbroken array. */
2979 /* Make sure the gap won't interfere, by moving it out of the text
2980 we will operate on. */
2981 if (start1
< gap
&& gap
< end2
)
2983 if (gap
- start1
< end2
- gap
)
2989 start1_byte
= CHAR_TO_BYTE (start1
);
2990 start2_byte
= CHAR_TO_BYTE (start2
);
2991 len1_byte
= CHAR_TO_BYTE (end1
) - start1_byte
;
2992 len2_byte
= CHAR_TO_BYTE (end2
) - start2_byte
;
2996 combined_before_bytes_2
2997 = count_combining_before (BYTE_POS_ADDR (start2_byte
),
2998 len2_byte
, start1
, start1_byte
);
2999 combined_before_bytes_1
3000 = count_combining_before (BYTE_POS_ADDR (start1_byte
),
3001 len1_byte
, end2
, start2_byte
+ len2_byte
);
3002 combined_after_bytes_1
3003 = count_combining_after (BYTE_POS_ADDR (start1_byte
),
3004 len1_byte
, end2
, start2_byte
+ len2_byte
);
3005 combined_after_bytes_2
= 0;
3009 combined_before_bytes_2
3010 = count_combining_before (BYTE_POS_ADDR (start2_byte
),
3011 len2_byte
, start1
, start1_byte
);
3012 combined_before_bytes_1
3013 = count_combining_before (BYTE_POS_ADDR (start1_byte
),
3014 len1_byte
, start2
, start2_byte
);
3015 combined_after_bytes_2
3016 = count_combining_after (BYTE_POS_ADDR (start2_byte
),
3017 len2_byte
, end1
, start1_byte
+ len1_byte
);
3018 combined_after_bytes_1
3019 = count_combining_after (BYTE_POS_ADDR (start1_byte
),
3020 len1_byte
, end2
, start2_byte
+ len2_byte
);
3023 /* If any combining is going to happen, do this the stupid way,
3024 because replace handles combining properly. */
3025 if (combined_before_bytes_1
|| combined_before_bytes_2
3026 || combined_after_bytes_1
|| combined_after_bytes_2
)
3028 Lisp_Object text1
, text2
;
3030 text1
= text2
= Qnil
;
3031 GCPRO2 (text1
, text2
);
3033 text1
= make_buffer_string_both (start1
, start1_byte
,
3034 end1
, start1_byte
+ len1_byte
, 1);
3035 text2
= make_buffer_string_both (start2
, start2_byte
,
3036 end2
, start2_byte
+ len2_byte
, 1);
3038 transpose_markers (start1
, end1
, start2
, end2
,
3039 start1_byte
, start1_byte
+ len1_byte
,
3040 start2_byte
, start2_byte
+ len2_byte
);
3042 replace_range (start2
, end2
, text1
, 1, 0, 0);
3043 replace_range (start1
, end1
, text2
, 1, 0, 0);
3049 /* Hmmm... how about checking to see if the gap is large
3050 enough to use as the temporary storage? That would avoid an
3051 allocation... interesting. Later, don't fool with it now. */
3053 /* Working without memmove, for portability (sigh), so must be
3054 careful of overlapping subsections of the array... */
3056 if (end1
== start2
) /* adjacent regions */
3058 modify_region (current_buffer
, start1
, end2
);
3059 record_change (start1
, len1
+ len2
);
3061 #ifdef USE_TEXT_PROPERTIES
3062 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
3063 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
3064 Fset_text_properties (make_number (start1
), make_number (end2
),
3066 #endif /* USE_TEXT_PROPERTIES */
3068 /* First region smaller than second. */
3069 if (len1_byte
< len2_byte
)
3071 /* We use alloca only if it is small,
3072 because we want to avoid stack overflow. */
3073 if (len2_byte
> 20000)
3074 temp
= (unsigned char *) xmalloc (len2_byte
);
3076 temp
= (unsigned char *) alloca (len2_byte
);
3078 /* Don't precompute these addresses. We have to compute them
3079 at the last minute, because the relocating allocator might
3080 have moved the buffer around during the xmalloc. */
3081 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3082 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3084 bcopy (start2_addr
, temp
, len2_byte
);
3085 bcopy (start1_addr
, start1_addr
+ len2_byte
, len1_byte
);
3086 bcopy (temp
, start1_addr
, len2_byte
);
3087 if (len2_byte
> 20000)
3091 /* First region not smaller than second. */
3093 if (len1_byte
> 20000)
3094 temp
= (unsigned char *) xmalloc (len1_byte
);
3096 temp
= (unsigned char *) alloca (len1_byte
);
3097 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3098 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3099 bcopy (start1_addr
, temp
, len1_byte
);
3100 bcopy (start2_addr
, start1_addr
, len2_byte
);
3101 bcopy (temp
, start1_addr
+ len2_byte
, len1_byte
);
3102 if (len1_byte
> 20000)
3105 #ifdef USE_TEXT_PROPERTIES
3106 graft_intervals_into_buffer (tmp_interval1
, start1
+ len2
,
3107 len1
, current_buffer
, 0);
3108 graft_intervals_into_buffer (tmp_interval2
, start1
,
3109 len2
, current_buffer
, 0);
3110 #endif /* USE_TEXT_PROPERTIES */
3112 /* Non-adjacent regions, because end1 != start2, bleagh... */
3115 len_mid
= start2_byte
- (start1_byte
+ len1_byte
);
3117 if (len1_byte
== len2_byte
)
3118 /* Regions are same size, though, how nice. */
3120 modify_region (current_buffer
, start1
, end1
);
3121 modify_region (current_buffer
, start2
, end2
);
3122 record_change (start1
, len1
);
3123 record_change (start2
, len2
);
3124 #ifdef USE_TEXT_PROPERTIES
3125 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
3126 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
3127 Fset_text_properties (make_number (start1
), make_number (end1
),
3129 Fset_text_properties (make_number (start2
), make_number (end2
),
3131 #endif /* USE_TEXT_PROPERTIES */
3133 if (len1_byte
> 20000)
3134 temp
= (unsigned char *) xmalloc (len1_byte
);
3136 temp
= (unsigned char *) alloca (len1_byte
);
3137 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3138 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3139 bcopy (start1_addr
, temp
, len1_byte
);
3140 bcopy (start2_addr
, start1_addr
, len2_byte
);
3141 bcopy (temp
, start2_addr
, len1_byte
);
3142 if (len1_byte
> 20000)
3144 #ifdef USE_TEXT_PROPERTIES
3145 graft_intervals_into_buffer (tmp_interval1
, start2
,
3146 len1
, current_buffer
, 0);
3147 graft_intervals_into_buffer (tmp_interval2
, start1
,
3148 len2
, current_buffer
, 0);
3149 #endif /* USE_TEXT_PROPERTIES */
3152 else if (len1_byte
< len2_byte
) /* Second region larger than first */
3153 /* Non-adjacent & unequal size, area between must also be shifted. */
3155 modify_region (current_buffer
, start1
, end2
);
3156 record_change (start1
, (end2
- start1
));
3157 #ifdef USE_TEXT_PROPERTIES
3158 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
3159 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
3160 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
3161 Fset_text_properties (make_number (start1
), make_number (end2
),
3163 #endif /* USE_TEXT_PROPERTIES */
3165 /* holds region 2 */
3166 if (len2_byte
> 20000)
3167 temp
= (unsigned char *) xmalloc (len2_byte
);
3169 temp
= (unsigned char *) alloca (len2_byte
);
3170 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3171 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3172 bcopy (start2_addr
, temp
, len2_byte
);
3173 bcopy (start1_addr
, start1_addr
+ len_mid
+ len2_byte
, len1_byte
);
3174 safe_bcopy (start1_addr
+ len1_byte
, start1_addr
+ len2_byte
, len_mid
);
3175 bcopy (temp
, start1_addr
, len2_byte
);
3176 if (len2_byte
> 20000)
3178 #ifdef USE_TEXT_PROPERTIES
3179 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
3180 len1
, current_buffer
, 0);
3181 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
3182 len_mid
, current_buffer
, 0);
3183 graft_intervals_into_buffer (tmp_interval2
, start1
,
3184 len2
, current_buffer
, 0);
3185 #endif /* USE_TEXT_PROPERTIES */
3188 /* Second region smaller than first. */
3190 record_change (start1
, (end2
- start1
));
3191 modify_region (current_buffer
, start1
, end2
);
3193 #ifdef USE_TEXT_PROPERTIES
3194 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
3195 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
3196 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
3197 Fset_text_properties (make_number (start1
), make_number (end2
),
3199 #endif /* USE_TEXT_PROPERTIES */
3201 /* holds region 1 */
3202 if (len1_byte
> 20000)
3203 temp
= (unsigned char *) xmalloc (len1_byte
);
3205 temp
= (unsigned char *) alloca (len1_byte
);
3206 start1_addr
= BYTE_POS_ADDR (start1_byte
);
3207 start2_addr
= BYTE_POS_ADDR (start2_byte
);
3208 bcopy (start1_addr
, temp
, len1_byte
);
3209 bcopy (start2_addr
, start1_addr
, len2_byte
);
3210 bcopy (start1_addr
+ len1_byte
, start1_addr
+ len2_byte
, len_mid
);
3211 bcopy (temp
, start1_addr
+ len2_byte
+ len_mid
, len1_byte
);
3212 if (len1_byte
> 20000)
3214 #ifdef USE_TEXT_PROPERTIES
3215 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
3216 len1
, current_buffer
, 0);
3217 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
3218 len_mid
, current_buffer
, 0);
3219 graft_intervals_into_buffer (tmp_interval2
, start1
,
3220 len2
, current_buffer
, 0);
3221 #endif /* USE_TEXT_PROPERTIES */
3225 /* When doing multiple transpositions, it might be nice
3226 to optimize this. Perhaps the markers in any one buffer
3227 should be organized in some sorted data tree. */
3228 if (NILP (leave_markers
))
3230 transpose_markers (start1
, end1
, start2
, end2
,
3231 start1_byte
, start1_byte
+ len1_byte
,
3232 start2_byte
, start2_byte
+ len2_byte
);
3233 fix_overlays_in_range (start1
, end2
);
3245 Qbuffer_access_fontify_functions
3246 = intern ("buffer-access-fontify-functions");
3247 staticpro (&Qbuffer_access_fontify_functions
);
3249 DEFVAR_LISP ("buffer-access-fontify-functions",
3250 &Vbuffer_access_fontify_functions
,
3251 "List of functions called by `buffer-substring' to fontify if necessary.\n\
3252 Each function is called with two arguments which specify the range\n\
3253 of the buffer being accessed.");
3254 Vbuffer_access_fontify_functions
= Qnil
;
3258 extern Lisp_Object Vprin1_to_string_buffer
;
3259 obuf
= Fcurrent_buffer ();
3260 /* Do this here, because init_buffer_once is too early--it won't work. */
3261 Fset_buffer (Vprin1_to_string_buffer
);
3262 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
3263 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
3268 DEFVAR_LISP ("buffer-access-fontified-property",
3269 &Vbuffer_access_fontified_property
,
3270 "Property which (if non-nil) indicates text has been fontified.\n\
3271 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
3272 functions if all the text being accessed has this property.");
3273 Vbuffer_access_fontified_property
= Qnil
;
3275 DEFVAR_LISP ("system-name", &Vsystem_name
,
3276 "The name of the machine Emacs is running on.");
3278 DEFVAR_LISP ("user-full-name", &Vuser_full_name
,
3279 "The full name of the user logged in.");
3281 DEFVAR_LISP ("user-login-name", &Vuser_login_name
,
3282 "The user's name, taken from environment variables if possible.");
3284 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name
,
3285 "The user's name, based upon the real uid only.");
3287 defsubr (&Schar_equal
);
3288 defsubr (&Sgoto_char
);
3289 defsubr (&Sstring_to_char
);
3290 defsubr (&Schar_to_string
);
3291 defsubr (&Sbuffer_substring
);
3292 defsubr (&Sbuffer_substring_no_properties
);
3293 defsubr (&Sbuffer_string
);
3295 defsubr (&Spoint_marker
);
3296 defsubr (&Smark_marker
);
3298 defsubr (&Sregion_beginning
);
3299 defsubr (&Sregion_end
);
3301 defsubr (&Sline_beginning_position
);
3302 defsubr (&Sline_end_position
);
3304 /* defsubr (&Smark); */
3305 /* defsubr (&Sset_mark); */
3306 defsubr (&Ssave_excursion
);
3307 defsubr (&Ssave_current_buffer
);
3309 defsubr (&Sbufsize
);
3310 defsubr (&Spoint_max
);
3311 defsubr (&Spoint_min
);
3312 defsubr (&Spoint_min_marker
);
3313 defsubr (&Spoint_max_marker
);
3314 defsubr (&Sgap_position
);
3315 defsubr (&Sgap_size
);
3316 defsubr (&Sposition_bytes
);
3317 defsubr (&Sbyte_to_position
);
3323 defsubr (&Sfollowing_char
);
3324 defsubr (&Sprevious_char
);
3325 defsubr (&Schar_after
);
3326 defsubr (&Schar_before
);
3328 defsubr (&Sinsert_before_markers
);
3329 defsubr (&Sinsert_and_inherit
);
3330 defsubr (&Sinsert_and_inherit_before_markers
);
3331 defsubr (&Sinsert_char
);
3333 defsubr (&Suser_login_name
);
3334 defsubr (&Suser_real_login_name
);
3335 defsubr (&Suser_uid
);
3336 defsubr (&Suser_real_uid
);
3337 defsubr (&Suser_full_name
);
3338 defsubr (&Semacs_pid
);
3339 defsubr (&Scurrent_time
);
3340 defsubr (&Sformat_time_string
);
3341 defsubr (&Sdecode_time
);
3342 defsubr (&Sencode_time
);
3343 defsubr (&Scurrent_time_string
);
3344 defsubr (&Scurrent_time_zone
);
3345 defsubr (&Sset_time_zone_rule
);
3346 defsubr (&Ssystem_name
);
3347 defsubr (&Smessage
);
3348 defsubr (&Smessage_box
);
3349 defsubr (&Smessage_or_box
);
3350 defsubr (&Scurrent_message
);
3353 defsubr (&Sinsert_buffer_substring
);
3354 defsubr (&Scompare_buffer_substrings
);
3355 defsubr (&Ssubst_char_in_region
);
3356 defsubr (&Stranslate_region
);
3357 defsubr (&Sdelete_region
);
3359 defsubr (&Snarrow_to_region
);
3360 defsubr (&Ssave_restriction
);
3361 defsubr (&Stranspose_regions
);