1 /* Lisp functions pertaining to editing.
2 Copyright (C) 1985,86,87,89,93,94,95,96,97 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 void set_time_zone_rule ();
54 Lisp_Object Vbuffer_access_fontify_functions
;
55 Lisp_Object Qbuffer_access_fontify_functions
;
56 Lisp_Object Vbuffer_access_fontified_property
;
58 Lisp_Object
Fuser_full_name ();
60 /* Some static data, and a function to initialize it for each run */
62 Lisp_Object Vsystem_name
;
63 Lisp_Object Vuser_real_login_name
; /* login name of current user ID */
64 Lisp_Object Vuser_full_name
; /* full name of current user */
65 Lisp_Object Vuser_login_name
; /* user name from LOGNAME or USER */
71 register unsigned char *p
, *q
, *r
;
72 struct passwd
*pw
; /* password entry for the current user */
75 /* Set up system_name even when dumping. */
79 /* Don't bother with this on initial start when just dumping out */
82 #endif /* not CANNOT_DUMP */
84 pw
= (struct passwd
*) getpwuid (getuid ());
86 /* We let the real user name default to "root" because that's quite
87 accurate on MSDOG and because it lets Emacs find the init file.
88 (The DVX libraries override the Djgpp libraries here.) */
89 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "root");
91 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "unknown");
94 /* Get the effective user name, by consulting environment variables,
95 or the effective uid if those are unset. */
96 user_name
= (char *) getenv ("LOGNAME");
99 user_name
= (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
100 #else /* WINDOWSNT */
101 user_name
= (char *) getenv ("USER");
102 #endif /* WINDOWSNT */
105 pw
= (struct passwd
*) getpwuid (geteuid ());
106 user_name
= (char *) (pw
? pw
->pw_name
: "unknown");
108 Vuser_login_name
= build_string (user_name
);
110 /* If the user name claimed in the environment vars differs from
111 the real uid, use the claimed name to find the full name. */
112 tem
= Fstring_equal (Vuser_login_name
, Vuser_real_login_name
);
113 Vuser_full_name
= Fuser_full_name (NILP (tem
)? make_number (geteuid())
116 p
= (unsigned char *) getenv ("NAME");
118 Vuser_full_name
= build_string (p
);
119 else if (NILP (Vuser_full_name
))
120 Vuser_full_name
= build_string ("unknown");
123 DEFUN ("char-to-string", Fchar_to_string
, Schar_to_string
, 1, 1, 0,
124 "Convert arg CHAR to a string containing multi-byte form of that character.")
126 Lisp_Object character
;
129 char workbuf
[4], *str
;
131 CHECK_NUMBER (character
, 0);
133 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
134 return make_string (str
, len
);
137 DEFUN ("string-to-char", Fstring_to_char
, Sstring_to_char
, 1, 1, 0,
138 "Convert arg STRING to a character, the first character of that string.\n\
139 A multibyte character is handled correctly.")
141 register Lisp_Object string
;
143 register Lisp_Object val
;
144 register struct Lisp_String
*p
;
145 CHECK_STRING (string
, 0);
146 p
= XSTRING (string
);
148 XSETFASTINT (val
, STRING_CHAR (p
->data
, p
->size
));
150 XSETFASTINT (val
, 0);
154 DEFUN ("sref", Fsref
, Ssref
, 2, 2, 0,
155 "Return the character in STRING at INDEX. INDEX starts at 0.\n\
156 A multibyte character is handled correctly.\n\
157 INDEX not pointing at character boundary is an error.")
159 Lisp_Object str
, idx
;
161 register int idxval
, len
;
162 register unsigned char *p
;
163 register Lisp_Object val
;
165 CHECK_STRING (str
, 0);
166 CHECK_NUMBER (idx
, 1);
168 if (idxval
< 0 || idxval
>= (len
= XVECTOR (str
)->size
))
169 args_out_of_range (str
, idx
);
170 p
= XSTRING (str
)->data
+ idxval
;
171 if (!CHAR_HEAD_P (p
))
172 error ("Not character boundary");
174 len
= XSTRING (str
)->size
- idxval
;
175 XSETFASTINT (val
, STRING_CHAR (p
, len
));
184 register Lisp_Object mark
;
185 mark
= Fmake_marker ();
186 Fset_marker (mark
, make_number (val
), Qnil
);
190 DEFUN ("point", Fpoint
, Spoint
, 0, 0, 0,
191 "Return value of point, as an integer.\n\
192 Beginning of buffer is position (point-min)")
196 XSETFASTINT (temp
, PT
);
200 DEFUN ("point-marker", Fpoint_marker
, Spoint_marker
, 0, 0, 0,
201 "Return value of point, as a marker object.")
204 return buildmark (PT
);
208 clip_to_bounds (lower
, num
, upper
)
209 int lower
, num
, upper
;
213 else if (num
> upper
)
219 DEFUN ("goto-char", Fgoto_char
, Sgoto_char
, 1, 1, "NGoto char: ",
220 "Set point to POSITION, a number or marker.\n\
221 Beginning of buffer is position (point-min), end is (point-max).\n\
222 If the position is in the middle of a multibyte form,\n\
223 the actual point is set at the head of the multibyte form\n\
224 except in the case that `enable-multibyte-characters' is nil.")
226 register Lisp_Object position
;
231 CHECK_NUMBER_COERCE_MARKER (position
, 0);
233 pos
= clip_to_bounds (BEGV
, XINT (position
), ZV
);
234 /* If POS is in a middle of multi-byte form (i.e. *P >= 0xA0), we
235 must decrement POS until it points the head of the multi-byte
237 if (!NILP (current_buffer
->enable_multibyte_characters
)
238 && *(p
= POS_ADDR (pos
)) >= 0xA0
241 /* Since a multi-byte form does not contain the gap, POS should
242 not stride over the gap while it is being decreased. So, we
243 set the limit as below. */
244 unsigned char *p_min
= pos
< GPT
? BEG_ADDR
: GAP_END_ADDR
;
245 unsigned int saved_pos
= pos
;
249 } while (p
> p_min
&& *p
>= 0xA0);
251 /* This was an invalid multi-byte form. */
253 XSETFASTINT (position
, pos
);
260 region_limit (beginningp
)
263 extern Lisp_Object Vmark_even_if_inactive
; /* Defined in callint.c. */
264 register Lisp_Object m
;
265 if (!NILP (Vtransient_mark_mode
) && NILP (Vmark_even_if_inactive
)
266 && NILP (current_buffer
->mark_active
))
267 Fsignal (Qmark_inactive
, Qnil
);
268 m
= Fmarker_position (current_buffer
->mark
);
269 if (NILP (m
)) error ("There is no region now");
270 if ((PT
< XFASTINT (m
)) == beginningp
)
271 return (make_number (PT
));
276 DEFUN ("region-beginning", Fregion_beginning
, Sregion_beginning
, 0, 0, 0,
277 "Return position of beginning of region, as an integer.")
280 return (region_limit (1));
283 DEFUN ("region-end", Fregion_end
, Sregion_end
, 0, 0, 0,
284 "Return position of end of region, as an integer.")
287 return (region_limit (0));
290 DEFUN ("mark-marker", Fmark_marker
, Smark_marker
, 0, 0, 0,
291 "Return this buffer's mark, as a marker object.\n\
292 Watch out! Moving this marker changes the mark position.\n\
293 If you set the marker not to point anywhere, the buffer will have no mark.")
296 return current_buffer
->mark
;
299 DEFUN ("line-beginning-position", Fline_beginning_position
, Sline_beginning_position
,
301 "Return the character position of the first character on the current line.\n\
302 With argument N not nil or 1, move forward N - 1 lines first.\n\
303 If scan reaches end of buffer, return that position.\n\
304 This function does not move point.")
308 register int orig
, end
;
316 Fforward_line (make_number (XINT (n
) - 1));
320 return make_number (end
);
323 DEFUN ("line-end-position", Fline_end_position
, Sline_end_position
,
325 "Return the character position of the last character on the current line.\n\
326 With argument N not nil or 1, move forward N - 1 lines first.\n\
327 If scan reaches end of buffer, return that position.\n\
328 This function does not move point.")
337 return make_number (find_before_next_newline
338 (PT
, 0, XINT (n
) - (XINT (n
) <= 0)));
342 save_excursion_save ()
344 register int visible
= (XBUFFER (XWINDOW (selected_window
)->buffer
)
347 return Fcons (Fpoint_marker (),
348 Fcons (Fcopy_marker (current_buffer
->mark
, Qnil
),
349 Fcons (visible
? Qt
: Qnil
,
350 current_buffer
->mark_active
)));
354 save_excursion_restore (info
)
357 Lisp_Object tem
, tem1
, omark
, nmark
;
358 struct gcpro gcpro1
, gcpro2
, gcpro3
;
360 tem
= Fmarker_buffer (Fcar (info
));
361 /* If buffer being returned to is now deleted, avoid error */
362 /* Otherwise could get error here while unwinding to top level
364 /* In that case, Fmarker_buffer returns nil now. */
368 omark
= nmark
= Qnil
;
369 GCPRO3 (info
, omark
, nmark
);
374 unchain_marker (tem
);
375 tem
= Fcar (Fcdr (info
));
376 omark
= Fmarker_position (current_buffer
->mark
);
377 Fset_marker (current_buffer
->mark
, tem
, Fcurrent_buffer ());
378 nmark
= Fmarker_position (tem
);
379 unchain_marker (tem
);
380 tem
= Fcdr (Fcdr (info
));
381 #if 0 /* We used to make the current buffer visible in the selected window
382 if that was true previously. That avoids some anomalies.
383 But it creates others, and it wasn't documented, and it is simpler
384 and cleaner never to alter the window/buffer connections. */
387 && current_buffer
!= XBUFFER (XWINDOW (selected_window
)->buffer
))
388 Fswitch_to_buffer (Fcurrent_buffer (), Qnil
);
391 tem1
= current_buffer
->mark_active
;
392 current_buffer
->mark_active
= Fcdr (tem
);
393 if (!NILP (Vrun_hooks
))
395 /* If mark is active now, and either was not active
396 or was at a different place, run the activate hook. */
397 if (! NILP (current_buffer
->mark_active
))
399 if (! EQ (omark
, nmark
))
400 call1 (Vrun_hooks
, intern ("activate-mark-hook"));
402 /* If mark has ceased to be active, run deactivate hook. */
403 else if (! NILP (tem1
))
404 call1 (Vrun_hooks
, intern ("deactivate-mark-hook"));
410 DEFUN ("save-excursion", Fsave_excursion
, Ssave_excursion
, 0, UNEVALLED
, 0,
411 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
412 Executes BODY just like `progn'.\n\
413 The values of point, mark and the current buffer are restored\n\
414 even in case of abnormal exit (throw or error).\n\
415 The state of activation of the mark is also restored.")
419 register Lisp_Object val
;
420 int count
= specpdl_ptr
- specpdl
;
422 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
425 return unbind_to (count
, val
);
428 DEFUN ("save-current-buffer", Fsave_current_buffer
, Ssave_current_buffer
, 0, UNEVALLED
, 0,
429 "Save the current buffer; execute BODY; restore the current buffer.\n\
430 Executes BODY just like `progn'.")
434 register Lisp_Object val
;
435 int count
= specpdl_ptr
- specpdl
;
437 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
440 return unbind_to (count
, val
);
443 DEFUN ("buffer-size", Fbufsize
, Sbufsize
, 0, 0, 0,
444 "Return the number of characters in the current buffer.")
448 XSETFASTINT (temp
, Z
- BEG
);
452 DEFUN ("point-min", Fpoint_min
, Spoint_min
, 0, 0, 0,
453 "Return the minimum permissible value of point in the current buffer.\n\
454 This is 1, unless narrowing (a buffer restriction) is in effect.")
458 XSETFASTINT (temp
, BEGV
);
462 DEFUN ("point-min-marker", Fpoint_min_marker
, Spoint_min_marker
, 0, 0, 0,
463 "Return a marker to the minimum permissible value of point in this buffer.\n\
464 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
467 return buildmark (BEGV
);
470 DEFUN ("point-max", Fpoint_max
, Spoint_max
, 0, 0, 0,
471 "Return the maximum permissible value of point in the current buffer.\n\
472 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
473 is in effect, in which case it is less.")
477 XSETFASTINT (temp
, ZV
);
481 DEFUN ("point-max-marker", Fpoint_max_marker
, Spoint_max_marker
, 0, 0, 0,
482 "Return a marker to the maximum permissible value of point in this buffer.\n\
483 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
484 is in effect, in which case it is less.")
487 return buildmark (ZV
);
490 DEFUN ("following-char", Ffollowing_char
, Sfollowing_char
, 0, 0, 0,
491 "Return the character following point, as a number.\n\
492 At the end of the buffer or accessible region, return 0.\n\
493 If `enable-multibyte-characters' is nil or point is not\n\
494 at character boundary, multibyte form is ignored,\n\
495 and only one byte following point is returned as a character.")
500 XSETFASTINT (temp
, 0);
502 XSETFASTINT (temp
, FETCH_CHAR (PT
));
506 DEFUN ("preceding-char", Fprevious_char
, Sprevious_char
, 0, 0, 0,
507 "Return the character preceding point, as a number.\n\
508 At the beginning of the buffer or accessible region, return 0.\n\
509 If `enable-multibyte-characters' is nil or point is not\n\
510 at character boundary, multi-byte form is ignored,\n\
511 and only one byte preceding point is returned as a character.")
516 XSETFASTINT (temp
, 0);
517 else if (!NILP (current_buffer
->enable_multibyte_characters
))
521 XSETFASTINT (temp
, FETCH_CHAR (pos
));
524 XSETFASTINT (temp
, FETCH_BYTE (PT
- 1));
528 DEFUN ("bobp", Fbobp
, Sbobp
, 0, 0, 0,
529 "Return T if point is at the beginning of the buffer.\n\
530 If the buffer is narrowed, this means the beginning of the narrowed part.")
538 DEFUN ("eobp", Feobp
, Seobp
, 0, 0, 0,
539 "Return T if point is at the end of the buffer.\n\
540 If the buffer is narrowed, this means the end of the narrowed part.")
548 DEFUN ("bolp", Fbolp
, Sbolp
, 0, 0, 0,
549 "Return T if point is at the beginning of a line.")
552 if (PT
== BEGV
|| FETCH_BYTE (PT
- 1) == '\n')
557 DEFUN ("eolp", Feolp
, Seolp
, 0, 0, 0,
558 "Return T if point is at the end of a line.\n\
559 `End of a line' includes point being at the end of the buffer.")
562 if (PT
== ZV
|| FETCH_BYTE (PT
) == '\n')
567 DEFUN ("char-after", Fchar_after
, Schar_after
, 0, 1, 0,
568 "Return character in current buffer at position POS.\n\
569 POS is an integer or a buffer pointer.\n\
570 If POS is out of range, the value is nil.\n\
571 If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
572 multi-byte form is ignored, and only one byte at POS\n\
573 is returned as a character.")
577 register Lisp_Object val
;
584 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
587 if (n
< BEGV
|| n
>= ZV
)
591 XSETFASTINT (val
, FETCH_CHAR (n
));
595 DEFUN ("char-before", Fchar_before
, Schar_before
, 0, 1, 0,
596 "Return character in current buffer preceding position POS.\n\
597 POS is an integer or a buffer pointer.\n\
598 If POS is out of range, the value is nil.\n\
599 If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
600 multi-byte form is ignored, and only one byte preceding POS\n\
601 is returned as a character.")
605 register Lisp_Object val
;
612 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
617 if (n
<= BEGV
|| n
> ZV
)
620 if (!NILP (current_buffer
->enable_multibyte_characters
))
623 XSETFASTINT (val
, FETCH_CHAR (n
));
628 XSETFASTINT (val
, FETCH_BYTE (n
));
633 DEFUN ("user-login-name", Fuser_login_name
, Suser_login_name
, 0, 1, 0,
634 "Return the name under which the user logged in, as a string.\n\
635 This is based on the effective uid, not the real uid.\n\
636 Also, if the environment variable LOGNAME or USER is set,\n\
637 that determines the value of this function.\n\n\
638 If optional argument UID is an integer, return the login name of the user\n\
639 with that uid, or nil if there is no such user.")
645 /* Set up the user name info if we didn't do it before.
646 (That can happen if Emacs is dumpable
647 but you decide to run `temacs -l loadup' and not dump. */
648 if (INTEGERP (Vuser_login_name
))
652 return Vuser_login_name
;
654 CHECK_NUMBER (uid
, 0);
655 pw
= (struct passwd
*) getpwuid (XINT (uid
));
656 return (pw
? build_string (pw
->pw_name
) : Qnil
);
659 DEFUN ("user-real-login-name", Fuser_real_login_name
, Suser_real_login_name
,
661 "Return the name of the user's real uid, as a string.\n\
662 This ignores the environment variables LOGNAME and USER, so it differs from\n\
663 `user-login-name' when running under `su'.")
666 /* Set up the user name info if we didn't do it before.
667 (That can happen if Emacs is dumpable
668 but you decide to run `temacs -l loadup' and not dump. */
669 if (INTEGERP (Vuser_login_name
))
671 return Vuser_real_login_name
;
674 DEFUN ("user-uid", Fuser_uid
, Suser_uid
, 0, 0, 0,
675 "Return the effective uid of Emacs, as an integer.")
678 return make_number (geteuid ());
681 DEFUN ("user-real-uid", Fuser_real_uid
, Suser_real_uid
, 0, 0, 0,
682 "Return the real uid of Emacs, as an integer.")
685 return make_number (getuid ());
688 DEFUN ("user-full-name", Fuser_full_name
, Suser_full_name
, 0, 1, 0,
689 "Return the full name of the user logged in, as a string.\n\
690 If optional argument UID is an integer, return the full name of the user\n\
691 with that uid, or \"unknown\" if there is no such user.\n\
692 If UID is a string, return the full name of the user with that login\n\
693 name, or \"unknown\" if no such user could be found.")
698 register unsigned char *p
, *q
;
699 extern char *index ();
703 return Vuser_full_name
;
704 else if (NUMBERP (uid
))
705 pw
= (struct passwd
*) getpwuid (XINT (uid
));
706 else if (STRINGP (uid
))
707 pw
= (struct passwd
*) getpwnam (XSTRING (uid
)->data
);
709 error ("Invalid UID specification");
714 p
= (unsigned char *) USER_FULL_NAME
;
715 /* Chop off everything after the first comma. */
716 q
= (unsigned char *) index (p
, ',');
717 full
= make_string (p
, q
? q
- p
: strlen (p
));
719 #ifdef AMPERSAND_FULL_NAME
720 p
= XSTRING (full
)->data
;
721 q
= (unsigned char *) index (p
, '&');
722 /* Substitute the login name for the &, upcasing the first character. */
725 register unsigned char *r
;
728 login
= Fuser_login_name (make_number (pw
->pw_uid
));
729 r
= (unsigned char *) alloca (strlen (p
) + XSTRING (login
)->size
+ 1);
732 strcat (r
, XSTRING (login
)->data
);
733 r
[q
- p
] = UPCASE (r
[q
- p
]);
735 full
= build_string (r
);
737 #endif /* AMPERSAND_FULL_NAME */
742 DEFUN ("system-name", Fsystem_name
, Ssystem_name
, 0, 0, 0,
743 "Return the name of the machine you are running on, as a string.")
749 /* For the benefit of callers who don't want to include lisp.h */
753 if (STRINGP (Vsystem_name
))
754 return (char *) XSTRING (Vsystem_name
)->data
;
759 DEFUN ("emacs-pid", Femacs_pid
, Semacs_pid
, 0, 0, 0,
760 "Return the process ID of Emacs, as an integer.")
763 return make_number (getpid ());
766 DEFUN ("current-time", Fcurrent_time
, Scurrent_time
, 0, 0, 0,
767 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
768 The time is returned as a list of three integers. The first has the\n\
769 most significant 16 bits of the seconds, while the second has the\n\
770 least significant 16 bits. The third integer gives the microsecond\n\
773 The microsecond count is zero on systems that do not provide\n\
774 resolution finer than a second.")
778 Lisp_Object result
[3];
781 XSETINT (result
[0], (EMACS_SECS (t
) >> 16) & 0xffff);
782 XSETINT (result
[1], (EMACS_SECS (t
) >> 0) & 0xffff);
783 XSETINT (result
[2], EMACS_USECS (t
));
785 return Flist (3, result
);
790 lisp_time_argument (specified_time
, result
)
791 Lisp_Object specified_time
;
794 if (NILP (specified_time
))
795 return time (result
) != -1;
798 Lisp_Object high
, low
;
799 high
= Fcar (specified_time
);
800 CHECK_NUMBER (high
, 0);
801 low
= Fcdr (specified_time
);
804 CHECK_NUMBER (low
, 0);
805 *result
= (XINT (high
) << 16) + (XINT (low
) & 0xffff);
806 return *result
>> 16 == XINT (high
);
811 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
812 "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
813 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
814 `current-time' or `file-attributes'.\n\
815 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
816 as Universal Time; nil means describe TIME in the local time zone.\n\
817 The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
818 by text that describes the specified date and time in TIME:\n\
820 %Y is the year, %y within the century, %C the century.\n\
821 %G is the year corresponding to the ISO week, %g within the century.\n\
822 %m is the numeric month, %b and %h the abbreviated name, %B the full name.\n\
823 %d is the day of the month, zero-padded, %e is blank-padded.\n\
824 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\
825 %a is the abbreviated name of the day of week, %A the full name.\n\
826 %U is the week number starting on Sunday, %W starting on Monday,\n\
827 %V according to ISO 8601.\n\
828 %j is the day of the year.\n\
830 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\
831 only blank-padded, %l is like %I blank-padded.\n\
835 %Z is the time zone name, %z is the numeric form.\n\
836 %s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\
838 %c is the locale's date and time format.\n\
839 %x is the locale's \"preferred\" date format.\n\
840 %D is like \"%m/%d/%y\".\n\
842 %R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\
843 %X is the locale's \"preferred\" time format.\n\
845 Finally, %n is like \n, %t is like \t, %% is a literal %.\n\
847 Certain flags and modifiers are available with some format controls.\n\
848 The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
849 but padded with blanks; %-X is like %X, but without padding.\n\
850 %NX (where N stands for an integer) is like %X,\n\
851 but takes up at least N (a number) positions.\n\
852 The modifiers are `E' and `O'. For certain characters X,\n\
853 %EX is a locale's alternative version of %X;\n\
854 %OX is like %X, but uses the locale's number symbols.\n\
856 For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
857 (format_string, time, universal)
860 DEFUN ("format-time-string", Fformat_time_string
, Sformat_time_string
, 1, 3, 0,
861 0 /* See immediately above */)
862 (format_string
, time
, universal
)
863 Lisp_Object format_string
, time
, universal
;
868 CHECK_STRING (format_string
, 1);
870 if (! lisp_time_argument (time
, &value
))
871 error ("Invalid time specification");
873 /* This is probably enough. */
874 size
= XSTRING (format_string
)->size
* 6 + 50;
878 char *buf
= (char *) alloca (size
+ 1);
882 result
= emacs_strftime (buf
, size
, XSTRING (format_string
)->data
,
883 (NILP (universal
) ? localtime (&value
)
885 if ((result
> 0 && result
< size
) || (result
== 0 && buf
[0] == '\0'))
886 return build_string (buf
);
888 /* If buffer was too small, make it bigger and try again. */
889 result
= emacs_strftime (NULL
, 0x7fffffff, XSTRING (format_string
)->data
,
890 (NILP (universal
) ? localtime (&value
)
896 DEFUN ("decode-time", Fdecode_time
, Sdecode_time
, 0, 1, 0,
897 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
898 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
899 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
900 to use the current time. The list has the following nine members:\n\
901 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
902 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
903 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
904 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
905 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
906 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
907 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
908 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
910 Lisp_Object specified_time
;
914 struct tm
*decoded_time
;
915 Lisp_Object list_args
[9];
917 if (! lisp_time_argument (specified_time
, &time_spec
))
918 error ("Invalid time specification");
920 decoded_time
= localtime (&time_spec
);
921 XSETFASTINT (list_args
[0], decoded_time
->tm_sec
);
922 XSETFASTINT (list_args
[1], decoded_time
->tm_min
);
923 XSETFASTINT (list_args
[2], decoded_time
->tm_hour
);
924 XSETFASTINT (list_args
[3], decoded_time
->tm_mday
);
925 XSETFASTINT (list_args
[4], decoded_time
->tm_mon
+ 1);
926 XSETINT (list_args
[5], decoded_time
->tm_year
+ 1900);
927 XSETFASTINT (list_args
[6], decoded_time
->tm_wday
);
928 list_args
[7] = (decoded_time
->tm_isdst
)? Qt
: Qnil
;
930 /* Make a copy, in case gmtime modifies the struct. */
931 save_tm
= *decoded_time
;
932 decoded_time
= gmtime (&time_spec
);
933 if (decoded_time
== 0)
936 XSETINT (list_args
[8], tm_diff (&save_tm
, decoded_time
));
937 return Flist (9, list_args
);
940 DEFUN ("encode-time", Fencode_time
, Sencode_time
, 6, MANY
, 0,
941 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
942 This is the reverse operation of `decode-time', which see.\n\
943 ZONE defaults to the current time zone rule. This can\n\
944 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
945 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
946 applied without consideration for daylight savings time.\n\
948 You can pass more than 7 arguments; then the first six arguments\n\
949 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
950 The intervening arguments are ignored.\n\
951 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
953 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
954 for example, a DAY of 0 means the day preceding the given month.\n\
955 Year numbers less than 100 are treated just like other year numbers.\n\
956 If you want them to stand for years in this century, you must do that yourself.")
959 register Lisp_Object
*args
;
963 Lisp_Object zone
= (nargs
> 6 ? args
[nargs
- 1] : Qnil
);
965 CHECK_NUMBER (args
[0], 0); /* second */
966 CHECK_NUMBER (args
[1], 1); /* minute */
967 CHECK_NUMBER (args
[2], 2); /* hour */
968 CHECK_NUMBER (args
[3], 3); /* day */
969 CHECK_NUMBER (args
[4], 4); /* month */
970 CHECK_NUMBER (args
[5], 5); /* year */
972 tm
.tm_sec
= XINT (args
[0]);
973 tm
.tm_min
= XINT (args
[1]);
974 tm
.tm_hour
= XINT (args
[2]);
975 tm
.tm_mday
= XINT (args
[3]);
976 tm
.tm_mon
= XINT (args
[4]) - 1;
977 tm
.tm_year
= XINT (args
[5]) - 1900;
988 char **oldenv
= environ
, **newenv
;
992 else if (STRINGP (zone
))
993 tzstring
= (char *) XSTRING (zone
)->data
;
994 else if (INTEGERP (zone
))
996 int abszone
= abs (XINT (zone
));
997 sprintf (tzbuf
, "XXX%s%d:%02d:%02d", "-" + (XINT (zone
) < 0),
998 abszone
/ (60*60), (abszone
/60) % 60, abszone
% 60);
1002 error ("Invalid time zone specification");
1004 /* Set TZ before calling mktime; merely adjusting mktime's returned
1005 value doesn't suffice, since that would mishandle leap seconds. */
1006 set_time_zone_rule (tzstring
);
1008 time
= mktime (&tm
);
1010 /* Restore TZ to previous value. */
1014 #ifdef LOCALTIME_CACHE
1019 if (time
== (time_t) -1)
1020 error ("Specified time is not representable");
1022 return make_time (time
);
1025 DEFUN ("current-time-string", Fcurrent_time_string
, Scurrent_time_string
, 0, 1, 0,
1026 "Return the current time, as a human-readable string.\n\
1027 Programs can use this function to decode a time,\n\
1028 since the number of columns in each field is fixed.\n\
1029 The format is `Sun Sep 16 01:03:52 1973'.\n\
1030 However, see also the functions `decode-time' and `format-time-string'\n\
1031 which provide a much more powerful and general facility.\n\
1033 If an argument is given, it specifies a time to format\n\
1034 instead of the current time. The argument should have the form:\n\
1037 (HIGH LOW . IGNORED).\n\
1038 Thus, you can use times obtained from `current-time'\n\
1039 and from `file-attributes'.")
1041 Lisp_Object specified_time
;
1047 if (! lisp_time_argument (specified_time
, &value
))
1049 tem
= (char *) ctime (&value
);
1051 strncpy (buf
, tem
, 24);
1054 return build_string (buf
);
1057 #define TM_YEAR_BASE 1900
1059 /* Yield A - B, measured in seconds.
1060 This function is copied from the GNU C Library. */
1065 /* Compute intervening leap days correctly even if year is negative.
1066 Take care to avoid int overflow in leap day calculations,
1067 but it's OK to assume that A and B are close to each other. */
1068 int a4
= (a
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (a
->tm_year
& 3);
1069 int b4
= (b
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (b
->tm_year
& 3);
1070 int a100
= a4
/ 25 - (a4
% 25 < 0);
1071 int b100
= b4
/ 25 - (b4
% 25 < 0);
1072 int a400
= a100
>> 2;
1073 int b400
= b100
>> 2;
1074 int intervening_leap_days
= (a4
- b4
) - (a100
- b100
) + (a400
- b400
);
1075 int years
= a
->tm_year
- b
->tm_year
;
1076 int days
= (365 * years
+ intervening_leap_days
1077 + (a
->tm_yday
- b
->tm_yday
));
1078 return (60 * (60 * (24 * days
+ (a
->tm_hour
- b
->tm_hour
))
1079 + (a
->tm_min
- b
->tm_min
))
1080 + (a
->tm_sec
- b
->tm_sec
));
1083 DEFUN ("current-time-zone", Fcurrent_time_zone
, Scurrent_time_zone
, 0, 1, 0,
1084 "Return the offset and name for the local time zone.\n\
1085 This returns a list of the form (OFFSET NAME).\n\
1086 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1087 A negative value means west of Greenwich.\n\
1088 NAME is a string giving the name of the time zone.\n\
1089 If an argument is given, it specifies when the time zone offset is determined\n\
1090 instead of using the current time. The argument should have the form:\n\
1093 (HIGH LOW . IGNORED).\n\
1094 Thus, you can use times obtained from `current-time'\n\
1095 and from `file-attributes'.\n\
1097 Some operating systems cannot provide all this information to Emacs;\n\
1098 in this case, `current-time-zone' returns a list containing nil for\n\
1099 the data it can't find.")
1101 Lisp_Object specified_time
;
1106 if (lisp_time_argument (specified_time
, &value
)
1107 && (t
= gmtime (&value
)) != 0)
1113 gmt
= *t
; /* Make a copy, in case localtime modifies *t. */
1114 t
= localtime (&value
);
1115 offset
= tm_diff (t
, &gmt
);
1119 s
= (char *)t
->tm_zone
;
1120 #else /* not HAVE_TM_ZONE */
1122 if (t
->tm_isdst
== 0 || t
->tm_isdst
== 1)
1123 s
= tzname
[t
->tm_isdst
];
1125 #endif /* not HAVE_TM_ZONE */
1128 /* No local time zone name is available; use "+-NNNN" instead. */
1129 int am
= (offset
< 0 ? -offset
: offset
) / 60;
1130 sprintf (buf
, "%c%02d%02d", (offset
< 0 ? '-' : '+'), am
/60, am
%60);
1133 return Fcons (make_number (offset
), Fcons (build_string (s
), Qnil
));
1136 return Fmake_list (make_number (2), Qnil
);
1139 /* This holds the value of `environ' produced by the previous
1140 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1141 has never been called. */
1142 static char **environbuf
;
1144 DEFUN ("set-time-zone-rule", Fset_time_zone_rule
, Sset_time_zone_rule
, 1, 1, 0,
1145 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
1146 If TZ is nil, use implementation-defined default time zone information.\n\
1147 If TZ is t, use Universal Time.")
1155 else if (EQ (tz
, Qt
))
1159 CHECK_STRING (tz
, 0);
1160 tzstring
= (char *) XSTRING (tz
)->data
;
1163 set_time_zone_rule (tzstring
);
1166 environbuf
= environ
;
1171 #ifdef LOCALTIME_CACHE
1173 /* These two values are known to load tz files in buggy implementations,
1174 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1175 Their values shouldn't matter in non-buggy implementations.
1176 We don't use string literals for these strings,
1177 since if a string in the environment is in readonly
1178 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1179 See Sun bugs 1113095 and 1114114, ``Timezone routines
1180 improperly modify environment''. */
1182 static char set_time_zone_rule_tz1
[] = "TZ=GMT+0";
1183 static char set_time_zone_rule_tz2
[] = "TZ=GMT+1";
1187 /* Set the local time zone rule to TZSTRING.
1188 This allocates memory into `environ', which it is the caller's
1189 responsibility to free. */
1191 set_time_zone_rule (tzstring
)
1195 char **from
, **to
, **newenv
;
1197 /* Make the ENVIRON vector longer with room for TZSTRING. */
1198 for (from
= environ
; *from
; from
++)
1200 envptrs
= from
- environ
+ 2;
1201 newenv
= to
= (char **) xmalloc (envptrs
* sizeof (char *)
1202 + (tzstring
? strlen (tzstring
) + 4 : 0));
1204 /* Add TZSTRING to the end of environ, as a value for TZ. */
1207 char *t
= (char *) (to
+ envptrs
);
1209 strcat (t
, tzstring
);
1213 /* Copy the old environ vector elements into NEWENV,
1214 but don't copy the TZ variable.
1215 So we have only one definition of TZ, which came from TZSTRING. */
1216 for (from
= environ
; *from
; from
++)
1217 if (strncmp (*from
, "TZ=", 3) != 0)
1223 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1224 the TZ variable is stored. If we do not have a TZSTRING,
1225 TO points to the vector slot which has the terminating null. */
1227 #ifdef LOCALTIME_CACHE
1229 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1230 "US/Pacific" that loads a tz file, then changes to a value like
1231 "XXX0" that does not load a tz file, and then changes back to
1232 its original value, the last change is (incorrectly) ignored.
1233 Also, if TZ changes twice in succession to values that do
1234 not load a tz file, tzset can dump core (see Sun bug#1225179).
1235 The following code works around these bugs. */
1239 /* Temporarily set TZ to a value that loads a tz file
1240 and that differs from tzstring. */
1242 *newenv
= (strcmp (tzstring
, set_time_zone_rule_tz1
+ 3) == 0
1243 ? set_time_zone_rule_tz2
: set_time_zone_rule_tz1
);
1249 /* The implied tzstring is unknown, so temporarily set TZ to
1250 two different values that each load a tz file. */
1251 *to
= set_time_zone_rule_tz1
;
1254 *to
= set_time_zone_rule_tz2
;
1259 /* Now TZ has the desired value, and tzset can be invoked safely. */
1266 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1267 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1268 type of object is Lisp_String). INHERIT is passed to
1269 INSERT_FROM_STRING_FUNC as the last argument. */
1271 general_insert_function (insert_func
, insert_from_string_func
,
1272 inherit
, nargs
, args
)
1273 int (*insert_func
)(), (*insert_from_string_func
)();
1275 register Lisp_Object
*args
;
1277 register int argnum
;
1278 register Lisp_Object val
;
1280 for (argnum
= 0; argnum
< nargs
; argnum
++)
1286 char workbuf
[4], *str
;
1289 if (!NILP (current_buffer
->enable_multibyte_characters
))
1290 len
= CHAR_STRING (XFASTINT (val
), workbuf
, str
);
1292 workbuf
[0] = XINT (val
), str
= workbuf
, len
= 1;
1293 (*insert_func
) (str
, len
);
1295 else if (STRINGP (val
))
1297 (*insert_from_string_func
) (val
, 0, XSTRING (val
)->size
, inherit
);
1301 val
= wrong_type_argument (Qchar_or_string_p
, val
);
1315 /* Callers passing one argument to Finsert need not gcpro the
1316 argument "array", since the only element of the array will
1317 not be used after calling insert or insert_from_string, so
1318 we don't care if it gets trashed. */
1320 DEFUN ("insert", Finsert
, Sinsert
, 0, MANY
, 0,
1321 "Insert the arguments, either strings or characters, at point.\n\
1322 Point and before-insertion-markers move forward so that it ends up\n\
1323 after the inserted text.\n\
1324 Any other markers at the point of insertion remain before the text.")
1327 register Lisp_Object
*args
;
1329 general_insert_function (insert
, insert_from_string
, 0, nargs
, args
);
1333 DEFUN ("insert-and-inherit", Finsert_and_inherit
, Sinsert_and_inherit
,
1335 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1336 Point and before-insertion-markers move forward so that it ends up\n\
1337 after the inserted text.\n\
1338 Any other markers at the point of insertion remain before the text.")
1341 register Lisp_Object
*args
;
1343 general_insert_function (insert_and_inherit
, insert_from_string
, 1,
1348 DEFUN ("insert-before-markers", Finsert_before_markers
, Sinsert_before_markers
, 0, MANY
, 0,
1349 "Insert strings or characters at point, relocating markers after the text.\n\
1350 Point and before-insertion-markers move forward so that it ends up\n\
1351 after the inserted text.\n\
1352 Any other markers at the point of insertion also end up after the text.")
1355 register Lisp_Object
*args
;
1357 general_insert_function (insert_before_markers
,
1358 insert_from_string_before_markers
, 0,
1363 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers
,
1364 Sinsert_and_inherit_before_markers
, 0, MANY
, 0,
1365 "Insert text at point, relocating markers and inheriting properties.\n\
1366 Point moves forward so that it ends up after the inserted text.\n\
1367 Any other markers at the point of insertion also end up after the text.")
1370 register Lisp_Object
*args
;
1372 general_insert_function (insert_before_markers_and_inherit
,
1373 insert_from_string_before_markers
, 1,
1378 DEFUN ("insert-char", Finsert_char
, Sinsert_char
, 2, 3, 0,
1379 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1380 Point and before-insertion-markers are affected as in the function `insert'.\n\
1381 Both arguments are required.\n\
1382 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1383 from adjoining text, if those properties are sticky.")
1384 (character
, count
, inherit
)
1385 Lisp_Object character
, count
, inherit
;
1387 register unsigned char *string
;
1388 register int strlen
;
1391 unsigned char workbuf
[4], *str
;
1393 CHECK_NUMBER (character
, 0);
1394 CHECK_NUMBER (count
, 1);
1396 if (!NILP (current_buffer
->enable_multibyte_characters
))
1397 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
1399 workbuf
[0] = XFASTINT (character
), str
= workbuf
, len
= 1;
1400 n
= XINT (count
) * len
;
1403 strlen
= min (n
, 256 * len
);
1404 string
= (unsigned char *) alloca (strlen
);
1405 for (i
= 0; i
< strlen
; i
++)
1406 string
[i
] = str
[i
% len
];
1410 if (!NILP (inherit
))
1411 insert_and_inherit (string
, strlen
);
1413 insert (string
, strlen
);
1418 if (!NILP (inherit
))
1419 insert_and_inherit (string
, n
);
1427 /* Making strings from buffer contents. */
1429 /* Return a Lisp_String containing the text of the current buffer from
1430 START to END. If text properties are in use and the current buffer
1431 has properties in the range specified, the resulting string will also
1432 have them, if PROPS is nonzero.
1434 We don't want to use plain old make_string here, because it calls
1435 make_uninit_string, which can cause the buffer arena to be
1436 compacted. make_string has no way of knowing that the data has
1437 been moved, and thus copies the wrong data into the string. This
1438 doesn't effect most of the other users of make_string, so it should
1439 be left as is. But we should use this function when conjuring
1440 buffer substrings. */
1443 make_buffer_string (start
, end
, props
)
1447 Lisp_Object result
, tem
, tem1
;
1449 if (start
< GPT
&& GPT
< end
)
1452 result
= make_uninit_string (end
- start
);
1453 bcopy (POS_ADDR (start
), XSTRING (result
)->data
, end
- start
);
1455 /* If desired, update and copy the text properties. */
1456 #ifdef USE_TEXT_PROPERTIES
1459 update_buffer_properties (start
, end
);
1461 tem
= Fnext_property_change (make_number (start
), Qnil
, make_number (end
));
1462 tem1
= Ftext_properties_at (make_number (start
), Qnil
);
1464 if (XINT (tem
) != end
|| !NILP (tem1
))
1465 copy_intervals_to_string (result
, current_buffer
, start
, end
- start
);
1472 /* Call Vbuffer_access_fontify_functions for the range START ... END
1473 in the current buffer, if necessary. */
1476 update_buffer_properties (start
, end
)
1479 #ifdef USE_TEXT_PROPERTIES
1480 /* If this buffer has some access functions,
1481 call them, specifying the range of the buffer being accessed. */
1482 if (!NILP (Vbuffer_access_fontify_functions
))
1484 Lisp_Object args
[3];
1487 args
[0] = Qbuffer_access_fontify_functions
;
1488 XSETINT (args
[1], start
);
1489 XSETINT (args
[2], end
);
1491 /* But don't call them if we can tell that the work
1492 has already been done. */
1493 if (!NILP (Vbuffer_access_fontified_property
))
1495 tem
= Ftext_property_any (args
[1], args
[2],
1496 Vbuffer_access_fontified_property
,
1499 Frun_hook_with_args (3, args
);
1502 Frun_hook_with_args (3, args
);
1507 DEFUN ("buffer-substring", Fbuffer_substring
, Sbuffer_substring
, 2, 2, 0,
1508 "Return the contents of part of the current buffer as a string.\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
, 1);
1523 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties
,
1524 Sbuffer_substring_no_properties
, 2, 2, 0,
1525 "Return the characters of part of the buffer, without the text properties.\n\
1526 The two arguments START and END are character positions;\n\
1527 they can be in either order.")
1529 Lisp_Object start
, end
;
1533 validate_region (&start
, &end
);
1537 return make_buffer_string (b
, e
, 0);
1540 DEFUN ("buffer-string", Fbuffer_string
, Sbuffer_string
, 0, 0, 0,
1541 "Return the contents of the current buffer as a string.\n\
1542 If narrowing is in effect, this function returns only the visible part\n\
1546 return make_buffer_string (BEGV
, ZV
, 1);
1549 DEFUN ("insert-buffer-substring", Finsert_buffer_substring
, Sinsert_buffer_substring
,
1551 "Insert before point a substring of the contents of buffer BUFFER.\n\
1552 BUFFER may be a buffer or a buffer name.\n\
1553 Arguments START and END are character numbers specifying the substring.\n\
1554 They default to the beginning and the end of BUFFER.")
1556 Lisp_Object buf
, start
, end
;
1558 register int b
, e
, temp
;
1559 register struct buffer
*bp
, *obuf
;
1562 buffer
= Fget_buffer (buf
);
1565 bp
= XBUFFER (buffer
);
1566 if (NILP (bp
->name
))
1567 error ("Selecting deleted buffer");
1573 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1580 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1585 temp
= b
, b
= e
, e
= temp
;
1587 if (!(BUF_BEGV (bp
) <= b
&& e
<= BUF_ZV (bp
)))
1588 args_out_of_range (start
, end
);
1590 obuf
= current_buffer
;
1591 set_buffer_internal_1 (bp
);
1592 update_buffer_properties (b
, e
);
1593 set_buffer_internal_1 (obuf
);
1595 insert_from_buffer (bp
, b
, e
- b
, 0);
1599 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings
, Scompare_buffer_substrings
,
1601 "Compare two substrings of two buffers; return result as number.\n\
1602 the value is -N if first string is less after N-1 chars,\n\
1603 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
1604 Each substring is represented as three arguments: BUFFER, START and END.\n\
1605 That makes six args in all, three for each substring.\n\n\
1606 The value of `case-fold-search' in the current buffer\n\
1607 determines whether case is significant or ignored.")
1608 (buffer1
, start1
, end1
, buffer2
, start2
, end2
)
1609 Lisp_Object buffer1
, start1
, end1
, buffer2
, start2
, end2
;
1611 register int begp1
, endp1
, begp2
, endp2
, temp
, len1
, len2
, length
, i
;
1612 register struct buffer
*bp1
, *bp2
;
1613 register Lisp_Object
*trt
1614 = (!NILP (current_buffer
->case_fold_search
)
1615 ? XCHAR_TABLE (current_buffer
->case_canon_table
)->contents
: 0);
1617 /* Find the first buffer and its substring. */
1620 bp1
= current_buffer
;
1624 buf1
= Fget_buffer (buffer1
);
1627 bp1
= XBUFFER (buf1
);
1628 if (NILP (bp1
->name
))
1629 error ("Selecting deleted buffer");
1633 begp1
= BUF_BEGV (bp1
);
1636 CHECK_NUMBER_COERCE_MARKER (start1
, 1);
1637 begp1
= XINT (start1
);
1640 endp1
= BUF_ZV (bp1
);
1643 CHECK_NUMBER_COERCE_MARKER (end1
, 2);
1644 endp1
= XINT (end1
);
1648 temp
= begp1
, begp1
= endp1
, endp1
= temp
;
1650 if (!(BUF_BEGV (bp1
) <= begp1
1652 && endp1
<= BUF_ZV (bp1
)))
1653 args_out_of_range (start1
, end1
);
1655 /* Likewise for second substring. */
1658 bp2
= current_buffer
;
1662 buf2
= Fget_buffer (buffer2
);
1665 bp2
= XBUFFER (buf2
);
1666 if (NILP (bp2
->name
))
1667 error ("Selecting deleted buffer");
1671 begp2
= BUF_BEGV (bp2
);
1674 CHECK_NUMBER_COERCE_MARKER (start2
, 4);
1675 begp2
= XINT (start2
);
1678 endp2
= BUF_ZV (bp2
);
1681 CHECK_NUMBER_COERCE_MARKER (end2
, 5);
1682 endp2
= XINT (end2
);
1686 temp
= begp2
, begp2
= endp2
, endp2
= temp
;
1688 if (!(BUF_BEGV (bp2
) <= begp2
1690 && endp2
<= BUF_ZV (bp2
)))
1691 args_out_of_range (start2
, end2
);
1693 len1
= endp1
- begp1
;
1694 len2
= endp2
- begp2
;
1699 for (i
= 0; i
< length
; i
++)
1701 int c1
= *BUF_CHAR_ADDRESS (bp1
, begp1
+ i
);
1702 int c2
= *BUF_CHAR_ADDRESS (bp2
, begp2
+ i
);
1705 c1
= XINT (trt
[c1
]);
1706 c2
= XINT (trt
[c2
]);
1709 return make_number (- 1 - i
);
1711 return make_number (i
+ 1);
1714 /* The strings match as far as they go.
1715 If one is shorter, that one is less. */
1717 return make_number (length
+ 1);
1718 else if (length
< len2
)
1719 return make_number (- length
- 1);
1721 /* Same length too => they are equal. */
1722 return make_number (0);
1726 subst_char_in_region_unwind (arg
)
1729 return current_buffer
->undo_list
= arg
;
1733 subst_char_in_region_unwind_1 (arg
)
1736 return current_buffer
->filename
= arg
;
1739 DEFUN ("subst-char-in-region", Fsubst_char_in_region
,
1740 Ssubst_char_in_region
, 4, 5, 0,
1741 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1742 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1743 and don't mark the buffer as really changed.\n\
1744 Both characters must have the same length of multi-byte form.")
1745 (start
, end
, fromchar
, tochar
, noundo
)
1746 Lisp_Object start
, end
, fromchar
, tochar
, noundo
;
1748 register int pos
, stop
, i
, len
;
1750 unsigned char fromwork
[4], *fromstr
, towork
[4], *tostr
, *p
;
1751 int count
= specpdl_ptr
- specpdl
;
1753 validate_region (&start
, &end
);
1754 CHECK_NUMBER (fromchar
, 2);
1755 CHECK_NUMBER (tochar
, 3);
1757 if (! NILP (current_buffer
->enable_multibyte_characters
))
1759 len
= CHAR_STRING (XFASTINT (fromchar
), fromwork
, fromstr
);
1760 if (CHAR_STRING (XFASTINT (tochar
), towork
, tostr
) != len
)
1761 error ("Characters in subst-char-in-region have different byte-lengths");
1766 fromwork
[0] = XFASTINT (fromchar
), fromstr
= fromwork
;
1767 towork
[0] = XFASTINT (tochar
), tostr
= towork
;
1773 /* If we don't want undo, turn off putting stuff on the list.
1774 That's faster than getting rid of things,
1775 and it prevents even the entry for a first change.
1776 Also inhibit locking the file. */
1779 record_unwind_protect (subst_char_in_region_unwind
,
1780 current_buffer
->undo_list
);
1781 current_buffer
->undo_list
= Qt
;
1782 /* Don't do file-locking. */
1783 record_unwind_protect (subst_char_in_region_unwind_1
,
1784 current_buffer
->filename
);
1785 current_buffer
->filename
= Qnil
;
1789 stop
= min(stop
, GPT
);
1795 if (pos
>= XINT (end
)) break;
1799 if (p
[0] == fromstr
[0]
1801 || (p
[1] == fromstr
[1]
1802 && (len
== 2 || (p
[2] == fromstr
[2]
1803 && (len
== 3 || p
[3] == fromstr
[3]))))))
1807 modify_region (current_buffer
, XINT (start
), XINT (end
));
1809 if (! NILP (noundo
))
1811 if (MODIFF
- 1 == SAVE_MODIFF
)
1813 if (MODIFF
- 1 == current_buffer
->auto_save_modified
)
1814 current_buffer
->auto_save_modified
++;
1821 record_change (pos
, len
);
1822 for (i
= 0; i
< len
; i
++) *p
++ = tostr
[i
];
1830 signal_after_change (XINT (start
),
1831 stop
- XINT (start
), stop
- XINT (start
));
1833 unbind_to (count
, Qnil
);
1837 DEFUN ("translate-region", Ftranslate_region
, Stranslate_region
, 3, 3, 0,
1838 "From START to END, translate characters according to TABLE.\n\
1839 TABLE is a string; the Nth character in it is the mapping\n\
1840 for the character with code N. Returns the number of characters changed.")
1844 register Lisp_Object table
;
1846 register int pos
, stop
; /* Limits of the region. */
1847 register unsigned char *tt
; /* Trans table. */
1848 register int oc
; /* Old character. */
1849 register int nc
; /* New character. */
1850 int cnt
; /* Number of changes made. */
1851 Lisp_Object z
; /* Return. */
1852 int size
; /* Size of translate table. */
1854 validate_region (&start
, &end
);
1855 CHECK_STRING (table
, 2);
1857 size
= XSTRING (table
)->size
;
1858 tt
= XSTRING (table
)->data
;
1862 modify_region (current_buffer
, pos
, stop
);
1865 for (; pos
< stop
; ++pos
)
1867 oc
= FETCH_BYTE (pos
);
1873 record_change (pos
, 1);
1874 *(POS_ADDR (pos
)) = nc
;
1875 signal_after_change (pos
, 1, 1);
1881 XSETFASTINT (z
, cnt
);
1885 DEFUN ("delete-region", Fdelete_region
, Sdelete_region
, 2, 2, "r",
1886 "Delete the text between point and mark.\n\
1887 When called from a program, expects two arguments,\n\
1888 positions (integers or markers) specifying the stretch to be deleted.")
1890 Lisp_Object start
, end
;
1892 validate_region (&start
, &end
);
1893 del_range (XINT (start
), XINT (end
));
1897 DEFUN ("widen", Fwiden
, Swiden
, 0, 0, "",
1898 "Remove restrictions (narrowing) from current buffer.\n\
1899 This allows the buffer's full text to be seen and edited.")
1902 if (BEG
!= BEGV
|| Z
!= ZV
)
1903 current_buffer
->clip_changed
= 1;
1905 SET_BUF_ZV (current_buffer
, Z
);
1906 /* Changing the buffer bounds invalidates any recorded current column. */
1907 invalidate_current_column ();
1911 DEFUN ("narrow-to-region", Fnarrow_to_region
, Snarrow_to_region
, 2, 2, "r",
1912 "Restrict editing in this buffer to the current region.\n\
1913 The rest of the text becomes temporarily invisible and untouchable\n\
1914 but is not deleted; if you save the buffer in a file, the invisible\n\
1915 text is included in the file. \\[widen] makes all visible again.\n\
1916 See also `save-restriction'.\n\
1918 When calling from a program, pass two arguments; positions (integers\n\
1919 or markers) bounding the text that should remain visible.")
1921 register Lisp_Object start
, end
;
1923 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1924 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1926 if (XINT (start
) > XINT (end
))
1929 tem
= start
; start
= end
; end
= tem
;
1932 if (!(BEG
<= XINT (start
) && XINT (start
) <= XINT (end
) && XINT (end
) <= Z
))
1933 args_out_of_range (start
, end
);
1935 if (BEGV
!= XFASTINT (start
) || ZV
!= XFASTINT (end
))
1936 current_buffer
->clip_changed
= 1;
1938 BEGV
= XFASTINT (start
);
1939 SET_BUF_ZV (current_buffer
, XFASTINT (end
));
1940 if (PT
< XFASTINT (start
))
1941 SET_PT (XFASTINT (start
));
1942 if (PT
> XFASTINT (end
))
1943 SET_PT (XFASTINT (end
));
1944 /* Changing the buffer bounds invalidates any recorded current column. */
1945 invalidate_current_column ();
1950 save_restriction_save ()
1952 register Lisp_Object bottom
, top
;
1953 /* Note: I tried using markers here, but it does not win
1954 because insertion at the end of the saved region
1955 does not advance mh and is considered "outside" the saved region. */
1956 XSETFASTINT (bottom
, BEGV
- BEG
);
1957 XSETFASTINT (top
, Z
- ZV
);
1959 return Fcons (Fcurrent_buffer (), Fcons (bottom
, top
));
1963 save_restriction_restore (data
)
1966 register struct buffer
*buf
;
1967 register int newhead
, newtail
;
1968 register Lisp_Object tem
;
1971 buf
= XBUFFER (XCONS (data
)->car
);
1973 data
= XCONS (data
)->cdr
;
1975 tem
= XCONS (data
)->car
;
1976 newhead
= XINT (tem
);
1977 tem
= XCONS (data
)->cdr
;
1978 newtail
= XINT (tem
);
1979 if (newhead
+ newtail
> BUF_Z (buf
) - BUF_BEG (buf
))
1985 obegv
= BUF_BEGV (buf
);
1988 BUF_BEGV (buf
) = BUF_BEG (buf
) + newhead
;
1989 SET_BUF_ZV (buf
, BUF_Z (buf
) - newtail
);
1991 if (obegv
!= BUF_BEGV (buf
) || ozv
!= BUF_ZV (buf
))
1992 current_buffer
->clip_changed
= 1;
1994 /* If point is outside the new visible range, move it inside. */
1996 clip_to_bounds (BUF_BEGV (buf
), BUF_PT (buf
), BUF_ZV (buf
)));
2001 DEFUN ("save-restriction", Fsave_restriction
, Ssave_restriction
, 0, UNEVALLED
, 0,
2002 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2003 The buffer's restrictions make parts of the beginning and end invisible.\n\
2004 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2005 This special form, `save-restriction', saves the current buffer's restrictions\n\
2006 when it is entered, and restores them when it is exited.\n\
2007 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2008 The old restrictions settings are restored\n\
2009 even in case of abnormal exit (throw or error).\n\
2011 The value returned is the value of the last form in BODY.\n\
2013 `save-restriction' can get confused if, within the BODY, you widen\n\
2014 and then make changes outside the area within the saved restrictions.\n\
2016 Note: if you are using both `save-excursion' and `save-restriction',\n\
2017 use `save-excursion' outermost:\n\
2018 (save-excursion (save-restriction ...))")
2022 register Lisp_Object val
;
2023 int count
= specpdl_ptr
- specpdl
;
2025 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
2026 val
= Fprogn (body
);
2027 return unbind_to (count
, val
);
2030 /* Buffer for the most recent text displayed by Fmessage. */
2031 static char *message_text
;
2033 /* Allocated length of that buffer. */
2034 static int message_length
;
2036 DEFUN ("message", Fmessage
, Smessage
, 1, MANY
, 0,
2037 "Print a one-line message at the bottom of the screen.\n\
2038 The first argument is a format control string, and the rest are data\n\
2039 to be formatted under control of the string. See `format' for details.\n\
2041 If the first argument is nil, clear any existing message; let the\n\
2042 minibuffer contents show.")
2054 register Lisp_Object val
;
2055 val
= Fformat (nargs
, args
);
2056 /* Copy the data so that it won't move when we GC. */
2059 message_text
= (char *)xmalloc (80);
2060 message_length
= 80;
2062 if (XSTRING (val
)->size
> message_length
)
2064 message_length
= XSTRING (val
)->size
;
2065 message_text
= (char *)xrealloc (message_text
, message_length
);
2067 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
2068 message2 (message_text
, XSTRING (val
)->size
);
2073 DEFUN ("message-box", Fmessage_box
, Smessage_box
, 1, MANY
, 0,
2074 "Display a message, in a dialog box if possible.\n\
2075 If a dialog box is not available, use the echo area.\n\
2076 The first argument is a format control string, and the rest are data\n\
2077 to be formatted under control of the string. See `format' for details.\n\
2079 If the first argument is nil, clear any existing message; let the\n\
2080 minibuffer contents show.")
2092 register Lisp_Object val
;
2093 val
= Fformat (nargs
, args
);
2096 Lisp_Object pane
, menu
, obj
;
2097 struct gcpro gcpro1
;
2098 pane
= Fcons (Fcons (build_string ("OK"), Qt
), Qnil
);
2100 menu
= Fcons (val
, pane
);
2101 obj
= Fx_popup_dialog (Qt
, menu
);
2105 #else /* not HAVE_MENUS */
2106 /* Copy the data so that it won't move when we GC. */
2109 message_text
= (char *)xmalloc (80);
2110 message_length
= 80;
2112 if (XSTRING (val
)->size
> message_length
)
2114 message_length
= XSTRING (val
)->size
;
2115 message_text
= (char *)xrealloc (message_text
, message_length
);
2117 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
2118 message2 (message_text
, XSTRING (val
)->size
);
2120 #endif /* not HAVE_MENUS */
2124 extern Lisp_Object last_nonmenu_event
;
2127 DEFUN ("message-or-box", Fmessage_or_box
, Smessage_or_box
, 1, MANY
, 0,
2128 "Display a message in a dialog box or in the echo area.\n\
2129 If this command was invoked with the mouse, use a dialog box.\n\
2130 Otherwise, use the echo area.\n\
2131 The first argument is a format control string, and the rest are data\n\
2132 to be formatted under control of the string. See `format' for details.\n\
2134 If the first argument is nil, clear any existing message; let the\n\
2135 minibuffer contents show.")
2141 if (NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2142 return Fmessage_box (nargs
, args
);
2144 return Fmessage (nargs
, args
);
2147 DEFUN ("current-message", Fcurrent_message
, Scurrent_message
, 0, 0, 0,
2148 "Return the string currently displayed in the echo area, or nil if none.")
2151 return (echo_area_glyphs
2152 ? make_string (echo_area_glyphs
, echo_area_glyphs_length
)
2156 DEFUN ("format", Fformat
, Sformat
, 1, MANY
, 0,
2157 "Format a string out of a control-string and arguments.\n\
2158 The first argument is a control string.\n\
2159 The other arguments are substituted into it to make the result, a string.\n\
2160 It may contain %-sequences meaning to substitute the next argument.\n\
2161 %s means print a string argument. Actually, prints any object, with `princ'.\n\
2162 %d means print as number in decimal (%o octal, %x hex).\n\
2163 %e means print a number in exponential notation.\n\
2164 %f means print a number in decimal-point notation.\n\
2165 %g means print a number in exponential notation\n\
2166 or decimal-point notation, whichever uses fewer characters.\n\
2167 %c means print a number as a single character.\n\
2168 %S means print any object as an s-expression (using prin1).\n\
2169 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2170 Use %% to put a single % into the output.")
2173 register Lisp_Object
*args
;
2175 register int n
; /* The number of the next arg to substitute */
2176 register int total
= 5; /* An estimate of the final length */
2178 register unsigned char *format
, *end
;
2180 extern char *index ();
2181 /* It should not be necessary to GCPRO ARGS, because
2182 the caller in the interpreter should take care of that. */
2184 CHECK_STRING (args
[0], 0);
2185 format
= XSTRING (args
[0])->data
;
2186 end
= format
+ XSTRING (args
[0])->size
;
2189 while (format
!= end
)
2190 if (*format
++ == '%')
2194 /* Process a numeric arg and skip it. */
2195 minlen
= atoi (format
);
2199 while ((*format
>= '0' && *format
<= '9')
2200 || *format
== '-' || *format
== ' ' || *format
== '.')
2205 else if (++n
>= nargs
)
2206 error ("Not enough arguments for format string");
2207 else if (*format
== 'S')
2209 /* For `S', prin1 the argument and then treat like a string. */
2210 register Lisp_Object tem
;
2211 tem
= Fprin1_to_string (args
[n
], Qnil
);
2215 else if (SYMBOLP (args
[n
]))
2217 XSETSTRING (args
[n
], XSYMBOL (args
[n
])->name
);
2220 else if (STRINGP (args
[n
]))
2223 if (*format
!= 's' && *format
!= 'S')
2224 error ("format specifier doesn't match argument type");
2225 total
+= XSTRING (args
[n
])->size
;
2226 /* We have to put an arbitrary limit on minlen
2227 since otherwise it could make alloca fail. */
2228 if (minlen
< XSTRING (args
[n
])->size
+ 1000)
2231 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
2232 else if (INTEGERP (args
[n
]) && *format
!= 's')
2234 #ifdef LISP_FLOAT_TYPE
2235 /* The following loop assumes the Lisp type indicates
2236 the proper way to pass the argument.
2237 So make sure we have a flonum if the argument should
2239 if (*format
== 'e' || *format
== 'f' || *format
== 'g')
2240 args
[n
] = Ffloat (args
[n
]);
2243 /* We have to put an arbitrary limit on minlen
2244 since otherwise it could make alloca fail. */
2248 #ifdef LISP_FLOAT_TYPE
2249 else if (FLOATP (args
[n
]) && *format
!= 's')
2251 if (! (*format
== 'e' || *format
== 'f' || *format
== 'g'))
2252 args
[n
] = Ftruncate (args
[n
], Qnil
);
2254 /* We have to put an arbitrary limit on minlen
2255 since otherwise it could make alloca fail. */
2262 /* Anything but a string, convert to a string using princ. */
2263 register Lisp_Object tem
;
2264 tem
= Fprin1_to_string (args
[n
], Qt
);
2271 register int nstrings
= n
+ 1;
2273 /* Allocate twice as many strings as we have %-escapes; floats occupy
2274 two slots, and we're not sure how many of those we have. */
2275 register unsigned char **strings
2276 = (unsigned char **) alloca (2 * nstrings
* sizeof (unsigned char *));
2280 for (n
= 0; n
< nstrings
; n
++)
2283 strings
[i
++] = (unsigned char *) "";
2284 else if (INTEGERP (args
[n
]))
2285 /* We checked above that the corresponding format effector
2286 isn't %s, which would cause MPV. */
2287 strings
[i
++] = (unsigned char *) XINT (args
[n
]);
2288 #ifdef LISP_FLOAT_TYPE
2289 else if (FLOATP (args
[n
]))
2291 union { double d
; char *half
[2]; } u
;
2293 u
.d
= XFLOAT (args
[n
])->data
;
2294 strings
[i
++] = (unsigned char *) u
.half
[0];
2295 strings
[i
++] = (unsigned char *) u
.half
[1];
2299 /* The first string is treated differently
2300 because it is the format string. */
2301 strings
[i
++] = XSTRING (args
[n
])->data
;
2303 strings
[i
++] = (unsigned char *) XSTRING (args
[n
]);
2306 /* Make room in result for all the non-%-codes in the control string. */
2307 total
+= XSTRING (args
[0])->size
;
2309 /* Format it in bigger and bigger buf's until it all fits. */
2312 buf
= (char *) alloca (total
+ 1);
2315 length
= doprnt_lisp (buf
, total
+ 1, strings
[0],
2316 end
, i
-1, strings
+ 1);
2317 if (buf
[total
- 1] == 0)
2325 return make_string (buf
, length
);
2331 format1 (string1
, arg0
, arg1
, arg2
, arg3
, arg4
)
2332 EMACS_INT arg0
, arg1
, arg2
, arg3
, arg4
;
2346 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, args
);
2348 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, &string1
+ 1);
2350 return build_string (buf
);
2353 DEFUN ("char-equal", Fchar_equal
, Schar_equal
, 2, 2, 0,
2354 "Return t if two characters match, optionally ignoring case.\n\
2355 Both arguments must be characters (i.e. integers).\n\
2356 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2358 register Lisp_Object c1
, c2
;
2360 CHECK_NUMBER (c1
, 0);
2361 CHECK_NUMBER (c2
, 1);
2363 if (XINT (c1
) == XINT (c2
)
2364 && (NILP (current_buffer
->case_fold_search
)
2365 || DOWNCASE (XFASTINT (c1
)) == DOWNCASE (XFASTINT (c2
))))
2370 /* Transpose the markers in two regions of the current buffer, and
2371 adjust the ones between them if necessary (i.e.: if the regions
2374 Traverses the entire marker list of the buffer to do so, adding an
2375 appropriate amount to some, subtracting from some, and leaving the
2376 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2378 It's the caller's job to see that (start1 <= end1 <= start2 <= end2). */
2381 transpose_markers (start1
, end1
, start2
, end2
)
2382 register int start1
, end1
, start2
, end2
;
2384 register int amt1
, amt2
, diff
, mpos
;
2385 register Lisp_Object marker
;
2387 /* Update point as if it were a marker. */
2391 TEMP_SET_PT (PT
+ (end2
- end1
));
2392 else if (PT
< start2
)
2393 TEMP_SET_PT (PT
+ (end2
- start2
) - (end1
- start1
));
2395 TEMP_SET_PT (PT
- (start2
- start1
));
2397 /* We used to adjust the endpoints here to account for the gap, but that
2398 isn't good enough. Even if we assume the caller has tried to move the
2399 gap out of our way, it might still be at start1 exactly, for example;
2400 and that places it `inside' the interval, for our purposes. The amount
2401 of adjustment is nontrivial if there's a `denormalized' marker whose
2402 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2403 the dirty work to Fmarker_position, below. */
2405 /* The difference between the region's lengths */
2406 diff
= (end2
- start2
) - (end1
- start1
);
2408 /* For shifting each marker in a region by the length of the other
2409 * region plus the distance between the regions.
2411 amt1
= (end2
- start2
) + (start2
- end1
);
2412 amt2
= (end1
- start1
) + (start2
- end1
);
2414 for (marker
= BUF_MARKERS (current_buffer
); !NILP (marker
);
2415 marker
= XMARKER (marker
)->chain
)
2417 mpos
= marker_position (marker
);
2418 if (mpos
>= start1
&& mpos
< end2
)
2422 else if (mpos
< start2
)
2426 if (mpos
> GPT
) mpos
+= GAP_SIZE
;
2427 XMARKER (marker
)->bufpos
= mpos
;
2432 DEFUN ("transpose-regions", Ftranspose_regions
, Stranspose_regions
, 4, 5, 0,
2433 "Transpose region START1 to END1 with START2 to END2.\n\
2434 The regions may not be overlapping, because the size of the buffer is\n\
2435 never changed in a transposition.\n\
2437 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't transpose\n\
2438 any markers that happen to be located in the regions.\n\
2440 Transposing beyond buffer boundaries is an error.")
2441 (startr1
, endr1
, startr2
, endr2
, leave_markers
)
2442 Lisp_Object startr1
, endr1
, startr2
, endr2
, leave_markers
;
2444 register int start1
, end1
, start2
, end2
,
2445 gap
, len1
, len_mid
, len2
;
2446 unsigned char *start1_addr
, *start2_addr
, *temp
;
2448 #ifdef USE_TEXT_PROPERTIES
2449 INTERVAL cur_intv
, tmp_interval1
, tmp_interval_mid
, tmp_interval2
;
2450 cur_intv
= BUF_INTERVALS (current_buffer
);
2451 #endif /* USE_TEXT_PROPERTIES */
2453 validate_region (&startr1
, &endr1
);
2454 validate_region (&startr2
, &endr2
);
2456 start1
= XFASTINT (startr1
);
2457 end1
= XFASTINT (endr1
);
2458 start2
= XFASTINT (startr2
);
2459 end2
= XFASTINT (endr2
);
2462 /* Swap the regions if they're reversed. */
2465 register int glumph
= start1
;
2473 len1
= end1
- start1
;
2474 len2
= end2
- start2
;
2477 error ("transposed regions not properly ordered");
2478 else if (start1
== end1
|| start2
== end2
)
2479 error ("transposed region may not be of length 0");
2481 /* The possibilities are:
2482 1. Adjacent (contiguous) regions, or separate but equal regions
2483 (no, really equal, in this case!), or
2484 2. Separate regions of unequal size.
2486 The worst case is usually No. 2. It means that (aside from
2487 potential need for getting the gap out of the way), there also
2488 needs to be a shifting of the text between the two regions. So
2489 if they are spread far apart, we are that much slower... sigh. */
2491 /* It must be pointed out that the really studly thing to do would
2492 be not to move the gap at all, but to leave it in place and work
2493 around it if necessary. This would be extremely efficient,
2494 especially considering that people are likely to do
2495 transpositions near where they are working interactively, which
2496 is exactly where the gap would be found. However, such code
2497 would be much harder to write and to read. So, if you are
2498 reading this comment and are feeling squirrely, by all means have
2499 a go! I just didn't feel like doing it, so I will simply move
2500 the gap the minimum distance to get it out of the way, and then
2501 deal with an unbroken array. */
2503 /* Make sure the gap won't interfere, by moving it out of the text
2504 we will operate on. */
2505 if (start1
< gap
&& gap
< end2
)
2507 if (gap
- start1
< end2
- gap
)
2513 /* Hmmm... how about checking to see if the gap is large
2514 enough to use as the temporary storage? That would avoid an
2515 allocation... interesting. Later, don't fool with it now. */
2517 /* Working without memmove, for portability (sigh), so must be
2518 careful of overlapping subsections of the array... */
2520 if (end1
== start2
) /* adjacent regions */
2522 modify_region (current_buffer
, start1
, end2
);
2523 record_change (start1
, len1
+ len2
);
2525 #ifdef USE_TEXT_PROPERTIES
2526 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2527 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2528 Fset_text_properties (make_number (start1
), make_number (end2
),
2530 #endif /* USE_TEXT_PROPERTIES */
2532 /* First region smaller than second. */
2535 /* We use alloca only if it is small,
2536 because we want to avoid stack overflow. */
2538 temp
= (unsigned char *) xmalloc (len2
);
2540 temp
= (unsigned char *) alloca (len2
);
2542 /* Don't precompute these addresses. We have to compute them
2543 at the last minute, because the relocating allocator might
2544 have moved the buffer around during the xmalloc. */
2545 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2546 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2548 bcopy (start2_addr
, temp
, len2
);
2549 bcopy (start1_addr
, start1_addr
+ len2
, len1
);
2550 bcopy (temp
, start1_addr
, len2
);
2555 /* First region not smaller than second. */
2558 temp
= (unsigned char *) xmalloc (len1
);
2560 temp
= (unsigned char *) alloca (len1
);
2561 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2562 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2563 bcopy (start1_addr
, temp
, len1
);
2564 bcopy (start2_addr
, start1_addr
, len2
);
2565 bcopy (temp
, start1_addr
+ len2
, len1
);
2569 #ifdef USE_TEXT_PROPERTIES
2570 graft_intervals_into_buffer (tmp_interval1
, start1
+ len2
,
2571 len1
, current_buffer
, 0);
2572 graft_intervals_into_buffer (tmp_interval2
, start1
,
2573 len2
, current_buffer
, 0);
2574 #endif /* USE_TEXT_PROPERTIES */
2576 /* Non-adjacent regions, because end1 != start2, bleagh... */
2580 /* Regions are same size, though, how nice. */
2582 modify_region (current_buffer
, start1
, end1
);
2583 modify_region (current_buffer
, start2
, end2
);
2584 record_change (start1
, len1
);
2585 record_change (start2
, len2
);
2586 #ifdef USE_TEXT_PROPERTIES
2587 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2588 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2589 Fset_text_properties (make_number (start1
), make_number (end1
),
2591 Fset_text_properties (make_number (start2
), make_number (end2
),
2593 #endif /* USE_TEXT_PROPERTIES */
2596 temp
= (unsigned char *) xmalloc (len1
);
2598 temp
= (unsigned char *) alloca (len1
);
2599 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2600 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2601 bcopy (start1_addr
, temp
, len1
);
2602 bcopy (start2_addr
, start1_addr
, len2
);
2603 bcopy (temp
, start2_addr
, len1
);
2606 #ifdef USE_TEXT_PROPERTIES
2607 graft_intervals_into_buffer (tmp_interval1
, start2
,
2608 len1
, current_buffer
, 0);
2609 graft_intervals_into_buffer (tmp_interval2
, start1
,
2610 len2
, current_buffer
, 0);
2611 #endif /* USE_TEXT_PROPERTIES */
2614 else if (len1
< len2
) /* Second region larger than first */
2615 /* Non-adjacent & unequal size, area between must also be shifted. */
2617 len_mid
= start2
- end1
;
2618 modify_region (current_buffer
, start1
, end2
);
2619 record_change (start1
, (end2
- start1
));
2620 #ifdef USE_TEXT_PROPERTIES
2621 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2622 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2623 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2624 Fset_text_properties (make_number (start1
), make_number (end2
),
2626 #endif /* USE_TEXT_PROPERTIES */
2628 /* holds region 2 */
2630 temp
= (unsigned char *) xmalloc (len2
);
2632 temp
= (unsigned char *) alloca (len2
);
2633 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2634 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2635 bcopy (start2_addr
, temp
, len2
);
2636 bcopy (start1_addr
, start1_addr
+ len_mid
+ len2
, len1
);
2637 safe_bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2638 bcopy (temp
, start1_addr
, len2
);
2641 #ifdef USE_TEXT_PROPERTIES
2642 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2643 len1
, current_buffer
, 0);
2644 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2645 len_mid
, current_buffer
, 0);
2646 graft_intervals_into_buffer (tmp_interval2
, start1
,
2647 len2
, current_buffer
, 0);
2648 #endif /* USE_TEXT_PROPERTIES */
2651 /* Second region smaller than first. */
2653 len_mid
= start2
- end1
;
2654 record_change (start1
, (end2
- start1
));
2655 modify_region (current_buffer
, start1
, end2
);
2657 #ifdef USE_TEXT_PROPERTIES
2658 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2659 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2660 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2661 Fset_text_properties (make_number (start1
), make_number (end2
),
2663 #endif /* USE_TEXT_PROPERTIES */
2665 /* holds region 1 */
2667 temp
= (unsigned char *) xmalloc (len1
);
2669 temp
= (unsigned char *) alloca (len1
);
2670 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2671 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2672 bcopy (start1_addr
, temp
, len1
);
2673 bcopy (start2_addr
, start1_addr
, len2
);
2674 bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2675 bcopy (temp
, start1_addr
+ len2
+ len_mid
, len1
);
2678 #ifdef USE_TEXT_PROPERTIES
2679 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2680 len1
, current_buffer
, 0);
2681 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2682 len_mid
, current_buffer
, 0);
2683 graft_intervals_into_buffer (tmp_interval2
, start1
,
2684 len2
, current_buffer
, 0);
2685 #endif /* USE_TEXT_PROPERTIES */
2689 /* todo: this will be slow, because for every transposition, we
2690 traverse the whole friggin marker list. Possible solutions:
2691 somehow get a list of *all* the markers across multiple
2692 transpositions and do it all in one swell phoop. Or maybe modify
2693 Emacs' marker code to keep an ordered list or tree. This might
2694 be nicer, and more beneficial in the long run, but would be a
2695 bunch of work. Plus the way they're arranged now is nice. */
2696 if (NILP (leave_markers
))
2698 transpose_markers (start1
, end1
, start2
, end2
);
2699 fix_overlays_in_range (start1
, end2
);
2711 Qbuffer_access_fontify_functions
2712 = intern ("buffer-access-fontify-functions");
2713 staticpro (&Qbuffer_access_fontify_functions
);
2715 DEFVAR_LISP ("buffer-access-fontify-functions",
2716 &Vbuffer_access_fontify_functions
,
2717 "List of functions called by `buffer-substring' to fontify if necessary.\n\
2718 Each function is called with two arguments which specify the range\n\
2719 of the buffer being accessed.");
2720 Vbuffer_access_fontify_functions
= Qnil
;
2724 extern Lisp_Object Vprin1_to_string_buffer
;
2725 obuf
= Fcurrent_buffer ();
2726 /* Do this here, because init_buffer_once is too early--it won't work. */
2727 Fset_buffer (Vprin1_to_string_buffer
);
2728 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
2729 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
2734 DEFVAR_LISP ("buffer-access-fontified-property",
2735 &Vbuffer_access_fontified_property
,
2736 "Property which (if non-nil) indicates text has been fontified.\n\
2737 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
2738 functions if all the text being accessed has this property.");
2739 Vbuffer_access_fontified_property
= Qnil
;
2741 DEFVAR_LISP ("system-name", &Vsystem_name
,
2742 "The name of the machine Emacs is running on.");
2744 DEFVAR_LISP ("user-full-name", &Vuser_full_name
,
2745 "The full name of the user logged in.");
2747 DEFVAR_LISP ("user-login-name", &Vuser_login_name
,
2748 "The user's name, taken from environment variables if possible.");
2750 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name
,
2751 "The user's name, based upon the real uid only.");
2753 defsubr (&Schar_equal
);
2754 defsubr (&Sgoto_char
);
2755 defsubr (&Sstring_to_char
);
2756 defsubr (&Schar_to_string
);
2758 defsubr (&Sbuffer_substring
);
2759 defsubr (&Sbuffer_substring_no_properties
);
2760 defsubr (&Sbuffer_string
);
2762 defsubr (&Spoint_marker
);
2763 defsubr (&Smark_marker
);
2765 defsubr (&Sregion_beginning
);
2766 defsubr (&Sregion_end
);
2767 /* defsubr (&Smark); */
2768 /* defsubr (&Sset_mark); */
2769 defsubr (&Ssave_excursion
);
2770 defsubr (&Ssave_current_buffer
);
2772 defsubr (&Sbufsize
);
2773 defsubr (&Spoint_max
);
2774 defsubr (&Spoint_min
);
2775 defsubr (&Spoint_min_marker
);
2776 defsubr (&Spoint_max_marker
);
2778 defsubr (&Sline_beginning_position
);
2779 defsubr (&Sline_end_position
);
2785 defsubr (&Sfollowing_char
);
2786 defsubr (&Sprevious_char
);
2787 defsubr (&Schar_after
);
2788 defsubr (&Schar_before
);
2790 defsubr (&Sinsert_before_markers
);
2791 defsubr (&Sinsert_and_inherit
);
2792 defsubr (&Sinsert_and_inherit_before_markers
);
2793 defsubr (&Sinsert_char
);
2795 defsubr (&Suser_login_name
);
2796 defsubr (&Suser_real_login_name
);
2797 defsubr (&Suser_uid
);
2798 defsubr (&Suser_real_uid
);
2799 defsubr (&Suser_full_name
);
2800 defsubr (&Semacs_pid
);
2801 defsubr (&Scurrent_time
);
2802 defsubr (&Sformat_time_string
);
2803 defsubr (&Sdecode_time
);
2804 defsubr (&Sencode_time
);
2805 defsubr (&Scurrent_time_string
);
2806 defsubr (&Scurrent_time_zone
);
2807 defsubr (&Sset_time_zone_rule
);
2808 defsubr (&Ssystem_name
);
2809 defsubr (&Smessage
);
2810 defsubr (&Smessage_box
);
2811 defsubr (&Smessage_or_box
);
2812 defsubr (&Scurrent_message
);
2815 defsubr (&Sinsert_buffer_substring
);
2816 defsubr (&Scompare_buffer_substrings
);
2817 defsubr (&Ssubst_char_in_region
);
2818 defsubr (&Stranslate_region
);
2819 defsubr (&Sdelete_region
);
2821 defsubr (&Snarrow_to_region
);
2822 defsubr (&Ssave_restriction
);
2823 defsubr (&Stranspose_regions
);