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>
33 #include "intervals.h"
40 #define min(a, b) ((a) < (b) ? (a) : (b))
41 #define max(a, b) ((a) > (b) ? (a) : (b))
47 extern char **environ
;
48 extern Lisp_Object
make_time ();
49 extern void insert_from_buffer ();
50 static int tm_diff ();
51 static void update_buffer_properties ();
52 size_t emacs_strftime ();
53 void set_time_zone_rule ();
55 Lisp_Object Vbuffer_access_fontify_functions
;
56 Lisp_Object Qbuffer_access_fontify_functions
;
57 Lisp_Object Vbuffer_access_fontified_property
;
59 Lisp_Object
Fuser_full_name ();
61 /* Some static data, and a function to initialize it for each run */
63 Lisp_Object Vsystem_name
;
64 Lisp_Object Vuser_real_login_name
; /* login name of current user ID */
65 Lisp_Object Vuser_full_name
; /* full name of current user */
66 Lisp_Object Vuser_login_name
; /* user name from LOGNAME or USER */
72 register unsigned char *p
, *q
, *r
;
73 struct passwd
*pw
; /* password entry for the current user */
76 /* Set up system_name even when dumping. */
80 /* Don't bother with this on initial start when just dumping out */
83 #endif /* not CANNOT_DUMP */
85 pw
= (struct passwd
*) getpwuid (getuid ());
87 /* We let the real user name default to "root" because that's quite
88 accurate on MSDOG and because it lets Emacs find the init file.
89 (The DVX libraries override the Djgpp libraries here.) */
90 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "root");
92 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "unknown");
95 /* Get the effective user name, by consulting environment variables,
96 or the effective uid if those are unset. */
97 user_name
= (char *) getenv ("LOGNAME");
100 user_name
= (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
101 #else /* WINDOWSNT */
102 user_name
= (char *) getenv ("USER");
103 #endif /* WINDOWSNT */
106 pw
= (struct passwd
*) getpwuid (geteuid ());
107 user_name
= (char *) (pw
? pw
->pw_name
: "unknown");
109 Vuser_login_name
= build_string (user_name
);
111 /* If the user name claimed in the environment vars differs from
112 the real uid, use the claimed name to find the full name. */
113 tem
= Fstring_equal (Vuser_login_name
, Vuser_real_login_name
);
114 Vuser_full_name
= Fuser_full_name (NILP (tem
)? make_number (geteuid())
117 p
= (unsigned char *) getenv ("NAME");
119 Vuser_full_name
= build_string (p
);
120 else if (NILP (Vuser_full_name
))
121 Vuser_full_name
= build_string ("unknown");
124 DEFUN ("char-to-string", Fchar_to_string
, Schar_to_string
, 1, 1, 0,
125 "Convert arg CHAR to a string containing multi-byte form of that character.")
127 Lisp_Object character
;
130 unsigned char workbuf
[4], *str
;
132 CHECK_NUMBER (character
, 0);
134 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
135 return make_multibyte_string (str
, 1, len
);
138 DEFUN ("string-to-char", Fstring_to_char
, Sstring_to_char
, 1, 1, 0,
139 "Convert arg STRING to a character, the first character of that string.\n\
140 A multibyte character is handled correctly.")
142 register Lisp_Object string
;
144 register Lisp_Object val
;
145 register struct Lisp_String
*p
;
146 CHECK_STRING (string
, 0);
147 p
= XSTRING (string
);
149 XSETFASTINT (val
, STRING_CHAR (p
->data
, p
->size
));
151 XSETFASTINT (val
, 0);
156 buildmark (charpos
, bytepos
)
157 int charpos
, bytepos
;
159 register Lisp_Object mark
;
160 mark
= Fmake_marker ();
161 set_marker_both (mark
, Qnil
, charpos
, bytepos
);
165 DEFUN ("point", Fpoint
, Spoint
, 0, 0, 0,
166 "Return value of point, as an integer.\n\
167 Beginning of buffer is position (point-min)")
171 XSETFASTINT (temp
, PT
);
175 DEFUN ("point-marker", Fpoint_marker
, Spoint_marker
, 0, 0, 0,
176 "Return value of point, as a marker object.")
179 return buildmark (PT
, PT_BYTE
);
183 clip_to_bounds (lower
, num
, upper
)
184 int lower
, num
, upper
;
188 else if (num
> upper
)
194 DEFUN ("goto-char", Fgoto_char
, Sgoto_char
, 1, 1, "NGoto char: ",
195 "Set point to POSITION, a number or marker.\n\
196 Beginning of buffer is position (point-min), end is (point-max).\n\
197 If the position is in the middle of a multibyte form,\n\
198 the actual point is set at the head of the multibyte form\n\
199 except in the case that `enable-multibyte-characters' is nil.")
201 register Lisp_Object position
;
206 if (MARKERP (position
))
208 pos
= marker_position (position
);
210 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
212 SET_PT_BOTH (ZV
, ZV_BYTE
);
214 SET_PT_BOTH (pos
, marker_byte_position (position
));
219 CHECK_NUMBER_COERCE_MARKER (position
, 0);
221 pos
= clip_to_bounds (BEGV
, XINT (position
), ZV
);
227 region_limit (beginningp
)
230 extern Lisp_Object Vmark_even_if_inactive
; /* Defined in callint.c. */
231 register Lisp_Object m
;
232 if (!NILP (Vtransient_mark_mode
) && NILP (Vmark_even_if_inactive
)
233 && NILP (current_buffer
->mark_active
))
234 Fsignal (Qmark_inactive
, Qnil
);
235 m
= Fmarker_position (current_buffer
->mark
);
236 if (NILP (m
)) error ("There is no region now");
237 if ((PT
< XFASTINT (m
)) == beginningp
)
238 return (make_number (PT
));
243 DEFUN ("region-beginning", Fregion_beginning
, Sregion_beginning
, 0, 0, 0,
244 "Return position of beginning of region, as an integer.")
247 return (region_limit (1));
250 DEFUN ("region-end", Fregion_end
, Sregion_end
, 0, 0, 0,
251 "Return position of end of region, as an integer.")
254 return (region_limit (0));
257 DEFUN ("mark-marker", Fmark_marker
, Smark_marker
, 0, 0, 0,
258 "Return this buffer's mark, as a marker object.\n\
259 Watch out! Moving this marker changes the mark position.\n\
260 If you set the marker not to point anywhere, the buffer will have no mark.")
263 return current_buffer
->mark
;
266 DEFUN ("line-beginning-position", Fline_beginning_position
, Sline_beginning_position
,
268 "Return the character position of the first character on the current line.\n\
269 With argument N not nil or 1, move forward N - 1 lines first.\n\
270 If scan reaches end of buffer, return that position.\n\
271 This function does not move point.")
275 register int orig
, orig_byte
, end
;
284 Fforward_line (make_number (XINT (n
) - 1));
286 SET_PT_BOTH (orig
, orig_byte
);
288 return make_number (end
);
291 DEFUN ("line-end-position", Fline_end_position
, Sline_end_position
,
293 "Return the character position of the last character on the current line.\n\
294 With argument N not nil or 1, move forward N - 1 lines first.\n\
295 If scan reaches end of buffer, return that position.\n\
296 This function does not move point.")
305 return make_number (find_before_next_newline
306 (PT
, 0, XINT (n
) - (XINT (n
) <= 0)));
310 save_excursion_save ()
312 register int visible
= (XBUFFER (XWINDOW (selected_window
)->buffer
)
315 return Fcons (Fpoint_marker (),
316 Fcons (Fcopy_marker (current_buffer
->mark
, Qnil
),
317 Fcons (visible
? Qt
: Qnil
,
318 current_buffer
->mark_active
)));
322 save_excursion_restore (info
)
325 Lisp_Object tem
, tem1
, omark
, nmark
;
326 struct gcpro gcpro1
, gcpro2
, gcpro3
;
328 tem
= Fmarker_buffer (Fcar (info
));
329 /* If buffer being returned to is now deleted, avoid error */
330 /* Otherwise could get error here while unwinding to top level
332 /* In that case, Fmarker_buffer returns nil now. */
336 omark
= nmark
= Qnil
;
337 GCPRO3 (info
, omark
, nmark
);
342 unchain_marker (tem
);
343 tem
= Fcar (Fcdr (info
));
344 omark
= Fmarker_position (current_buffer
->mark
);
345 Fset_marker (current_buffer
->mark
, tem
, Fcurrent_buffer ());
346 nmark
= Fmarker_position (tem
);
347 unchain_marker (tem
);
348 tem
= Fcdr (Fcdr (info
));
349 #if 0 /* We used to make the current buffer visible in the selected window
350 if that was true previously. That avoids some anomalies.
351 But it creates others, and it wasn't documented, and it is simpler
352 and cleaner never to alter the window/buffer connections. */
355 && current_buffer
!= XBUFFER (XWINDOW (selected_window
)->buffer
))
356 Fswitch_to_buffer (Fcurrent_buffer (), Qnil
);
359 tem1
= current_buffer
->mark_active
;
360 current_buffer
->mark_active
= Fcdr (tem
);
361 if (!NILP (Vrun_hooks
))
363 /* If mark is active now, and either was not active
364 or was at a different place, run the activate hook. */
365 if (! NILP (current_buffer
->mark_active
))
367 if (! EQ (omark
, nmark
))
368 call1 (Vrun_hooks
, intern ("activate-mark-hook"));
370 /* If mark has ceased to be active, run deactivate hook. */
371 else if (! NILP (tem1
))
372 call1 (Vrun_hooks
, intern ("deactivate-mark-hook"));
378 DEFUN ("save-excursion", Fsave_excursion
, Ssave_excursion
, 0, UNEVALLED
, 0,
379 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
380 Executes BODY just like `progn'.\n\
381 The values of point, mark and the current buffer are restored\n\
382 even in case of abnormal exit (throw or error).\n\
383 The state of activation of the mark is also restored.")
387 register Lisp_Object val
;
388 int count
= specpdl_ptr
- specpdl
;
390 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
393 return unbind_to (count
, val
);
396 DEFUN ("save-current-buffer", Fsave_current_buffer
, Ssave_current_buffer
, 0, UNEVALLED
, 0,
397 "Save the current buffer; execute BODY; restore the current buffer.\n\
398 Executes BODY just like `progn'.")
402 register Lisp_Object val
;
403 int count
= specpdl_ptr
- specpdl
;
405 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
408 return unbind_to (count
, val
);
411 DEFUN ("buffer-size", Fbufsize
, Sbufsize
, 0, 0, 0,
412 "Return the number of characters in the current buffer.")
416 XSETFASTINT (temp
, Z
- BEG
);
420 DEFUN ("point-min", Fpoint_min
, Spoint_min
, 0, 0, 0,
421 "Return the minimum permissible value of point in the current buffer.\n\
422 This is 1, unless narrowing (a buffer restriction) is in effect.")
426 XSETFASTINT (temp
, BEGV
);
430 DEFUN ("point-min-marker", Fpoint_min_marker
, Spoint_min_marker
, 0, 0, 0,
431 "Return a marker to the minimum permissible value of point in this buffer.\n\
432 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
435 return buildmark (BEGV
, BEGV_BYTE
);
438 DEFUN ("point-max", Fpoint_max
, Spoint_max
, 0, 0, 0,
439 "Return the maximum permissible value of point in the current buffer.\n\
440 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
441 is in effect, in which case it is less.")
445 XSETFASTINT (temp
, ZV
);
449 DEFUN ("point-max-marker", Fpoint_max_marker
, Spoint_max_marker
, 0, 0, 0,
450 "Return a marker to the maximum permissible value of point in this buffer.\n\
451 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
452 is in effect, in which case it is less.")
455 return buildmark (ZV
, ZV_BYTE
);
458 DEFUN ("following-char", Ffollowing_char
, Sfollowing_char
, 0, 0, 0,
459 "Return the character following point, as a number.\n\
460 At the end of the buffer or accessible region, return 0.\n\
461 If `enable-multibyte-characters' is nil or point is not\n\
462 at character boundary, multibyte form is ignored,\n\
463 and only one byte following point is returned as a character.")
468 XSETFASTINT (temp
, 0);
470 XSETFASTINT (temp
, FETCH_CHAR (PT_BYTE
));
474 DEFUN ("preceding-char", Fprevious_char
, Sprevious_char
, 0, 0, 0,
475 "Return the character preceding point, as a number.\n\
476 At the beginning of the buffer or accessible region, return 0.\n\
477 If `enable-multibyte-characters' is nil or point is not\n\
478 at character boundary, multi-byte form is ignored,\n\
479 and only one byte preceding point is returned as a character.")
484 XSETFASTINT (temp
, 0);
485 else if (!NILP (current_buffer
->enable_multibyte_characters
))
489 XSETFASTINT (temp
, FETCH_CHAR (pos
));
492 XSETFASTINT (temp
, FETCH_BYTE (PT_BYTE
- 1));
496 DEFUN ("bobp", Fbobp
, Sbobp
, 0, 0, 0,
497 "Return t if point is at the beginning of the buffer.\n\
498 If the buffer is narrowed, this means the beginning of the narrowed part.")
506 DEFUN ("eobp", Feobp
, Seobp
, 0, 0, 0,
507 "Return t if point is at the end of the buffer.\n\
508 If the buffer is narrowed, this means the end of the narrowed part.")
516 DEFUN ("bolp", Fbolp
, Sbolp
, 0, 0, 0,
517 "Return t if point is at the beginning of a line.")
520 if (PT
== BEGV
|| FETCH_BYTE (PT_BYTE
- 1) == '\n')
525 DEFUN ("eolp", Feolp
, Seolp
, 0, 0, 0,
526 "Return t if point is at the end of a line.\n\
527 `End of a line' includes point being at the end of the buffer.")
530 if (PT
== ZV
|| FETCH_BYTE (PT_BYTE
) == '\n')
535 DEFUN ("char-after", Fchar_after
, Schar_after
, 0, 1, 0,
536 "Return character in current buffer at position POS.\n\
537 POS is an integer or a buffer pointer.\n\
538 If POS is out of range, the value is nil.\n\
539 If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
540 multi-byte form is ignored, and only one byte at POS\n\
541 is returned as a character.")
545 register int pos_byte
;
546 register Lisp_Object val
;
549 return make_number (FETCH_CHAR (PT_BYTE
));
552 pos_byte
= marker_byte_position (pos
);
555 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
557 pos_byte
= CHAR_TO_BYTE (XINT (pos
));
560 if (pos_byte
< BEGV_BYTE
|| pos_byte
>= ZV_BYTE
)
563 return make_number (FETCH_CHAR (pos_byte
));
566 DEFUN ("char-before", Fchar_before
, Schar_before
, 0, 1, 0,
567 "Return character in current buffer preceding position POS.\n\
568 POS is an integer or a buffer pointer.\n\
569 If POS is out of range, the value is nil.\n\
570 If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
571 multi-byte form is ignored, and only one byte preceding POS\n\
572 is returned as a character.")
576 register Lisp_Object val
;
577 register int pos_byte
;
581 else if (MARKERP (pos
))
582 pos_byte
= marker_byte_position (pos
);
585 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
587 pos_byte
= CHAR_TO_BYTE (XINT (pos
));
590 if (pos_byte
<= BEGV_BYTE
|| pos_byte
> ZV_BYTE
)
593 if (!NILP (current_buffer
->enable_multibyte_characters
))
596 XSETFASTINT (val
, FETCH_CHAR (pos_byte
));
601 XSETFASTINT (val
, FETCH_BYTE (pos_byte
));
606 DEFUN ("user-login-name", Fuser_login_name
, Suser_login_name
, 0, 1, 0,
607 "Return the name under which the user logged in, as a string.\n\
608 This is based on the effective uid, not the real uid.\n\
609 Also, if the environment variable LOGNAME or USER is set,\n\
610 that determines the value of this function.\n\n\
611 If optional argument UID is an integer, return the login name of the user\n\
612 with that uid, or nil if there is no such user.")
618 /* Set up the user name info if we didn't do it before.
619 (That can happen if Emacs is dumpable
620 but you decide to run `temacs -l loadup' and not dump. */
621 if (INTEGERP (Vuser_login_name
))
625 return Vuser_login_name
;
627 CHECK_NUMBER (uid
, 0);
628 pw
= (struct passwd
*) getpwuid (XINT (uid
));
629 return (pw
? build_string (pw
->pw_name
) : Qnil
);
632 DEFUN ("user-real-login-name", Fuser_real_login_name
, Suser_real_login_name
,
634 "Return the name of the user's real uid, as a string.\n\
635 This ignores the environment variables LOGNAME and USER, so it differs from\n\
636 `user-login-name' when running under `su'.")
639 /* Set up the user name info if we didn't do it before.
640 (That can happen if Emacs is dumpable
641 but you decide to run `temacs -l loadup' and not dump. */
642 if (INTEGERP (Vuser_login_name
))
644 return Vuser_real_login_name
;
647 DEFUN ("user-uid", Fuser_uid
, Suser_uid
, 0, 0, 0,
648 "Return the effective uid of Emacs, as an integer.")
651 return make_number (geteuid ());
654 DEFUN ("user-real-uid", Fuser_real_uid
, Suser_real_uid
, 0, 0, 0,
655 "Return the real uid of Emacs, as an integer.")
658 return make_number (getuid ());
661 DEFUN ("user-full-name", Fuser_full_name
, Suser_full_name
, 0, 1, 0,
662 "Return the full name of the user logged in, as a string.\n\
663 If optional argument UID is an integer, return the full name of the user\n\
664 with that uid, or \"unknown\" if there is no such user.\n\
665 If UID is a string, return the full name of the user with that login\n\
666 name, or \"unknown\" if no such user could be found.")
671 register unsigned char *p
, *q
;
672 extern char *index ();
676 return Vuser_full_name
;
677 else if (NUMBERP (uid
))
678 pw
= (struct passwd
*) getpwuid (XINT (uid
));
679 else if (STRINGP (uid
))
680 pw
= (struct passwd
*) getpwnam (XSTRING (uid
)->data
);
682 error ("Invalid UID specification");
687 p
= (unsigned char *) USER_FULL_NAME
;
688 /* Chop off everything after the first comma. */
689 q
= (unsigned char *) index (p
, ',');
690 full
= make_string (p
, q
? q
- p
: strlen (p
));
692 #ifdef AMPERSAND_FULL_NAME
693 p
= XSTRING (full
)->data
;
694 q
= (unsigned char *) index (p
, '&');
695 /* Substitute the login name for the &, upcasing the first character. */
698 register unsigned char *r
;
701 login
= Fuser_login_name (make_number (pw
->pw_uid
));
702 r
= (unsigned char *) alloca (strlen (p
) + XSTRING (login
)->size
+ 1);
705 strcat (r
, XSTRING (login
)->data
);
706 r
[q
- p
] = UPCASE (r
[q
- p
]);
708 full
= build_string (r
);
710 #endif /* AMPERSAND_FULL_NAME */
715 DEFUN ("system-name", Fsystem_name
, Ssystem_name
, 0, 0, 0,
716 "Return the name of the machine you are running on, as a string.")
722 /* For the benefit of callers who don't want to include lisp.h */
726 if (STRINGP (Vsystem_name
))
727 return (char *) XSTRING (Vsystem_name
)->data
;
732 DEFUN ("emacs-pid", Femacs_pid
, Semacs_pid
, 0, 0, 0,
733 "Return the process ID of Emacs, as an integer.")
736 return make_number (getpid ());
739 DEFUN ("current-time", Fcurrent_time
, Scurrent_time
, 0, 0, 0,
740 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
741 The time is returned as a list of three integers. The first has the\n\
742 most significant 16 bits of the seconds, while the second has the\n\
743 least significant 16 bits. The third integer gives the microsecond\n\
746 The microsecond count is zero on systems that do not provide\n\
747 resolution finer than a second.")
751 Lisp_Object result
[3];
754 XSETINT (result
[0], (EMACS_SECS (t
) >> 16) & 0xffff);
755 XSETINT (result
[1], (EMACS_SECS (t
) >> 0) & 0xffff);
756 XSETINT (result
[2], EMACS_USECS (t
));
758 return Flist (3, result
);
763 lisp_time_argument (specified_time
, result
)
764 Lisp_Object specified_time
;
767 if (NILP (specified_time
))
768 return time (result
) != -1;
771 Lisp_Object high
, low
;
772 high
= Fcar (specified_time
);
773 CHECK_NUMBER (high
, 0);
774 low
= Fcdr (specified_time
);
777 CHECK_NUMBER (low
, 0);
778 *result
= (XINT (high
) << 16) + (XINT (low
) & 0xffff);
779 return *result
>> 16 == XINT (high
);
784 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
785 "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
786 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
787 `current-time' or `file-attributes'.\n\
788 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
789 as Universal Time; nil means describe TIME in the local time zone.\n\
790 The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
791 by text that describes the specified date and time in TIME:\n\
793 %Y is the year, %y within the century, %C the century.\n\
794 %G is the year corresponding to the ISO week, %g within the century.\n\
795 %m is the numeric month.\n\
796 %b and %h are the locale's abbreviated month name, %B the full name.\n\
797 %d is the day of the month, zero-padded, %e is blank-padded.\n\
798 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\
799 %a is the locale's abbreviated name of the day of week, %A the full name.\n\
800 %U is the week number starting on Sunday, %W starting on Monday,\n\
801 %V according to ISO 8601.\n\
802 %j is the day of the year.\n\
804 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\
805 only blank-padded, %l is like %I blank-padded.\n\
806 %p is the locale's equivalent of either AM or PM.\n\
809 %Z is the time zone name, %z is the numeric form.\n\
810 %s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\
812 %c is the locale's date and time format.\n\
813 %x is the locale's \"preferred\" date format.\n\
814 %D is like \"%m/%d/%y\".\n\
816 %R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\
817 %X is the locale's \"preferred\" time format.\n\
819 Finally, %n is a newline, %t is a tab, %% is a literal %.\n\
821 Certain flags and modifiers are available with some format controls.\n\
822 The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
823 but padded with blanks; %-X is like %X, but without padding.\n\
824 %NX (where N stands for an integer) is like %X,\n\
825 but takes up at least N (a number) positions.\n\
826 The modifiers are `E' and `O'. For certain characters X,\n\
827 %EX is a locale's alternative version of %X;\n\
828 %OX is like %X, but uses the locale's number symbols.\n\
830 For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
831 (format_string, time, universal)
834 DEFUN ("format-time-string", Fformat_time_string
, Sformat_time_string
, 1, 3, 0,
835 0 /* See immediately above */)
836 (format_string
, time
, universal
)
837 Lisp_Object format_string
, time
, universal
;
842 CHECK_STRING (format_string
, 1);
844 if (! lisp_time_argument (time
, &value
))
845 error ("Invalid time specification");
847 /* This is probably enough. */
848 size
= XSTRING (format_string
)->size_byte
* 6 + 50;
852 char *buf
= (char *) alloca (size
+ 1);
856 result
= emacs_strftime (buf
, size
, XSTRING (format_string
)->data
,
857 (NILP (universal
) ? localtime (&value
)
859 if ((result
> 0 && result
< size
) || (result
== 0 && buf
[0] == '\0'))
860 return build_string (buf
);
862 /* If buffer was too small, make it bigger and try again. */
863 result
= emacs_strftime (NULL
, 0x7fffffff, XSTRING (format_string
)->data
,
864 (NILP (universal
) ? localtime (&value
)
870 DEFUN ("decode-time", Fdecode_time
, Sdecode_time
, 0, 1, 0,
871 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
872 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
873 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
874 to use the current time. The list has the following nine members:\n\
875 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
876 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
877 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
878 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
879 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
880 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
881 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
882 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
884 Lisp_Object specified_time
;
888 struct tm
*decoded_time
;
889 Lisp_Object list_args
[9];
891 if (! lisp_time_argument (specified_time
, &time_spec
))
892 error ("Invalid time specification");
894 decoded_time
= localtime (&time_spec
);
895 XSETFASTINT (list_args
[0], decoded_time
->tm_sec
);
896 XSETFASTINT (list_args
[1], decoded_time
->tm_min
);
897 XSETFASTINT (list_args
[2], decoded_time
->tm_hour
);
898 XSETFASTINT (list_args
[3], decoded_time
->tm_mday
);
899 XSETFASTINT (list_args
[4], decoded_time
->tm_mon
+ 1);
900 XSETINT (list_args
[5], decoded_time
->tm_year
+ 1900);
901 XSETFASTINT (list_args
[6], decoded_time
->tm_wday
);
902 list_args
[7] = (decoded_time
->tm_isdst
)? Qt
: Qnil
;
904 /* Make a copy, in case gmtime modifies the struct. */
905 save_tm
= *decoded_time
;
906 decoded_time
= gmtime (&time_spec
);
907 if (decoded_time
== 0)
910 XSETINT (list_args
[8], tm_diff (&save_tm
, decoded_time
));
911 return Flist (9, list_args
);
914 DEFUN ("encode-time", Fencode_time
, Sencode_time
, 6, MANY
, 0,
915 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
916 This is the reverse operation of `decode-time', which see.\n\
917 ZONE defaults to the current time zone rule. This can\n\
918 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
919 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
920 applied without consideration for daylight savings time.\n\
922 You can pass more than 7 arguments; then the first six arguments\n\
923 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
924 The intervening arguments are ignored.\n\
925 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
927 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
928 for example, a DAY of 0 means the day preceding the given month.\n\
929 Year numbers less than 100 are treated just like other year numbers.\n\
930 If you want them to stand for years in this century, you must do that yourself.")
933 register Lisp_Object
*args
;
937 Lisp_Object zone
= (nargs
> 6 ? args
[nargs
- 1] : Qnil
);
939 CHECK_NUMBER (args
[0], 0); /* second */
940 CHECK_NUMBER (args
[1], 1); /* minute */
941 CHECK_NUMBER (args
[2], 2); /* hour */
942 CHECK_NUMBER (args
[3], 3); /* day */
943 CHECK_NUMBER (args
[4], 4); /* month */
944 CHECK_NUMBER (args
[5], 5); /* year */
946 tm
.tm_sec
= XINT (args
[0]);
947 tm
.tm_min
= XINT (args
[1]);
948 tm
.tm_hour
= XINT (args
[2]);
949 tm
.tm_mday
= XINT (args
[3]);
950 tm
.tm_mon
= XINT (args
[4]) - 1;
951 tm
.tm_year
= XINT (args
[5]) - 1900;
962 char **oldenv
= environ
, **newenv
;
966 else if (STRINGP (zone
))
967 tzstring
= (char *) XSTRING (zone
)->data
;
968 else if (INTEGERP (zone
))
970 int abszone
= abs (XINT (zone
));
971 sprintf (tzbuf
, "XXX%s%d:%02d:%02d", "-" + (XINT (zone
) < 0),
972 abszone
/ (60*60), (abszone
/60) % 60, abszone
% 60);
976 error ("Invalid time zone specification");
978 /* Set TZ before calling mktime; merely adjusting mktime's returned
979 value doesn't suffice, since that would mishandle leap seconds. */
980 set_time_zone_rule (tzstring
);
984 /* Restore TZ to previous value. */
988 #ifdef LOCALTIME_CACHE
993 if (time
== (time_t) -1)
994 error ("Specified time is not representable");
996 return make_time (time
);
999 DEFUN ("current-time-string", Fcurrent_time_string
, Scurrent_time_string
, 0, 1, 0,
1000 "Return the current time, as a human-readable string.\n\
1001 Programs can use this function to decode a time,\n\
1002 since the number of columns in each field is fixed.\n\
1003 The format is `Sun Sep 16 01:03:52 1973'.\n\
1004 However, see also the functions `decode-time' and `format-time-string'\n\
1005 which provide a much more powerful and general facility.\n\
1007 If an argument is given, it specifies a time to format\n\
1008 instead of the current time. The argument should have the form:\n\
1011 (HIGH LOW . IGNORED).\n\
1012 Thus, you can use times obtained from `current-time'\n\
1013 and from `file-attributes'.")
1015 Lisp_Object specified_time
;
1021 if (! lisp_time_argument (specified_time
, &value
))
1023 tem
= (char *) ctime (&value
);
1025 strncpy (buf
, tem
, 24);
1028 return build_string (buf
);
1031 #define TM_YEAR_BASE 1900
1033 /* Yield A - B, measured in seconds.
1034 This function is copied from the GNU C Library. */
1039 /* Compute intervening leap days correctly even if year is negative.
1040 Take care to avoid int overflow in leap day calculations,
1041 but it's OK to assume that A and B are close to each other. */
1042 int a4
= (a
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (a
->tm_year
& 3);
1043 int b4
= (b
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (b
->tm_year
& 3);
1044 int a100
= a4
/ 25 - (a4
% 25 < 0);
1045 int b100
= b4
/ 25 - (b4
% 25 < 0);
1046 int a400
= a100
>> 2;
1047 int b400
= b100
>> 2;
1048 int intervening_leap_days
= (a4
- b4
) - (a100
- b100
) + (a400
- b400
);
1049 int years
= a
->tm_year
- b
->tm_year
;
1050 int days
= (365 * years
+ intervening_leap_days
1051 + (a
->tm_yday
- b
->tm_yday
));
1052 return (60 * (60 * (24 * days
+ (a
->tm_hour
- b
->tm_hour
))
1053 + (a
->tm_min
- b
->tm_min
))
1054 + (a
->tm_sec
- b
->tm_sec
));
1057 DEFUN ("current-time-zone", Fcurrent_time_zone
, Scurrent_time_zone
, 0, 1, 0,
1058 "Return the offset and name for the local time zone.\n\
1059 This returns a list of the form (OFFSET NAME).\n\
1060 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1061 A negative value means west of Greenwich.\n\
1062 NAME is a string giving the name of the time zone.\n\
1063 If an argument is given, it specifies when the time zone offset is determined\n\
1064 instead of using the current time. The argument should have the form:\n\
1067 (HIGH LOW . IGNORED).\n\
1068 Thus, you can use times obtained from `current-time'\n\
1069 and from `file-attributes'.\n\
1071 Some operating systems cannot provide all this information to Emacs;\n\
1072 in this case, `current-time-zone' returns a list containing nil for\n\
1073 the data it can't find.")
1075 Lisp_Object specified_time
;
1080 if (lisp_time_argument (specified_time
, &value
)
1081 && (t
= gmtime (&value
)) != 0)
1087 gmt
= *t
; /* Make a copy, in case localtime modifies *t. */
1088 t
= localtime (&value
);
1089 offset
= tm_diff (t
, &gmt
);
1093 s
= (char *)t
->tm_zone
;
1094 #else /* not HAVE_TM_ZONE */
1096 if (t
->tm_isdst
== 0 || t
->tm_isdst
== 1)
1097 s
= tzname
[t
->tm_isdst
];
1099 #endif /* not HAVE_TM_ZONE */
1102 /* No local time zone name is available; use "+-NNNN" instead. */
1103 int am
= (offset
< 0 ? -offset
: offset
) / 60;
1104 sprintf (buf
, "%c%02d%02d", (offset
< 0 ? '-' : '+'), am
/60, am
%60);
1107 return Fcons (make_number (offset
), Fcons (build_string (s
), Qnil
));
1110 return Fmake_list (make_number (2), Qnil
);
1113 /* This holds the value of `environ' produced by the previous
1114 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1115 has never been called. */
1116 static char **environbuf
;
1118 DEFUN ("set-time-zone-rule", Fset_time_zone_rule
, Sset_time_zone_rule
, 1, 1, 0,
1119 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
1120 If TZ is nil, use implementation-defined default time zone information.\n\
1121 If TZ is t, use Universal Time.")
1129 else if (EQ (tz
, Qt
))
1133 CHECK_STRING (tz
, 0);
1134 tzstring
= (char *) XSTRING (tz
)->data
;
1137 set_time_zone_rule (tzstring
);
1140 environbuf
= environ
;
1145 #ifdef LOCALTIME_CACHE
1147 /* These two values are known to load tz files in buggy implementations,
1148 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1149 Their values shouldn't matter in non-buggy implementations.
1150 We don't use string literals for these strings,
1151 since if a string in the environment is in readonly
1152 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1153 See Sun bugs 1113095 and 1114114, ``Timezone routines
1154 improperly modify environment''. */
1156 static char set_time_zone_rule_tz1
[] = "TZ=GMT+0";
1157 static char set_time_zone_rule_tz2
[] = "TZ=GMT+1";
1161 /* Set the local time zone rule to TZSTRING.
1162 This allocates memory into `environ', which it is the caller's
1163 responsibility to free. */
1165 set_time_zone_rule (tzstring
)
1169 char **from
, **to
, **newenv
;
1171 /* Make the ENVIRON vector longer with room for TZSTRING. */
1172 for (from
= environ
; *from
; from
++)
1174 envptrs
= from
- environ
+ 2;
1175 newenv
= to
= (char **) xmalloc (envptrs
* sizeof (char *)
1176 + (tzstring
? strlen (tzstring
) + 4 : 0));
1178 /* Add TZSTRING to the end of environ, as a value for TZ. */
1181 char *t
= (char *) (to
+ envptrs
);
1183 strcat (t
, tzstring
);
1187 /* Copy the old environ vector elements into NEWENV,
1188 but don't copy the TZ variable.
1189 So we have only one definition of TZ, which came from TZSTRING. */
1190 for (from
= environ
; *from
; from
++)
1191 if (strncmp (*from
, "TZ=", 3) != 0)
1197 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1198 the TZ variable is stored. If we do not have a TZSTRING,
1199 TO points to the vector slot which has the terminating null. */
1201 #ifdef LOCALTIME_CACHE
1203 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1204 "US/Pacific" that loads a tz file, then changes to a value like
1205 "XXX0" that does not load a tz file, and then changes back to
1206 its original value, the last change is (incorrectly) ignored.
1207 Also, if TZ changes twice in succession to values that do
1208 not load a tz file, tzset can dump core (see Sun bug#1225179).
1209 The following code works around these bugs. */
1213 /* Temporarily set TZ to a value that loads a tz file
1214 and that differs from tzstring. */
1216 *newenv
= (strcmp (tzstring
, set_time_zone_rule_tz1
+ 3) == 0
1217 ? set_time_zone_rule_tz2
: set_time_zone_rule_tz1
);
1223 /* The implied tzstring is unknown, so temporarily set TZ to
1224 two different values that each load a tz file. */
1225 *to
= set_time_zone_rule_tz1
;
1228 *to
= set_time_zone_rule_tz2
;
1233 /* Now TZ has the desired value, and tzset can be invoked safely. */
1240 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1241 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1242 type of object is Lisp_String). INHERIT is passed to
1243 INSERT_FROM_STRING_FUNC as the last argument. */
1246 general_insert_function (insert_func
, insert_from_string_func
,
1247 inherit
, nargs
, args
)
1248 void (*insert_func
) P_ ((unsigned char *, int));
1249 void (*insert_from_string_func
) P_ ((Lisp_Object
, int, int, int, int, int));
1251 register Lisp_Object
*args
;
1253 register int argnum
;
1254 register Lisp_Object val
;
1256 for (argnum
= 0; argnum
< nargs
; argnum
++)
1262 unsigned char workbuf
[4], *str
;
1265 if (!NILP (current_buffer
->enable_multibyte_characters
))
1266 len
= CHAR_STRING (XFASTINT (val
), workbuf
, str
);
1268 workbuf
[0] = XINT (val
), str
= workbuf
, len
= 1;
1269 (*insert_func
) (str
, len
);
1271 else if (STRINGP (val
))
1273 (*insert_from_string_func
) (val
, 0, 0,
1274 XSTRING (val
)->size
,
1275 XSTRING (val
)->size_byte
,
1280 val
= wrong_type_argument (Qchar_or_string_p
, val
);
1294 /* Callers passing one argument to Finsert need not gcpro the
1295 argument "array", since the only element of the array will
1296 not be used after calling insert or insert_from_string, so
1297 we don't care if it gets trashed. */
1299 DEFUN ("insert", Finsert
, Sinsert
, 0, MANY
, 0,
1300 "Insert the arguments, either strings or characters, at point.\n\
1301 Point and before-insertion-markers move forward so that it ends up\n\
1302 after the inserted text.\n\
1303 Any other markers at the point of insertion remain before the text.")
1306 register Lisp_Object
*args
;
1308 general_insert_function (insert
, insert_from_string
, 0, nargs
, args
);
1312 DEFUN ("insert-and-inherit", Finsert_and_inherit
, Sinsert_and_inherit
,
1314 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1315 Point and before-insertion-markers move forward so that it ends up\n\
1316 after the inserted text.\n\
1317 Any other markers at the point of insertion remain before the text.")
1320 register Lisp_Object
*args
;
1322 general_insert_function (insert_and_inherit
, insert_from_string
, 1,
1327 DEFUN ("insert-before-markers", Finsert_before_markers
, Sinsert_before_markers
, 0, MANY
, 0,
1328 "Insert strings or characters at point, relocating markers after the text.\n\
1329 Point and before-insertion-markers move forward so that it ends up\n\
1330 after the inserted text.\n\
1331 Any other markers at the point of insertion also end up after the text.")
1334 register Lisp_Object
*args
;
1336 general_insert_function (insert_before_markers
,
1337 insert_from_string_before_markers
, 0,
1342 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers
,
1343 Sinsert_and_inherit_before_markers
, 0, MANY
, 0,
1344 "Insert text at point, relocating markers and inheriting properties.\n\
1345 Point moves forward so that it ends up after the inserted text.\n\
1346 Any other markers at the point of insertion also end up after the text.")
1349 register Lisp_Object
*args
;
1351 general_insert_function (insert_before_markers_and_inherit
,
1352 insert_from_string_before_markers
, 1,
1357 DEFUN ("insert-char", Finsert_char
, Sinsert_char
, 2, 3, 0,
1358 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1359 Point and before-insertion-markers are affected as in the function `insert'.\n\
1360 Both arguments are required.\n\
1361 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1362 from adjoining text, if those properties are sticky.")
1363 (character
, count
, inherit
)
1364 Lisp_Object character
, count
, inherit
;
1366 register unsigned char *string
;
1367 register int strlen
;
1370 unsigned char workbuf
[4], *str
;
1372 CHECK_NUMBER (character
, 0);
1373 CHECK_NUMBER (count
, 1);
1375 if (!NILP (current_buffer
->enable_multibyte_characters
))
1376 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
1378 workbuf
[0] = XFASTINT (character
), str
= workbuf
, len
= 1;
1379 n
= XINT (count
) * len
;
1382 strlen
= min (n
, 256 * len
);
1383 string
= (unsigned char *) alloca (strlen
);
1384 for (i
= 0; i
< strlen
; i
++)
1385 string
[i
] = str
[i
% len
];
1389 if (!NILP (inherit
))
1390 insert_and_inherit (string
, strlen
);
1392 insert (string
, strlen
);
1397 if (!NILP (inherit
))
1398 insert_and_inherit (string
, n
);
1406 /* Making strings from buffer contents. */
1408 /* Return a Lisp_String containing the text of the current buffer from
1409 START to END. If text properties are in use and the current buffer
1410 has properties in the range specified, the resulting string will also
1411 have them, if PROPS is nonzero.
1413 We don't want to use plain old make_string here, because it calls
1414 make_uninit_string, which can cause the buffer arena to be
1415 compacted. make_string has no way of knowing that the data has
1416 been moved, and thus copies the wrong data into the string. This
1417 doesn't effect most of the other users of make_string, so it should
1418 be left as is. But we should use this function when conjuring
1419 buffer substrings. */
1422 make_buffer_string (start
, end
, props
)
1426 Lisp_Object result
, tem
, tem1
;
1427 int start_byte
= CHAR_TO_BYTE (start
);
1428 int end_byte
= CHAR_TO_BYTE (end
);
1430 if (start
< GPT
&& GPT
< end
)
1433 result
= make_uninit_multibyte_string (end
- start
, end_byte
- start_byte
);
1434 bcopy (BYTE_POS_ADDR (start_byte
), XSTRING (result
)->data
,
1435 end_byte
- start_byte
);
1437 /* If desired, update and copy the text properties. */
1438 #ifdef USE_TEXT_PROPERTIES
1441 update_buffer_properties (start
, end
);
1443 tem
= Fnext_property_change (make_number (start
), Qnil
, make_number (end
));
1444 tem1
= Ftext_properties_at (make_number (start
), Qnil
);
1446 if (XINT (tem
) != end
|| !NILP (tem1
))
1447 copy_intervals_to_string (result
, current_buffer
, start
,
1455 /* Call Vbuffer_access_fontify_functions for the range START ... END
1456 in the current buffer, if necessary. */
1459 update_buffer_properties (start
, end
)
1462 #ifdef USE_TEXT_PROPERTIES
1463 /* If this buffer has some access functions,
1464 call them, specifying the range of the buffer being accessed. */
1465 if (!NILP (Vbuffer_access_fontify_functions
))
1467 Lisp_Object args
[3];
1470 args
[0] = Qbuffer_access_fontify_functions
;
1471 XSETINT (args
[1], start
);
1472 XSETINT (args
[2], end
);
1474 /* But don't call them if we can tell that the work
1475 has already been done. */
1476 if (!NILP (Vbuffer_access_fontified_property
))
1478 tem
= Ftext_property_any (args
[1], args
[2],
1479 Vbuffer_access_fontified_property
,
1482 Frun_hook_with_args (3, args
);
1485 Frun_hook_with_args (3, args
);
1490 DEFUN ("buffer-substring", Fbuffer_substring
, Sbuffer_substring
, 2, 2, 0,
1491 "Return the contents of part of the current buffer as a string.\n\
1492 The two arguments START and END are character positions;\n\
1493 they can be in either order.")
1495 Lisp_Object start
, end
;
1499 validate_region (&start
, &end
);
1503 return make_buffer_string (b
, e
, 1);
1506 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties
,
1507 Sbuffer_substring_no_properties
, 2, 2, 0,
1508 "Return the characters of part of the buffer, without the text properties.\n\
1509 The two arguments START and END are character positions;\n\
1510 they can be in either order.")
1512 Lisp_Object start
, end
;
1516 validate_region (&start
, &end
);
1520 return make_buffer_string (b
, e
, 0);
1523 DEFUN ("buffer-string", Fbuffer_string
, Sbuffer_string
, 0, 0, 0,
1524 "Return the contents of the current buffer as a string.\n\
1525 If narrowing is in effect, this function returns only the visible part\n\
1529 return make_buffer_string (BEGV
, ZV
, 1);
1532 DEFUN ("insert-buffer-substring", Finsert_buffer_substring
, Sinsert_buffer_substring
,
1534 "Insert before point a substring of the contents of buffer BUFFER.\n\
1535 BUFFER may be a buffer or a buffer name.\n\
1536 Arguments START and END are character numbers specifying the substring.\n\
1537 They default to the beginning and the end of BUFFER.")
1539 Lisp_Object buf
, start
, end
;
1541 register int b
, e
, temp
;
1542 register struct buffer
*bp
, *obuf
;
1545 buffer
= Fget_buffer (buf
);
1548 bp
= XBUFFER (buffer
);
1549 if (NILP (bp
->name
))
1550 error ("Selecting deleted buffer");
1556 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1563 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1568 temp
= b
, b
= e
, e
= temp
;
1570 if (!(BUF_BEGV (bp
) <= b
&& e
<= BUF_ZV (bp
)))
1571 args_out_of_range (start
, end
);
1573 obuf
= current_buffer
;
1574 set_buffer_internal_1 (bp
);
1575 update_buffer_properties (b
, e
);
1576 set_buffer_internal_1 (obuf
);
1578 insert_from_buffer (bp
, b
, e
- b
, 0);
1582 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings
, Scompare_buffer_substrings
,
1584 "Compare two substrings of two buffers; return result as number.\n\
1585 the value is -N if first string is less after N-1 chars,\n\
1586 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
1587 Each substring is represented as three arguments: BUFFER, START and END.\n\
1588 That makes six args in all, three for each substring.\n\n\
1589 The value of `case-fold-search' in the current buffer\n\
1590 determines whether case is significant or ignored.")
1591 (buffer1
, start1
, end1
, buffer2
, start2
, end2
)
1592 Lisp_Object buffer1
, start1
, end1
, buffer2
, start2
, end2
;
1594 register int begp1
, endp1
, begp2
, endp2
, temp
, len1
, len2
, length
, i
;
1595 register struct buffer
*bp1
, *bp2
;
1596 register Lisp_Object
*trt
1597 = (!NILP (current_buffer
->case_fold_search
)
1598 ? XCHAR_TABLE (current_buffer
->case_canon_table
)->contents
: 0);
1600 int beg1_byte
, beg2_byte
;
1602 /* Find the first buffer and its substring. */
1605 bp1
= current_buffer
;
1609 buf1
= Fget_buffer (buffer1
);
1612 bp1
= XBUFFER (buf1
);
1613 if (NILP (bp1
->name
))
1614 error ("Selecting deleted buffer");
1618 begp1
= BUF_BEGV (bp1
);
1621 CHECK_NUMBER_COERCE_MARKER (start1
, 1);
1622 begp1
= XINT (start1
);
1625 endp1
= BUF_ZV (bp1
);
1628 CHECK_NUMBER_COERCE_MARKER (end1
, 2);
1629 endp1
= XINT (end1
);
1633 temp
= begp1
, begp1
= endp1
, endp1
= temp
;
1635 if (!(BUF_BEGV (bp1
) <= begp1
1637 && endp1
<= BUF_ZV (bp1
)))
1638 args_out_of_range (start1
, end1
);
1640 /* Likewise for second substring. */
1643 bp2
= current_buffer
;
1647 buf2
= Fget_buffer (buffer2
);
1650 bp2
= XBUFFER (buf2
);
1651 if (NILP (bp2
->name
))
1652 error ("Selecting deleted buffer");
1656 begp2
= BUF_BEGV (bp2
);
1659 CHECK_NUMBER_COERCE_MARKER (start2
, 4);
1660 begp2
= XINT (start2
);
1663 endp2
= BUF_ZV (bp2
);
1666 CHECK_NUMBER_COERCE_MARKER (end2
, 5);
1667 endp2
= XINT (end2
);
1671 temp
= begp2
, begp2
= endp2
, endp2
= temp
;
1673 if (!(BUF_BEGV (bp2
) <= begp2
1675 && endp2
<= BUF_ZV (bp2
)))
1676 args_out_of_range (start2
, end2
);
1678 beg1_byte
= buf_charpos_to_bytepos (bp1
, begp1
);
1679 beg2_byte
= buf_charpos_to_bytepos (bp2
, begp2
);
1680 len1
= buf_charpos_to_bytepos (bp1
, endp1
) - begp1
;
1681 len2
= buf_charpos_to_bytepos (bp2
, endp2
) - begp2
;
1686 for (i
= 0; i
< length
; i
++)
1688 unsigned char *p1
= BUF_BYTE_ADDRESS (bp1
, beg1_byte
+ i
);
1690 int c2
= *BUF_BYTE_ADDRESS (bp2
, beg2_byte
+ i
);
1692 /* If a character begins here,
1693 count the previous character now. */
1695 && (NILP (current_buffer
->enable_multibyte_characters
)
1696 || CHAR_HEAD_P (*p1
)))
1701 c1
= XINT (trt
[c1
]);
1702 c2
= XINT (trt
[c2
]);
1705 return make_number (- 1 - chars
);
1707 return make_number (chars
+ 1);
1710 /* The strings match as far as they go.
1711 If one is shorter, that one is less. */
1713 return make_number (chars
+ 1);
1714 else if (length
< len2
)
1715 return make_number (- chars
- 1);
1717 /* Same length too => they are equal. */
1718 return make_number (0);
1722 subst_char_in_region_unwind (arg
)
1725 return current_buffer
->undo_list
= arg
;
1729 subst_char_in_region_unwind_1 (arg
)
1732 return current_buffer
->filename
= arg
;
1735 DEFUN ("subst-char-in-region", Fsubst_char_in_region
,
1736 Ssubst_char_in_region
, 4, 5, 0,
1737 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1738 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1739 and don't mark the buffer as really changed.\n\
1740 Both characters must have the same length of multi-byte form.")
1741 (start
, end
, fromchar
, tochar
, noundo
)
1742 Lisp_Object start
, end
, fromchar
, tochar
, noundo
;
1744 register int pos
, stop
, i
, len
, end_byte
;
1746 unsigned char fromwork
[4], *fromstr
, towork
[4], *tostr
, *p
;
1747 int count
= specpdl_ptr
- specpdl
;
1749 validate_region (&start
, &end
);
1750 CHECK_NUMBER (fromchar
, 2);
1751 CHECK_NUMBER (tochar
, 3);
1753 if (! NILP (current_buffer
->enable_multibyte_characters
))
1755 len
= CHAR_STRING (XFASTINT (fromchar
), fromwork
, fromstr
);
1756 if (CHAR_STRING (XFASTINT (tochar
), towork
, tostr
) != len
)
1757 error ("Characters in subst-char-in-region have different byte-lengths");
1762 fromwork
[0] = XFASTINT (fromchar
), fromstr
= fromwork
;
1763 towork
[0] = XFASTINT (tochar
), tostr
= towork
;
1766 pos
= CHAR_TO_BYTE (XINT (start
));
1767 stop
= CHAR_TO_BYTE (XINT (end
));
1770 /* If we don't want undo, turn off putting stuff on the list.
1771 That's faster than getting rid of things,
1772 and it prevents even the entry for a first change.
1773 Also inhibit locking the file. */
1776 record_unwind_protect (subst_char_in_region_unwind
,
1777 current_buffer
->undo_list
);
1778 current_buffer
->undo_list
= Qt
;
1779 /* Don't do file-locking. */
1780 record_unwind_protect (subst_char_in_region_unwind_1
,
1781 current_buffer
->filename
);
1782 current_buffer
->filename
= Qnil
;
1786 stop
= min (stop
, GPT_BYTE
);
1787 p
= BYTE_POS_ADDR (pos
);
1792 if (pos
>= end_byte
) break;
1794 p
= BYTE_POS_ADDR (pos
);
1796 if (p
[0] == fromstr
[0]
1798 || (p
[1] == fromstr
[1]
1799 && (len
== 2 || (p
[2] == fromstr
[2]
1800 && (len
== 3 || p
[3] == fromstr
[3]))))))
1804 modify_region (current_buffer
, XINT (start
), XINT (end
));
1806 if (! NILP (noundo
))
1808 if (MODIFF
- 1 == SAVE_MODIFF
)
1810 if (MODIFF
- 1 == current_buffer
->auto_save_modified
)
1811 current_buffer
->auto_save_modified
++;
1818 record_change (pos
, len
);
1819 for (i
= 0; i
< len
; i
++) *p
++ = tostr
[i
];
1827 signal_after_change (XINT (start
),
1828 stop
- XINT (start
), stop
- XINT (start
));
1830 unbind_to (count
, Qnil
);
1834 DEFUN ("translate-region", Ftranslate_region
, Stranslate_region
, 3, 3, 0,
1835 "From START to END, translate characters according to TABLE.\n\
1836 TABLE is a string; the Nth character in it is the mapping\n\
1837 for the character with code N.\n\
1838 This function does not alter multibyte characters.\n\
1839 It returns the number of characters changed.")
1843 register Lisp_Object table
;
1845 register int pos_byte
, stop
; /* Limits of the region. */
1846 register unsigned char *tt
; /* Trans table. */
1847 register int nc
; /* New character. */
1848 int cnt
; /* Number of changes made. */
1849 int size
; /* Size of translate table. */
1852 validate_region (&start
, &end
);
1853 CHECK_STRING (table
, 2);
1855 size
= XSTRING (table
)->size_byte
;
1856 tt
= XSTRING (table
)->data
;
1858 pos_byte
= CHAR_TO_BYTE (XINT (start
));
1859 stop
= CHAR_TO_BYTE (XINT (end
));
1860 modify_region (current_buffer
, XINT (start
), XINT (end
));
1864 for (; pos_byte
< stop
; )
1866 register unsigned char *p
= BYTE_POS_ADDR (pos_byte
);
1870 oc
= STRING_CHAR_AND_LENGTH (p
, stop
- pos_byte
, len
);
1871 if (oc
< size
&& len
== 1)
1876 record_change (pos
, 1);
1878 signal_after_change (pos
, 1, 1);
1886 return make_number (cnt
);
1889 DEFUN ("delete-region", Fdelete_region
, Sdelete_region
, 2, 2, "r",
1890 "Delete the text between point and mark.\n\
1891 When called from a program, expects two arguments,\n\
1892 positions (integers or markers) specifying the stretch to be deleted.")
1894 Lisp_Object start
, end
;
1896 validate_region (&start
, &end
);
1897 del_range (XINT (start
), XINT (end
));
1901 DEFUN ("widen", Fwiden
, Swiden
, 0, 0, "",
1902 "Remove restrictions (narrowing) from current buffer.\n\
1903 This allows the buffer's full text to be seen and edited.")
1906 if (BEG
!= BEGV
|| Z
!= ZV
)
1907 current_buffer
->clip_changed
= 1;
1909 BEGV_BYTE
= BEG_BYTE
;
1910 SET_BUF_ZV_BOTH (current_buffer
, Z
, Z_BYTE
);
1911 /* Changing the buffer bounds invalidates any recorded current column. */
1912 invalidate_current_column ();
1916 DEFUN ("narrow-to-region", Fnarrow_to_region
, Snarrow_to_region
, 2, 2, "r",
1917 "Restrict editing in this buffer to the current region.\n\
1918 The rest of the text becomes temporarily invisible and untouchable\n\
1919 but is not deleted; if you save the buffer in a file, the invisible\n\
1920 text is included in the file. \\[widen] makes all visible again.\n\
1921 See also `save-restriction'.\n\
1923 When calling from a program, pass two arguments; positions (integers\n\
1924 or markers) bounding the text that should remain visible.")
1926 register Lisp_Object start
, end
;
1928 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1929 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1931 if (XINT (start
) > XINT (end
))
1934 tem
= start
; start
= end
; end
= tem
;
1937 if (!(BEG
<= XINT (start
) && XINT (start
) <= XINT (end
) && XINT (end
) <= Z
))
1938 args_out_of_range (start
, end
);
1940 if (BEGV
!= XFASTINT (start
) || ZV
!= XFASTINT (end
))
1941 current_buffer
->clip_changed
= 1;
1943 SET_BUF_BEGV (current_buffer
, XFASTINT (start
));
1944 SET_BUF_ZV (current_buffer
, XFASTINT (end
));
1945 if (PT
< XFASTINT (start
))
1946 SET_PT (XFASTINT (start
));
1947 if (PT
> XFASTINT (end
))
1948 SET_PT (XFASTINT (end
));
1949 /* Changing the buffer bounds invalidates any recorded current column. */
1950 invalidate_current_column ();
1955 save_restriction_save ()
1957 register Lisp_Object bottom
, top
;
1958 /* Note: I tried using markers here, but it does not win
1959 because insertion at the end of the saved region
1960 does not advance mh and is considered "outside" the saved region. */
1961 XSETFASTINT (bottom
, BEGV
- BEG
);
1962 XSETFASTINT (top
, Z
- ZV
);
1964 return Fcons (Fcurrent_buffer (), Fcons (bottom
, top
));
1968 save_restriction_restore (data
)
1971 register struct buffer
*buf
;
1972 register int newhead
, newtail
;
1973 register Lisp_Object tem
;
1976 buf
= XBUFFER (XCONS (data
)->car
);
1978 data
= XCONS (data
)->cdr
;
1980 tem
= XCONS (data
)->car
;
1981 newhead
= XINT (tem
);
1982 tem
= XCONS (data
)->cdr
;
1983 newtail
= XINT (tem
);
1984 if (newhead
+ newtail
> BUF_Z (buf
) - BUF_BEG (buf
))
1990 obegv
= BUF_BEGV (buf
);
1993 SET_BUF_BEGV (buf
, BUF_BEG (buf
) + newhead
);
1994 SET_BUF_ZV (buf
, BUF_Z (buf
) - newtail
);
1996 if (obegv
!= BUF_BEGV (buf
) || ozv
!= BUF_ZV (buf
))
1997 current_buffer
->clip_changed
= 1;
1999 /* If point is outside the new visible range, move it inside. */
2000 SET_BUF_PT_BOTH (buf
,
2001 clip_to_bounds (BUF_BEGV (buf
), BUF_PT (buf
), BUF_ZV (buf
)),
2002 clip_to_bounds (BUF_BEGV_BYTE (buf
), BUF_PT_BYTE (buf
),
2003 BUF_ZV_BYTE (buf
)));
2008 DEFUN ("save-restriction", Fsave_restriction
, Ssave_restriction
, 0, UNEVALLED
, 0,
2009 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2010 The buffer's restrictions make parts of the beginning and end invisible.\n\
2011 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2012 This special form, `save-restriction', saves the current buffer's restrictions\n\
2013 when it is entered, and restores them when it is exited.\n\
2014 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2015 The old restrictions settings are restored\n\
2016 even in case of abnormal exit (throw or error).\n\
2018 The value returned is the value of the last form in BODY.\n\
2020 `save-restriction' can get confused if, within the BODY, you widen\n\
2021 and then make changes outside the area within the saved restrictions.\n\
2023 Note: if you are using both `save-excursion' and `save-restriction',\n\
2024 use `save-excursion' outermost:\n\
2025 (save-excursion (save-restriction ...))")
2029 register Lisp_Object val
;
2030 int count
= specpdl_ptr
- specpdl
;
2032 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
2033 val
= Fprogn (body
);
2034 return unbind_to (count
, val
);
2037 /* Buffer for the most recent text displayed by Fmessage. */
2038 static char *message_text
;
2040 /* Allocated length of that buffer. */
2041 static int message_length
;
2043 DEFUN ("message", Fmessage
, Smessage
, 1, MANY
, 0,
2044 "Print a one-line message at the bottom of the screen.\n\
2045 The first argument is a format control string, and the rest are data\n\
2046 to be formatted under control of the string. See `format' for details.\n\
2048 If the first argument is nil, clear any existing message; let the\n\
2049 minibuffer contents show.")
2061 register Lisp_Object val
;
2062 val
= Fformat (nargs
, args
);
2063 /* Copy the data so that it won't move when we GC. */
2066 message_text
= (char *)xmalloc (80);
2067 message_length
= 80;
2069 if (XSTRING (val
)->size
> message_length
)
2071 message_length
= XSTRING (val
)->size_byte
;
2072 message_text
= (char *)xrealloc (message_text
, message_length
);
2074 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size_byte
);
2075 message2 (message_text
, XSTRING (val
)->size_byte
,
2076 STRING_MULTIBYTE (val
));
2081 DEFUN ("message-box", Fmessage_box
, Smessage_box
, 1, MANY
, 0,
2082 "Display a message, in a dialog box if possible.\n\
2083 If a dialog box is not available, use the echo area.\n\
2084 The first argument is a format control string, and the rest are data\n\
2085 to be formatted under control of the string. See `format' for details.\n\
2087 If the first argument is nil, clear any existing message; let the\n\
2088 minibuffer contents show.")
2100 register Lisp_Object val
;
2101 val
= Fformat (nargs
, args
);
2104 Lisp_Object pane
, menu
, obj
;
2105 struct gcpro gcpro1
;
2106 pane
= Fcons (Fcons (build_string ("OK"), Qt
), Qnil
);
2108 menu
= Fcons (val
, pane
);
2109 obj
= Fx_popup_dialog (Qt
, menu
);
2113 #else /* not HAVE_MENUS */
2114 /* Copy the data so that it won't move when we GC. */
2117 message_text
= (char *)xmalloc (80);
2118 message_length
= 80;
2120 if (XSTRING (val
)->size_byte
> message_length
)
2122 message_length
= XSTRING (val
)->size_byte
;
2123 message_text
= (char *)xrealloc (message_text
, message_length
);
2125 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size_byte
);
2126 message2 (message_text
, XSTRING (val
)->size_byte
);
2128 #endif /* not HAVE_MENUS */
2132 extern Lisp_Object last_nonmenu_event
;
2135 DEFUN ("message-or-box", Fmessage_or_box
, Smessage_or_box
, 1, MANY
, 0,
2136 "Display a message in a dialog box or in the echo area.\n\
2137 If this command was invoked with the mouse, use a dialog box.\n\
2138 Otherwise, use the echo area.\n\
2139 The first argument is a format control string, and the rest are data\n\
2140 to be formatted under control of the string. See `format' for details.\n\
2142 If the first argument is nil, clear any existing message; let the\n\
2143 minibuffer contents show.")
2149 if (NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2150 return Fmessage_box (nargs
, args
);
2152 return Fmessage (nargs
, args
);
2155 DEFUN ("current-message", Fcurrent_message
, Scurrent_message
, 0, 0, 0,
2156 "Return the string currently displayed in the echo area, or nil if none.")
2159 return (echo_area_glyphs
2160 ? make_string (echo_area_glyphs
, echo_area_glyphs_length
)
2164 /* Number of bytes that STRING will occupy when put into the result.
2165 MULTIBYTE is nonzero if the result should be multibyte. */
2167 #define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \
2168 (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \
2169 ? count_size_as_multibyte (XSTRING (STRING)->data, \
2170 XSTRING (STRING)->size_byte) \
2171 : XSTRING (STRING)->size_byte)
2173 DEFUN ("format", Fformat
, Sformat
, 1, MANY
, 0,
2174 "Format a string out of a control-string and arguments.\n\
2175 The first argument is a control string.\n\
2176 The other arguments are substituted into it to make the result, a string.\n\
2177 It may contain %-sequences meaning to substitute the next argument.\n\
2178 %s means print a string argument. Actually, prints any object, with `princ'.\n\
2179 %d means print as number in decimal (%o octal, %x hex).\n\
2180 %e means print a number in exponential notation.\n\
2181 %f means print a number in decimal-point notation.\n\
2182 %g means print a number in exponential notation\n\
2183 or decimal-point notation, whichever uses fewer characters.\n\
2184 %c means print a number as a single character.\n\
2185 %S means print any object as an s-expression (using prin1).\n\
2186 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2187 Use %% to put a single % into the output.")
2190 register Lisp_Object
*args
;
2192 register int n
; /* The number of the next arg to substitute */
2193 register int total
= 5; /* An estimate of the final length */
2195 register unsigned char *format
, *end
;
2197 /* Nonzero if the output should be a multibyte string,
2198 which is true if any of the inputs is one. */
2200 unsigned char *this_format
;
2201 int longest_format
= 0;
2204 extern char *index ();
2206 /* It should not be necessary to GCPRO ARGS, because
2207 the caller in the interpreter should take care of that. */
2209 for (n
= 0; n
< nargs
; n
++)
2210 if (STRINGP (args
[n
]) && STRING_MULTIBYTE (args
[n
]))
2213 CHECK_STRING (args
[0], 0);
2214 format
= XSTRING (args
[0])->data
;
2215 end
= format
+ XSTRING (args
[0])->size_byte
;
2217 /* Make room in result for all the non-%-codes in the control string. */
2218 total
+= CONVERTED_BYTE_SIZE (multibyte
, args
[0]);
2220 /* Add to TOTAL enough space to hold the converted arguments. */
2223 while (format
!= end
)
2224 if (*format
++ == '%')
2226 int minlen
, thissize
= 0;
2227 unsigned char *this_format_start
= format
- 1;
2229 /* Process a numeric arg and skip it. */
2230 minlen
= atoi (format
);
2234 while ((*format
>= '0' && *format
<= '9')
2235 || *format
== '-' || *format
== ' ' || *format
== '.')
2238 if (format
- this_format_start
+ 1 > longest_format
)
2239 longest_format
= format
- this_format_start
+ 1;
2243 else if (++n
>= nargs
)
2244 error ("Not enough arguments for format string");
2245 else if (*format
== 'S')
2247 /* For `S', prin1 the argument and then treat like a string. */
2248 register Lisp_Object tem
;
2249 tem
= Fprin1_to_string (args
[n
], Qnil
);
2250 if (STRING_MULTIBYTE (tem
))
2255 else if (SYMBOLP (args
[n
]))
2257 XSETSTRING (args
[n
], XSYMBOL (args
[n
])->name
);
2260 else if (STRINGP (args
[n
]))
2263 if (*format
!= 's' && *format
!= 'S')
2264 error ("format specifier doesn't match argument type");
2265 thissize
= CONVERTED_BYTE_SIZE (multibyte
, args
[n
]);
2267 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
2268 else if (INTEGERP (args
[n
]) && *format
!= 's')
2270 #ifdef LISP_FLOAT_TYPE
2271 /* The following loop assumes the Lisp type indicates
2272 the proper way to pass the argument.
2273 So make sure we have a flonum if the argument should
2275 if (*format
== 'e' || *format
== 'f' || *format
== 'g')
2276 args
[n
] = Ffloat (args
[n
]);
2280 #ifdef LISP_FLOAT_TYPE
2281 else if (FLOATP (args
[n
]) && *format
!= 's')
2283 if (! (*format
== 'e' || *format
== 'f' || *format
== 'g'))
2284 args
[n
] = Ftruncate (args
[n
], Qnil
);
2290 /* Anything but a string, convert to a string using princ. */
2291 register Lisp_Object tem
;
2292 tem
= Fprin1_to_string (args
[n
], Qt
);
2293 if (STRING_MULTIBYTE (tem
))
2299 if (thissize
< minlen
)
2302 total
+= thissize
+ 4;
2305 this_format
= (unsigned char *) alloca (longest_format
+ 1);
2307 /* Allocate the space for the result.
2308 Note that TOTAL is an overestimate. */
2310 buf
= (unsigned char *) alloca (total
+ 1);
2312 buf
= (unsigned char *) xmalloc (total
+ 1);
2318 /* Scan the format and store result in BUF. */
2319 format
= XSTRING (args
[0])->data
;
2320 while (format
!= end
)
2325 unsigned char *this_format_start
= format
;
2329 /* Process a numeric arg and skip it. */
2330 minlen
= atoi (format
);
2334 while ((*format
>= '0' && *format
<= '9')
2335 || *format
== '-' || *format
== ' ' || *format
== '.')
2338 if (*format
++ == '%')
2347 if (STRINGP (args
[n
]))
2349 int padding
, nbytes
;
2351 nbytes
= copy_text (XSTRING (args
[n
])->data
, p
,
2352 XSTRING (args
[n
])->size_byte
,
2353 STRING_MULTIBYTE (args
[n
]), multibyte
);
2355 nchars
+= XSTRING (args
[n
])->size
;
2357 /* If spec requires it, pad on right with spaces. */
2358 padding
= minlen
- XSTRING (args
[n
])->size
;
2359 while (padding
-- > 0)
2365 else if (INTEGERP (args
[n
]) || FLOATP (args
[n
]))
2369 bcopy (this_format_start
, this_format
,
2370 format
- this_format_start
);
2371 this_format
[format
- this_format_start
] = 0;
2373 sprintf (p
, this_format
, XINT (args
[n
]));
2375 this_nchars
= strlen (p
);
2377 nchars
+= this_nchars
;
2380 else if (multibyte
&& !STRING_MULTIBYTE (args
[0]))
2382 /* Convert a single-byte character to multibyte. */
2383 int len
= copy_text (format
, p
, 1, 0, 1);
2390 *p
++ = *format
++, nchars
++;
2393 val
= make_multibyte_string (buf
, nchars
, p
- buf
);
2395 /* If we allocated BUF with malloc, free it too. */
2405 format1 (string1
, arg0
, arg1
, arg2
, arg3
, arg4
)
2406 EMACS_INT arg0
, arg1
, arg2
, arg3
, arg4
;
2420 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, args
);
2422 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, &string1
+ 1);
2424 return build_string (buf
);
2427 DEFUN ("char-equal", Fchar_equal
, Schar_equal
, 2, 2, 0,
2428 "Return t if two characters match, optionally ignoring case.\n\
2429 Both arguments must be characters (i.e. integers).\n\
2430 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2432 register Lisp_Object c1
, c2
;
2435 CHECK_NUMBER (c1
, 0);
2436 CHECK_NUMBER (c2
, 1);
2438 if (XINT (c1
) == XINT (c2
))
2440 if (NILP (current_buffer
->case_fold_search
))
2443 /* Do these in separate statements,
2444 then compare the variables.
2445 because of the way DOWNCASE uses temp variables. */
2446 i1
= DOWNCASE (XFASTINT (c1
));
2447 i2
= DOWNCASE (XFASTINT (c2
));
2448 return (i1
== i2
? Qt
: Qnil
);
2451 /* Transpose the markers in two regions of the current buffer, and
2452 adjust the ones between them if necessary (i.e.: if the regions
2455 START1, END1 are the character positions of the first region.
2456 START1_BYTE, END1_BYTE are the byte positions.
2457 START2, END2 are the character positions of the second region.
2458 START2_BYTE, END2_BYTE are the byte positions.
2460 Traverses the entire marker list of the buffer to do so, adding an
2461 appropriate amount to some, subtracting from some, and leaving the
2462 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2464 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
2467 transpose_markers (start1
, end1
, start2
, end2
,
2468 start1_byte
, end1_byte
, start2_byte
, end2_byte
)
2469 register int start1
, end1
, start2
, end2
;
2470 register int start1_byte
, end1_byte
, start2_byte
, end2_byte
;
2472 register int amt1
, amt1_byte
, amt2
, amt2_byte
, diff
, diff_byte
, mpos
;
2473 register Lisp_Object marker
;
2475 /* Update point as if it were a marker. */
2479 TEMP_SET_PT_BOTH (PT
+ (end2
- end1
),
2480 PT_BYTE
+ (end2_byte
- end1_byte
));
2481 else if (PT
< start2
)
2482 TEMP_SET_PT_BOTH (PT
+ (end2
- start2
) - (end1
- start1
),
2483 (PT_BYTE
+ (end2_byte
- start2_byte
)
2484 - (end1_byte
- start1_byte
)));
2486 TEMP_SET_PT_BOTH (PT
- (start2
- start1
),
2487 PT_BYTE
- (start2_byte
- start1_byte
));
2489 /* We used to adjust the endpoints here to account for the gap, but that
2490 isn't good enough. Even if we assume the caller has tried to move the
2491 gap out of our way, it might still be at start1 exactly, for example;
2492 and that places it `inside' the interval, for our purposes. The amount
2493 of adjustment is nontrivial if there's a `denormalized' marker whose
2494 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2495 the dirty work to Fmarker_position, below. */
2497 /* The difference between the region's lengths */
2498 diff
= (end2
- start2
) - (end1
- start1
);
2499 diff_byte
= (end2_byte
- start2_byte
) - (end1_byte
- start1_byte
);
2501 /* For shifting each marker in a region by the length of the other
2502 region plus the distance between the regions. */
2503 amt1
= (end2
- start2
) + (start2
- end1
);
2504 amt2
= (end1
- start1
) + (start2
- end1
);
2505 amt1_byte
= (end2_byte
- start2_byte
) + (start2_byte
- end1_byte
);
2506 amt2_byte
= (end1_byte
- start1_byte
) + (start2_byte
- end1_byte
);
2508 for (marker
= BUF_MARKERS (current_buffer
); !NILP (marker
);
2509 marker
= XMARKER (marker
)->chain
)
2511 mpos
= marker_byte_position (marker
);
2512 if (mpos
>= start1_byte
&& mpos
< end2_byte
)
2514 if (mpos
< end1_byte
)
2516 else if (mpos
< start2_byte
)
2520 XMARKER (marker
)->bytepos
= mpos
;
2522 mpos
= XMARKER (marker
)->charpos
;
2523 if (mpos
>= start1
&& mpos
< end2
)
2527 else if (mpos
< start2
)
2532 XMARKER (marker
)->charpos
= mpos
;
2536 DEFUN ("transpose-regions", Ftranspose_regions
, Stranspose_regions
, 4, 5, 0,
2537 "Transpose region START1 to END1 with START2 to END2.\n\
2538 The regions may not be overlapping, because the size of the buffer is\n\
2539 never changed in a transposition.\n\
2541 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update\n\
2542 any markers that happen to be located in the regions.\n\
2544 Transposing beyond buffer boundaries is an error.")
2545 (startr1
, endr1
, startr2
, endr2
, leave_markers
)
2546 Lisp_Object startr1
, endr1
, startr2
, endr2
, leave_markers
;
2548 register int start1
, end1
, start2
, end2
;
2549 int start1_byte
, start2_byte
, len1_byte
, len2_byte
;
2550 int gap
, len1
, len_mid
, len2
;
2551 unsigned char *start1_addr
, *start2_addr
, *temp
;
2553 #ifdef USE_TEXT_PROPERTIES
2554 INTERVAL cur_intv
, tmp_interval1
, tmp_interval_mid
, tmp_interval2
;
2555 cur_intv
= BUF_INTERVALS (current_buffer
);
2556 #endif /* USE_TEXT_PROPERTIES */
2558 validate_region (&startr1
, &endr1
);
2559 validate_region (&startr2
, &endr2
);
2561 start1
= XFASTINT (startr1
);
2562 end1
= XFASTINT (endr1
);
2563 start2
= XFASTINT (startr2
);
2564 end2
= XFASTINT (endr2
);
2567 /* Swap the regions if they're reversed. */
2570 register int glumph
= start1
;
2578 len1
= end1
- start1
;
2579 len2
= end2
- start2
;
2582 error ("Transposed regions not properly ordered");
2583 else if (start1
== end1
|| start2
== end2
)
2584 error ("Transposed region may not be of length 0");
2586 /* The possibilities are:
2587 1. Adjacent (contiguous) regions, or separate but equal regions
2588 (no, really equal, in this case!), or
2589 2. Separate regions of unequal size.
2591 The worst case is usually No. 2. It means that (aside from
2592 potential need for getting the gap out of the way), there also
2593 needs to be a shifting of the text between the two regions. So
2594 if they are spread far apart, we are that much slower... sigh. */
2596 /* It must be pointed out that the really studly thing to do would
2597 be not to move the gap at all, but to leave it in place and work
2598 around it if necessary. This would be extremely efficient,
2599 especially considering that people are likely to do
2600 transpositions near where they are working interactively, which
2601 is exactly where the gap would be found. However, such code
2602 would be much harder to write and to read. So, if you are
2603 reading this comment and are feeling squirrely, by all means have
2604 a go! I just didn't feel like doing it, so I will simply move
2605 the gap the minimum distance to get it out of the way, and then
2606 deal with an unbroken array. */
2608 /* Make sure the gap won't interfere, by moving it out of the text
2609 we will operate on. */
2610 if (start1
< gap
&& gap
< end2
)
2612 if (gap
- start1
< end2
- gap
)
2618 start1_byte
= CHAR_TO_BYTE (start1
);
2619 start2_byte
= CHAR_TO_BYTE (start2
);
2620 len1_byte
= CHAR_TO_BYTE (end1
) - start1_byte
;
2621 len2_byte
= CHAR_TO_BYTE (end2
) - start2_byte
;
2623 /* Hmmm... how about checking to see if the gap is large
2624 enough to use as the temporary storage? That would avoid an
2625 allocation... interesting. Later, don't fool with it now. */
2627 /* Working without memmove, for portability (sigh), so must be
2628 careful of overlapping subsections of the array... */
2630 if (end1
== start2
) /* adjacent regions */
2632 modify_region (current_buffer
, start1
, end2
);
2633 record_change (start1
, len1
+ len2
);
2635 #ifdef USE_TEXT_PROPERTIES
2636 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2637 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2638 Fset_text_properties (make_number (start1
), make_number (end2
),
2640 #endif /* USE_TEXT_PROPERTIES */
2642 /* First region smaller than second. */
2643 if (len1_byte
< len2_byte
)
2645 /* We use alloca only if it is small,
2646 because we want to avoid stack overflow. */
2647 if (len2_byte
> 20000)
2648 temp
= (unsigned char *) xmalloc (len2_byte
);
2650 temp
= (unsigned char *) alloca (len2_byte
);
2652 /* Don't precompute these addresses. We have to compute them
2653 at the last minute, because the relocating allocator might
2654 have moved the buffer around during the xmalloc. */
2655 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1_byte
);
2656 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2_byte
);
2658 bcopy (start2_addr
, temp
, len2_byte
);
2659 bcopy (start1_addr
, start1_addr
+ len2_byte
, len1_byte
);
2660 bcopy (temp
, start1_addr
, len2_byte
);
2661 if (len2_byte
> 20000)
2665 /* First region not smaller than second. */
2667 if (len1_byte
> 20000)
2668 temp
= (unsigned char *) xmalloc (len1_byte
);
2670 temp
= (unsigned char *) alloca (len1_byte
);
2671 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1_byte
);
2672 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2_byte
);
2673 bcopy (start1_addr
, temp
, len1_byte
);
2674 bcopy (start2_addr
, start1_addr
, len2_byte
);
2675 bcopy (temp
, start1_addr
+ len2_byte
, len1_byte
);
2676 if (len1_byte
> 20000)
2679 #ifdef USE_TEXT_PROPERTIES
2680 graft_intervals_into_buffer (tmp_interval1
, start1
+ len2
,
2681 len1
, current_buffer
, 0);
2682 graft_intervals_into_buffer (tmp_interval2
, start1
,
2683 len2
, current_buffer
, 0);
2684 #endif /* USE_TEXT_PROPERTIES */
2686 /* Non-adjacent regions, because end1 != start2, bleagh... */
2689 len_mid
= start2_byte
- (start1_byte
+ len1_byte
);
2691 if (len1_byte
== len2_byte
)
2692 /* Regions are same size, though, how nice. */
2694 modify_region (current_buffer
, start1
, end1
);
2695 modify_region (current_buffer
, start2
, end2
);
2696 record_change (start1
, len1
);
2697 record_change (start2
, len2
);
2698 #ifdef USE_TEXT_PROPERTIES
2699 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2700 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2701 Fset_text_properties (make_number (start1
), make_number (end1
),
2703 Fset_text_properties (make_number (start2
), make_number (end2
),
2705 #endif /* USE_TEXT_PROPERTIES */
2707 if (len1_byte
> 20000)
2708 temp
= (unsigned char *) xmalloc (len1_byte
);
2710 temp
= (unsigned char *) alloca (len1_byte
);
2711 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1_byte
);
2712 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2_byte
);
2713 bcopy (start1_addr
, temp
, len1_byte
);
2714 bcopy (start2_addr
, start1_addr
, len2_byte
);
2715 bcopy (temp
, start2_addr
, len1_byte
);
2716 if (len1_byte
> 20000)
2718 #ifdef USE_TEXT_PROPERTIES
2719 graft_intervals_into_buffer (tmp_interval1
, start2
,
2720 len1
, current_buffer
, 0);
2721 graft_intervals_into_buffer (tmp_interval2
, start1
,
2722 len2
, current_buffer
, 0);
2723 #endif /* USE_TEXT_PROPERTIES */
2726 else if (len1_byte
< len2_byte
) /* Second region larger than first */
2727 /* Non-adjacent & unequal size, area between must also be shifted. */
2729 modify_region (current_buffer
, start1
, end2
);
2730 record_change (start1
, (end2
- start1
));
2731 #ifdef USE_TEXT_PROPERTIES
2732 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2733 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2734 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2735 Fset_text_properties (make_number (start1
), make_number (end2
),
2737 #endif /* USE_TEXT_PROPERTIES */
2739 /* holds region 2 */
2740 if (len2_byte
> 20000)
2741 temp
= (unsigned char *) xmalloc (len2_byte
);
2743 temp
= (unsigned char *) alloca (len2_byte
);
2744 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1_byte
);
2745 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2_byte
);
2746 bcopy (start2_addr
, temp
, len2_byte
);
2747 bcopy (start1_addr
, start1_addr
+ len_mid
+ len2_byte
, len1_byte
);
2748 safe_bcopy (start1_addr
+ len1_byte
, start1_addr
+ len2_byte
, len_mid
);
2749 bcopy (temp
, start1_addr
, len2_byte
);
2750 if (len2_byte
> 20000)
2752 #ifdef USE_TEXT_PROPERTIES
2753 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2754 len1
, current_buffer
, 0);
2755 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2756 len_mid
, current_buffer
, 0);
2757 graft_intervals_into_buffer (tmp_interval2
, start1
,
2758 len2
, current_buffer
, 0);
2759 #endif /* USE_TEXT_PROPERTIES */
2762 /* Second region smaller than first. */
2764 record_change (start1
, (end2
- start1
));
2765 modify_region (current_buffer
, start1
, end2
);
2767 #ifdef USE_TEXT_PROPERTIES
2768 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2769 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2770 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2771 Fset_text_properties (make_number (start1
), make_number (end2
),
2773 #endif /* USE_TEXT_PROPERTIES */
2775 /* holds region 1 */
2776 if (len1_byte
> 20000)
2777 temp
= (unsigned char *) xmalloc (len1_byte
);
2779 temp
= (unsigned char *) alloca (len1_byte
);
2780 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1_byte
);
2781 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2_byte
);
2782 bcopy (start1_addr
, temp
, len1_byte
);
2783 bcopy (start2_addr
, start1_addr
, len2_byte
);
2784 bcopy (start1_addr
+ len1_byte
, start1_addr
+ len2_byte
, len_mid
);
2785 bcopy (temp
, start1_addr
+ len2_byte
+ len_mid
, len1_byte
);
2786 if (len1_byte
> 20000)
2788 #ifdef USE_TEXT_PROPERTIES
2789 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2790 len1
, current_buffer
, 0);
2791 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2792 len_mid
, current_buffer
, 0);
2793 graft_intervals_into_buffer (tmp_interval2
, start1
,
2794 len2
, current_buffer
, 0);
2795 #endif /* USE_TEXT_PROPERTIES */
2799 /* When doing multiple transpositions, it might be nice
2800 to optimize this. Perhaps the markers in any one buffer
2801 should be organized in some sorted data tree. */
2802 if (NILP (leave_markers
))
2804 transpose_markers (start1
, end1
, start2
, end2
,
2805 start1_byte
, start1_byte
+ len1_byte
,
2806 start2_byte
, start2_byte
+ len2_byte
);
2807 fix_overlays_in_range (start1
, end2
);
2819 Qbuffer_access_fontify_functions
2820 = intern ("buffer-access-fontify-functions");
2821 staticpro (&Qbuffer_access_fontify_functions
);
2823 DEFVAR_LISP ("buffer-access-fontify-functions",
2824 &Vbuffer_access_fontify_functions
,
2825 "List of functions called by `buffer-substring' to fontify if necessary.\n\
2826 Each function is called with two arguments which specify the range\n\
2827 of the buffer being accessed.");
2828 Vbuffer_access_fontify_functions
= Qnil
;
2832 extern Lisp_Object Vprin1_to_string_buffer
;
2833 obuf
= Fcurrent_buffer ();
2834 /* Do this here, because init_buffer_once is too early--it won't work. */
2835 Fset_buffer (Vprin1_to_string_buffer
);
2836 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
2837 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
2842 DEFVAR_LISP ("buffer-access-fontified-property",
2843 &Vbuffer_access_fontified_property
,
2844 "Property which (if non-nil) indicates text has been fontified.\n\
2845 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
2846 functions if all the text being accessed has this property.");
2847 Vbuffer_access_fontified_property
= Qnil
;
2849 DEFVAR_LISP ("system-name", &Vsystem_name
,
2850 "The name of the machine Emacs is running on.");
2852 DEFVAR_LISP ("user-full-name", &Vuser_full_name
,
2853 "The full name of the user logged in.");
2855 DEFVAR_LISP ("user-login-name", &Vuser_login_name
,
2856 "The user's name, taken from environment variables if possible.");
2858 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name
,
2859 "The user's name, based upon the real uid only.");
2861 defsubr (&Schar_equal
);
2862 defsubr (&Sgoto_char
);
2863 defsubr (&Sstring_to_char
);
2864 defsubr (&Schar_to_string
);
2865 defsubr (&Sbuffer_substring
);
2866 defsubr (&Sbuffer_substring_no_properties
);
2867 defsubr (&Sbuffer_string
);
2869 defsubr (&Spoint_marker
);
2870 defsubr (&Smark_marker
);
2872 defsubr (&Sregion_beginning
);
2873 defsubr (&Sregion_end
);
2874 /* defsubr (&Smark); */
2875 /* defsubr (&Sset_mark); */
2876 defsubr (&Ssave_excursion
);
2877 defsubr (&Ssave_current_buffer
);
2879 defsubr (&Sbufsize
);
2880 defsubr (&Spoint_max
);
2881 defsubr (&Spoint_min
);
2882 defsubr (&Spoint_min_marker
);
2883 defsubr (&Spoint_max_marker
);
2885 defsubr (&Sline_beginning_position
);
2886 defsubr (&Sline_end_position
);
2892 defsubr (&Sfollowing_char
);
2893 defsubr (&Sprevious_char
);
2894 defsubr (&Schar_after
);
2895 defsubr (&Schar_before
);
2897 defsubr (&Sinsert_before_markers
);
2898 defsubr (&Sinsert_and_inherit
);
2899 defsubr (&Sinsert_and_inherit_before_markers
);
2900 defsubr (&Sinsert_char
);
2902 defsubr (&Suser_login_name
);
2903 defsubr (&Suser_real_login_name
);
2904 defsubr (&Suser_uid
);
2905 defsubr (&Suser_real_uid
);
2906 defsubr (&Suser_full_name
);
2907 defsubr (&Semacs_pid
);
2908 defsubr (&Scurrent_time
);
2909 defsubr (&Sformat_time_string
);
2910 defsubr (&Sdecode_time
);
2911 defsubr (&Sencode_time
);
2912 defsubr (&Scurrent_time_string
);
2913 defsubr (&Scurrent_time_zone
);
2914 defsubr (&Sset_time_zone_rule
);
2915 defsubr (&Ssystem_name
);
2916 defsubr (&Smessage
);
2917 defsubr (&Smessage_box
);
2918 defsubr (&Smessage_or_box
);
2919 defsubr (&Scurrent_message
);
2922 defsubr (&Sinsert_buffer_substring
);
2923 defsubr (&Scompare_buffer_substrings
);
2924 defsubr (&Ssubst_char_in_region
);
2925 defsubr (&Stranslate_region
);
2926 defsubr (&Sdelete_region
);
2928 defsubr (&Snarrow_to_region
);
2929 defsubr (&Ssave_restriction
);
2930 defsubr (&Stranspose_regions
);