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\".")
859 DEFUN ("format-time-string", Fformat_time_string
, Sformat_time_string
, 1, 3, 0,
860 0 /* See immediately above */)
861 (format_string
, time
, universal
)
862 Lisp_Object format_string
, time
, universal
;
867 CHECK_STRING (format_string
, 1);
869 if (! lisp_time_argument (time
, &value
))
870 error ("Invalid time specification");
872 /* This is probably enough. */
873 size
= XSTRING (format_string
)->size
* 6 + 50;
877 char *buf
= (char *) alloca (size
+ 1);
881 result
= emacs_strftime (buf
, size
, XSTRING (format_string
)->data
,
882 (NILP (universal
) ? localtime (&value
)
884 if ((result
> 0 && result
< size
) || (result
== 0 && buf
[0] == '\0'))
885 return build_string (buf
);
887 /* If buffer was too small, make it bigger and try again. */
888 result
= emacs_strftime (NULL
, 0x7fffffff, XSTRING (format_string
)->data
,
889 (NILP (universal
) ? localtime (&value
)
895 DEFUN ("decode-time", Fdecode_time
, Sdecode_time
, 0, 1, 0,
896 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
897 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
898 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
899 to use the current time. The list has the following nine members:\n\
900 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
901 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
902 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
903 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
904 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
905 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
906 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
907 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
909 Lisp_Object specified_time
;
913 struct tm
*decoded_time
;
914 Lisp_Object list_args
[9];
916 if (! lisp_time_argument (specified_time
, &time_spec
))
917 error ("Invalid time specification");
919 decoded_time
= localtime (&time_spec
);
920 XSETFASTINT (list_args
[0], decoded_time
->tm_sec
);
921 XSETFASTINT (list_args
[1], decoded_time
->tm_min
);
922 XSETFASTINT (list_args
[2], decoded_time
->tm_hour
);
923 XSETFASTINT (list_args
[3], decoded_time
->tm_mday
);
924 XSETFASTINT (list_args
[4], decoded_time
->tm_mon
+ 1);
925 XSETINT (list_args
[5], decoded_time
->tm_year
+ 1900);
926 XSETFASTINT (list_args
[6], decoded_time
->tm_wday
);
927 list_args
[7] = (decoded_time
->tm_isdst
)? Qt
: Qnil
;
929 /* Make a copy, in case gmtime modifies the struct. */
930 save_tm
= *decoded_time
;
931 decoded_time
= gmtime (&time_spec
);
932 if (decoded_time
== 0)
935 XSETINT (list_args
[8], tm_diff (&save_tm
, decoded_time
));
936 return Flist (9, list_args
);
939 DEFUN ("encode-time", Fencode_time
, Sencode_time
, 6, MANY
, 0,
940 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
941 This is the reverse operation of `decode-time', which see.\n\
942 ZONE defaults to the current time zone rule. This can\n\
943 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
944 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
945 applied without consideration for daylight savings time.\n\
947 You can pass more than 7 arguments; then the first six arguments\n\
948 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
949 The intervening arguments are ignored.\n\
950 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
952 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
953 for example, a DAY of 0 means the day preceding the given month.\n\
954 Year numbers less than 100 are treated just like other year numbers.\n\
955 If you want them to stand for years in this century, you must do that yourself.")
958 register Lisp_Object
*args
;
962 Lisp_Object zone
= (nargs
> 6 ? args
[nargs
- 1] : Qnil
);
964 CHECK_NUMBER (args
[0], 0); /* second */
965 CHECK_NUMBER (args
[1], 1); /* minute */
966 CHECK_NUMBER (args
[2], 2); /* hour */
967 CHECK_NUMBER (args
[3], 3); /* day */
968 CHECK_NUMBER (args
[4], 4); /* month */
969 CHECK_NUMBER (args
[5], 5); /* year */
971 tm
.tm_sec
= XINT (args
[0]);
972 tm
.tm_min
= XINT (args
[1]);
973 tm
.tm_hour
= XINT (args
[2]);
974 tm
.tm_mday
= XINT (args
[3]);
975 tm
.tm_mon
= XINT (args
[4]) - 1;
976 tm
.tm_year
= XINT (args
[5]) - 1900;
987 char **oldenv
= environ
, **newenv
;
991 else if (STRINGP (zone
))
992 tzstring
= (char *) XSTRING (zone
)->data
;
993 else if (INTEGERP (zone
))
995 int abszone
= abs (XINT (zone
));
996 sprintf (tzbuf
, "XXX%s%d:%02d:%02d", "-" + (XINT (zone
) < 0),
997 abszone
/ (60*60), (abszone
/60) % 60, abszone
% 60);
1001 error ("Invalid time zone specification");
1003 /* Set TZ before calling mktime; merely adjusting mktime's returned
1004 value doesn't suffice, since that would mishandle leap seconds. */
1005 set_time_zone_rule (tzstring
);
1007 time
= mktime (&tm
);
1009 /* Restore TZ to previous value. */
1013 #ifdef LOCALTIME_CACHE
1018 if (time
== (time_t) -1)
1019 error ("Specified time is not representable");
1021 return make_time (time
);
1024 DEFUN ("current-time-string", Fcurrent_time_string
, Scurrent_time_string
, 0, 1, 0,
1025 "Return the current time, as a human-readable string.\n\
1026 Programs can use this function to decode a time,\n\
1027 since the number of columns in each field is fixed.\n\
1028 The format is `Sun Sep 16 01:03:52 1973'.\n\
1029 However, see also the functions `decode-time' and `format-time-string'\n\
1030 which provide a much more powerful and general facility.\n\
1032 If an argument is given, it specifies a time to format\n\
1033 instead of the current time. The argument should have the form:\n\
1036 (HIGH LOW . IGNORED).\n\
1037 Thus, you can use times obtained from `current-time'\n\
1038 and from `file-attributes'.")
1040 Lisp_Object specified_time
;
1046 if (! lisp_time_argument (specified_time
, &value
))
1048 tem
= (char *) ctime (&value
);
1050 strncpy (buf
, tem
, 24);
1053 return build_string (buf
);
1056 #define TM_YEAR_BASE 1900
1058 /* Yield A - B, measured in seconds.
1059 This function is copied from the GNU C Library. */
1064 /* Compute intervening leap days correctly even if year is negative.
1065 Take care to avoid int overflow in leap day calculations,
1066 but it's OK to assume that A and B are close to each other. */
1067 int a4
= (a
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (a
->tm_year
& 3);
1068 int b4
= (b
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (b
->tm_year
& 3);
1069 int a100
= a4
/ 25 - (a4
% 25 < 0);
1070 int b100
= b4
/ 25 - (b4
% 25 < 0);
1071 int a400
= a100
>> 2;
1072 int b400
= b100
>> 2;
1073 int intervening_leap_days
= (a4
- b4
) - (a100
- b100
) + (a400
- b400
);
1074 int years
= a
->tm_year
- b
->tm_year
;
1075 int days
= (365 * years
+ intervening_leap_days
1076 + (a
->tm_yday
- b
->tm_yday
));
1077 return (60 * (60 * (24 * days
+ (a
->tm_hour
- b
->tm_hour
))
1078 + (a
->tm_min
- b
->tm_min
))
1079 + (a
->tm_sec
- b
->tm_sec
));
1082 DEFUN ("current-time-zone", Fcurrent_time_zone
, Scurrent_time_zone
, 0, 1, 0,
1083 "Return the offset and name for the local time zone.\n\
1084 This returns a list of the form (OFFSET NAME).\n\
1085 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1086 A negative value means west of Greenwich.\n\
1087 NAME is a string giving the name of the time zone.\n\
1088 If an argument is given, it specifies when the time zone offset is determined\n\
1089 instead of using the current time. The argument should have the form:\n\
1092 (HIGH LOW . IGNORED).\n\
1093 Thus, you can use times obtained from `current-time'\n\
1094 and from `file-attributes'.\n\
1096 Some operating systems cannot provide all this information to Emacs;\n\
1097 in this case, `current-time-zone' returns a list containing nil for\n\
1098 the data it can't find.")
1100 Lisp_Object specified_time
;
1105 if (lisp_time_argument (specified_time
, &value
)
1106 && (t
= gmtime (&value
)) != 0)
1112 gmt
= *t
; /* Make a copy, in case localtime modifies *t. */
1113 t
= localtime (&value
);
1114 offset
= tm_diff (t
, &gmt
);
1118 s
= (char *)t
->tm_zone
;
1119 #else /* not HAVE_TM_ZONE */
1121 if (t
->tm_isdst
== 0 || t
->tm_isdst
== 1)
1122 s
= tzname
[t
->tm_isdst
];
1124 #endif /* not HAVE_TM_ZONE */
1127 /* No local time zone name is available; use "+-NNNN" instead. */
1128 int am
= (offset
< 0 ? -offset
: offset
) / 60;
1129 sprintf (buf
, "%c%02d%02d", (offset
< 0 ? '-' : '+'), am
/60, am
%60);
1132 return Fcons (make_number (offset
), Fcons (build_string (s
), Qnil
));
1135 return Fmake_list (make_number (2), Qnil
);
1138 /* This holds the value of `environ' produced by the previous
1139 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1140 has never been called. */
1141 static char **environbuf
;
1143 DEFUN ("set-time-zone-rule", Fset_time_zone_rule
, Sset_time_zone_rule
, 1, 1, 0,
1144 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
1145 If TZ is nil, use implementation-defined default time zone information.\n\
1146 If TZ is t, use Universal Time.")
1154 else if (EQ (tz
, Qt
))
1158 CHECK_STRING (tz
, 0);
1159 tzstring
= (char *) XSTRING (tz
)->data
;
1162 set_time_zone_rule (tzstring
);
1165 environbuf
= environ
;
1170 #ifdef LOCALTIME_CACHE
1172 /* These two values are known to load tz files in buggy implementations,
1173 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1174 Their values shouldn't matter in non-buggy implementations.
1175 We don't use string literals for these strings,
1176 since if a string in the environment is in readonly
1177 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1178 See Sun bugs 1113095 and 1114114, ``Timezone routines
1179 improperly modify environment''. */
1181 static char set_time_zone_rule_tz1
[] = "TZ=GMT+0";
1182 static char set_time_zone_rule_tz2
[] = "TZ=GMT+1";
1186 /* Set the local time zone rule to TZSTRING.
1187 This allocates memory into `environ', which it is the caller's
1188 responsibility to free. */
1190 set_time_zone_rule (tzstring
)
1194 char **from
, **to
, **newenv
;
1196 /* Make the ENVIRON vector longer with room for TZSTRING. */
1197 for (from
= environ
; *from
; from
++)
1199 envptrs
= from
- environ
+ 2;
1200 newenv
= to
= (char **) xmalloc (envptrs
* sizeof (char *)
1201 + (tzstring
? strlen (tzstring
) + 4 : 0));
1203 /* Add TZSTRING to the end of environ, as a value for TZ. */
1206 char *t
= (char *) (to
+ envptrs
);
1208 strcat (t
, tzstring
);
1212 /* Copy the old environ vector elements into NEWENV,
1213 but don't copy the TZ variable.
1214 So we have only one definition of TZ, which came from TZSTRING. */
1215 for (from
= environ
; *from
; from
++)
1216 if (strncmp (*from
, "TZ=", 3) != 0)
1222 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1223 the TZ variable is stored. If we do not have a TZSTRING,
1224 TO points to the vector slot which has the terminating null. */
1226 #ifdef LOCALTIME_CACHE
1228 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1229 "US/Pacific" that loads a tz file, then changes to a value like
1230 "XXX0" that does not load a tz file, and then changes back to
1231 its original value, the last change is (incorrectly) ignored.
1232 Also, if TZ changes twice in succession to values that do
1233 not load a tz file, tzset can dump core (see Sun bug#1225179).
1234 The following code works around these bugs. */
1238 /* Temporarily set TZ to a value that loads a tz file
1239 and that differs from tzstring. */
1241 *newenv
= (strcmp (tzstring
, set_time_zone_rule_tz1
+ 3) == 0
1242 ? set_time_zone_rule_tz2
: set_time_zone_rule_tz1
);
1248 /* The implied tzstring is unknown, so temporarily set TZ to
1249 two different values that each load a tz file. */
1250 *to
= set_time_zone_rule_tz1
;
1253 *to
= set_time_zone_rule_tz2
;
1258 /* Now TZ has the desired value, and tzset can be invoked safely. */
1265 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1266 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1267 type of object is Lisp_String). INHERIT is passed to
1268 INSERT_FROM_STRING_FUNC as the last argument. */
1270 general_insert_function (insert_func
, insert_from_string_func
,
1271 inherit
, nargs
, args
)
1272 int (*insert_func
)(), (*insert_from_string_func
)();
1274 register Lisp_Object
*args
;
1276 register int argnum
;
1277 register Lisp_Object val
;
1279 for (argnum
= 0; argnum
< nargs
; argnum
++)
1285 char workbuf
[4], *str
;
1288 if (!NILP (current_buffer
->enable_multibyte_characters
))
1289 len
= CHAR_STRING (XFASTINT (val
), workbuf
, str
);
1291 workbuf
[0] = XINT (val
), str
= workbuf
, len
= 1;
1292 (*insert_func
) (str
, len
);
1294 else if (STRINGP (val
))
1296 (*insert_from_string_func
) (val
, 0, XSTRING (val
)->size
, inherit
);
1300 val
= wrong_type_argument (Qchar_or_string_p
, val
);
1314 /* Callers passing one argument to Finsert need not gcpro the
1315 argument "array", since the only element of the array will
1316 not be used after calling insert or insert_from_string, so
1317 we don't care if it gets trashed. */
1319 DEFUN ("insert", Finsert
, Sinsert
, 0, MANY
, 0,
1320 "Insert the arguments, either strings or characters, at point.\n\
1321 Point and before-insertion-markers move forward so that it ends up\n\
1322 after the inserted text.\n\
1323 Any other markers at the point of insertion remain before the text.")
1326 register Lisp_Object
*args
;
1328 general_insert_function (insert
, insert_from_string
, 0, nargs
, args
);
1332 DEFUN ("insert-and-inherit", Finsert_and_inherit
, Sinsert_and_inherit
,
1334 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1335 Point and before-insertion-markers move forward so that it ends up\n\
1336 after the inserted text.\n\
1337 Any other markers at the point of insertion remain before the text.")
1340 register Lisp_Object
*args
;
1342 general_insert_function (insert_and_inherit
, insert_from_string
, 1,
1347 DEFUN ("insert-before-markers", Finsert_before_markers
, Sinsert_before_markers
, 0, MANY
, 0,
1348 "Insert strings or characters at point, relocating markers after the text.\n\
1349 Point and before-insertion-markers move forward so that it ends up\n\
1350 after the inserted text.\n\
1351 Any other markers at the point of insertion also end up after the text.")
1354 register Lisp_Object
*args
;
1356 general_insert_function (insert_before_markers
,
1357 insert_from_string_before_markers
, 0,
1362 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers
,
1363 Sinsert_and_inherit_before_markers
, 0, MANY
, 0,
1364 "Insert text at point, relocating markers and inheriting properties.\n\
1365 Point moves forward so that it ends up after the inserted text.\n\
1366 Any other markers at the point of insertion also end up after the text.")
1369 register Lisp_Object
*args
;
1371 general_insert_function (insert_before_markers_and_inherit
,
1372 insert_from_string_before_markers
, 1,
1377 DEFUN ("insert-char", Finsert_char
, Sinsert_char
, 2, 3, 0,
1378 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1379 Point and before-insertion-markers are affected as in the function `insert'.\n\
1380 Both arguments are required.\n\
1381 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1382 from adjoining text, if those properties are sticky.")
1383 (character
, count
, inherit
)
1384 Lisp_Object character
, count
, inherit
;
1386 register unsigned char *string
;
1387 register int strlen
;
1390 unsigned char workbuf
[4], *str
;
1392 CHECK_NUMBER (character
, 0);
1393 CHECK_NUMBER (count
, 1);
1395 if (!NILP (current_buffer
->enable_multibyte_characters
))
1396 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
1398 workbuf
[0] = XFASTINT (character
), str
= workbuf
, len
= 1;
1399 n
= XINT (count
) * len
;
1402 strlen
= min (n
, 256 * len
);
1403 string
= (unsigned char *) alloca (strlen
);
1404 for (i
= 0; i
< strlen
; i
++)
1405 string
[i
] = str
[i
% len
];
1409 if (!NILP (inherit
))
1410 insert_and_inherit (string
, strlen
);
1412 insert (string
, strlen
);
1417 if (!NILP (inherit
))
1418 insert_and_inherit (string
, n
);
1426 /* Making strings from buffer contents. */
1428 /* Return a Lisp_String containing the text of the current buffer from
1429 START to END. If text properties are in use and the current buffer
1430 has properties in the range specified, the resulting string will also
1431 have them, if PROPS is nonzero.
1433 We don't want to use plain old make_string here, because it calls
1434 make_uninit_string, which can cause the buffer arena to be
1435 compacted. make_string has no way of knowing that the data has
1436 been moved, and thus copies the wrong data into the string. This
1437 doesn't effect most of the other users of make_string, so it should
1438 be left as is. But we should use this function when conjuring
1439 buffer substrings. */
1442 make_buffer_string (start
, end
, props
)
1446 Lisp_Object result
, tem
, tem1
;
1448 if (start
< GPT
&& GPT
< end
)
1451 result
= make_uninit_string (end
- start
);
1452 bcopy (POS_ADDR (start
), XSTRING (result
)->data
, end
- start
);
1454 /* If desired, update and copy the text properties. */
1455 #ifdef USE_TEXT_PROPERTIES
1458 update_buffer_properties (start
, end
);
1460 tem
= Fnext_property_change (make_number (start
), Qnil
, make_number (end
));
1461 tem1
= Ftext_properties_at (make_number (start
), Qnil
);
1463 if (XINT (tem
) != end
|| !NILP (tem1
))
1464 copy_intervals_to_string (result
, current_buffer
, start
, end
- start
);
1471 /* Call Vbuffer_access_fontify_functions for the range START ... END
1472 in the current buffer, if necessary. */
1475 update_buffer_properties (start
, end
)
1478 #ifdef USE_TEXT_PROPERTIES
1479 /* If this buffer has some access functions,
1480 call them, specifying the range of the buffer being accessed. */
1481 if (!NILP (Vbuffer_access_fontify_functions
))
1483 Lisp_Object args
[3];
1486 args
[0] = Qbuffer_access_fontify_functions
;
1487 XSETINT (args
[1], start
);
1488 XSETINT (args
[2], end
);
1490 /* But don't call them if we can tell that the work
1491 has already been done. */
1492 if (!NILP (Vbuffer_access_fontified_property
))
1494 tem
= Ftext_property_any (args
[1], args
[2],
1495 Vbuffer_access_fontified_property
,
1498 Frun_hook_with_args (3, args
);
1501 Frun_hook_with_args (3, args
);
1506 DEFUN ("buffer-substring", Fbuffer_substring
, Sbuffer_substring
, 2, 2, 0,
1507 "Return the contents of part of the current buffer as a string.\n\
1508 The two arguments START and END are character positions;\n\
1509 they can be in either order.")
1511 Lisp_Object start
, end
;
1515 validate_region (&start
, &end
);
1519 return make_buffer_string (b
, e
, 1);
1522 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties
,
1523 Sbuffer_substring_no_properties
, 2, 2, 0,
1524 "Return the characters of part of the buffer, without the text properties.\n\
1525 The two arguments START and END are character positions;\n\
1526 they can be in either order.")
1528 Lisp_Object start
, end
;
1532 validate_region (&start
, &end
);
1536 return make_buffer_string (b
, e
, 0);
1539 DEFUN ("buffer-string", Fbuffer_string
, Sbuffer_string
, 0, 0, 0,
1540 "Return the contents of the current buffer as a string.\n\
1541 If narrowing is in effect, this function returns only the visible part\n\
1545 return make_buffer_string (BEGV
, ZV
, 1);
1548 DEFUN ("insert-buffer-substring", Finsert_buffer_substring
, Sinsert_buffer_substring
,
1550 "Insert before point a substring of the contents of buffer BUFFER.\n\
1551 BUFFER may be a buffer or a buffer name.\n\
1552 Arguments START and END are character numbers specifying the substring.\n\
1553 They default to the beginning and the end of BUFFER.")
1555 Lisp_Object buf
, start
, end
;
1557 register int b
, e
, temp
;
1558 register struct buffer
*bp
, *obuf
;
1561 buffer
= Fget_buffer (buf
);
1564 bp
= XBUFFER (buffer
);
1565 if (NILP (bp
->name
))
1566 error ("Selecting deleted buffer");
1572 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1579 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1584 temp
= b
, b
= e
, e
= temp
;
1586 if (!(BUF_BEGV (bp
) <= b
&& e
<= BUF_ZV (bp
)))
1587 args_out_of_range (start
, end
);
1589 obuf
= current_buffer
;
1590 set_buffer_internal_1 (bp
);
1591 update_buffer_properties (b
, e
);
1592 set_buffer_internal_1 (obuf
);
1594 insert_from_buffer (bp
, b
, e
- b
, 0);
1598 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings
, Scompare_buffer_substrings
,
1600 "Compare two substrings of two buffers; return result as number.\n\
1601 the value is -N if first string is less after N-1 chars,\n\
1602 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
1603 Each substring is represented as three arguments: BUFFER, START and END.\n\
1604 That makes six args in all, three for each substring.\n\n\
1605 The value of `case-fold-search' in the current buffer\n\
1606 determines whether case is significant or ignored.")
1607 (buffer1
, start1
, end1
, buffer2
, start2
, end2
)
1608 Lisp_Object buffer1
, start1
, end1
, buffer2
, start2
, end2
;
1610 register int begp1
, endp1
, begp2
, endp2
, temp
, len1
, len2
, length
, i
;
1611 register struct buffer
*bp1
, *bp2
;
1612 register Lisp_Object
*trt
1613 = (!NILP (current_buffer
->case_fold_search
)
1614 ? XCHAR_TABLE (current_buffer
->case_canon_table
)->contents
: 0);
1616 /* Find the first buffer and its substring. */
1619 bp1
= current_buffer
;
1623 buf1
= Fget_buffer (buffer1
);
1626 bp1
= XBUFFER (buf1
);
1627 if (NILP (bp1
->name
))
1628 error ("Selecting deleted buffer");
1632 begp1
= BUF_BEGV (bp1
);
1635 CHECK_NUMBER_COERCE_MARKER (start1
, 1);
1636 begp1
= XINT (start1
);
1639 endp1
= BUF_ZV (bp1
);
1642 CHECK_NUMBER_COERCE_MARKER (end1
, 2);
1643 endp1
= XINT (end1
);
1647 temp
= begp1
, begp1
= endp1
, endp1
= temp
;
1649 if (!(BUF_BEGV (bp1
) <= begp1
1651 && endp1
<= BUF_ZV (bp1
)))
1652 args_out_of_range (start1
, end1
);
1654 /* Likewise for second substring. */
1657 bp2
= current_buffer
;
1661 buf2
= Fget_buffer (buffer2
);
1664 bp2
= XBUFFER (buf2
);
1665 if (NILP (bp2
->name
))
1666 error ("Selecting deleted buffer");
1670 begp2
= BUF_BEGV (bp2
);
1673 CHECK_NUMBER_COERCE_MARKER (start2
, 4);
1674 begp2
= XINT (start2
);
1677 endp2
= BUF_ZV (bp2
);
1680 CHECK_NUMBER_COERCE_MARKER (end2
, 5);
1681 endp2
= XINT (end2
);
1685 temp
= begp2
, begp2
= endp2
, endp2
= temp
;
1687 if (!(BUF_BEGV (bp2
) <= begp2
1689 && endp2
<= BUF_ZV (bp2
)))
1690 args_out_of_range (start2
, end2
);
1692 len1
= endp1
- begp1
;
1693 len2
= endp2
- begp2
;
1698 for (i
= 0; i
< length
; i
++)
1700 int c1
= *BUF_CHAR_ADDRESS (bp1
, begp1
+ i
);
1701 int c2
= *BUF_CHAR_ADDRESS (bp2
, begp2
+ i
);
1704 c1
= XINT (trt
[c1
]);
1705 c2
= XINT (trt
[c2
]);
1708 return make_number (- 1 - i
);
1710 return make_number (i
+ 1);
1713 /* The strings match as far as they go.
1714 If one is shorter, that one is less. */
1716 return make_number (length
+ 1);
1717 else if (length
< len2
)
1718 return make_number (- length
- 1);
1720 /* Same length too => they are equal. */
1721 return make_number (0);
1725 subst_char_in_region_unwind (arg
)
1728 return current_buffer
->undo_list
= arg
;
1732 subst_char_in_region_unwind_1 (arg
)
1735 return current_buffer
->filename
= arg
;
1738 DEFUN ("subst-char-in-region", Fsubst_char_in_region
,
1739 Ssubst_char_in_region
, 4, 5, 0,
1740 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1741 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1742 and don't mark the buffer as really changed.\n\
1743 Both characters must have the same length of multi-byte form.")
1744 (start
, end
, fromchar
, tochar
, noundo
)
1745 Lisp_Object start
, end
, fromchar
, tochar
, noundo
;
1747 register int pos
, stop
, i
, len
;
1749 unsigned char fromwork
[4], *fromstr
, towork
[4], *tostr
, *p
;
1750 int count
= specpdl_ptr
- specpdl
;
1752 validate_region (&start
, &end
);
1753 CHECK_NUMBER (fromchar
, 2);
1754 CHECK_NUMBER (tochar
, 3);
1756 if (! NILP (current_buffer
->enable_multibyte_characters
))
1758 len
= CHAR_STRING (XFASTINT (fromchar
), fromwork
, fromstr
);
1759 if (CHAR_STRING (XFASTINT (tochar
), towork
, tostr
) != len
)
1760 error ("Characters in subst-char-in-region have different byte-lengths");
1765 fromwork
[0] = XFASTINT (fromchar
), fromstr
= fromwork
;
1766 towork
[0] = XFASTINT (tochar
), tostr
= towork
;
1772 /* If we don't want undo, turn off putting stuff on the list.
1773 That's faster than getting rid of things,
1774 and it prevents even the entry for a first change.
1775 Also inhibit locking the file. */
1778 record_unwind_protect (subst_char_in_region_unwind
,
1779 current_buffer
->undo_list
);
1780 current_buffer
->undo_list
= Qt
;
1781 /* Don't do file-locking. */
1782 record_unwind_protect (subst_char_in_region_unwind_1
,
1783 current_buffer
->filename
);
1784 current_buffer
->filename
= Qnil
;
1788 stop
= min(stop
, GPT
);
1794 if (pos
>= XINT (end
)) break;
1798 if (p
[0] == fromstr
[0]
1800 || (p
[1] == fromstr
[1]
1801 && (len
== 2 || (p
[2] == fromstr
[2]
1802 && (len
== 3 || p
[3] == fromstr
[3]))))))
1806 modify_region (current_buffer
, XINT (start
), XINT (end
));
1808 if (! NILP (noundo
))
1810 if (MODIFF
- 1 == SAVE_MODIFF
)
1812 if (MODIFF
- 1 == current_buffer
->auto_save_modified
)
1813 current_buffer
->auto_save_modified
++;
1820 record_change (pos
, len
);
1821 for (i
= 0; i
< len
; i
++) *p
++ = tostr
[i
];
1829 signal_after_change (XINT (start
),
1830 stop
- XINT (start
), stop
- XINT (start
));
1832 unbind_to (count
, Qnil
);
1836 DEFUN ("translate-region", Ftranslate_region
, Stranslate_region
, 3, 3, 0,
1837 "From START to END, translate characters according to TABLE.\n\
1838 TABLE is a string; the Nth character in it is the mapping\n\
1839 for the character with code N. Returns the number of characters changed.")
1843 register Lisp_Object table
;
1845 register int pos
, stop
; /* Limits of the region. */
1846 register unsigned char *tt
; /* Trans table. */
1847 register int oc
; /* Old character. */
1848 register int nc
; /* New character. */
1849 int cnt
; /* Number of changes made. */
1850 Lisp_Object z
; /* Return. */
1851 int size
; /* Size of translate table. */
1853 validate_region (&start
, &end
);
1854 CHECK_STRING (table
, 2);
1856 size
= XSTRING (table
)->size
;
1857 tt
= XSTRING (table
)->data
;
1861 modify_region (current_buffer
, pos
, stop
);
1864 for (; pos
< stop
; ++pos
)
1866 oc
= FETCH_BYTE (pos
);
1872 record_change (pos
, 1);
1873 *(POS_ADDR (pos
)) = nc
;
1874 signal_after_change (pos
, 1, 1);
1880 XSETFASTINT (z
, cnt
);
1884 DEFUN ("delete-region", Fdelete_region
, Sdelete_region
, 2, 2, "r",
1885 "Delete the text between point and mark.\n\
1886 When called from a program, expects two arguments,\n\
1887 positions (integers or markers) specifying the stretch to be deleted.")
1889 Lisp_Object start
, end
;
1891 validate_region (&start
, &end
);
1892 del_range (XINT (start
), XINT (end
));
1896 DEFUN ("widen", Fwiden
, Swiden
, 0, 0, "",
1897 "Remove restrictions (narrowing) from current buffer.\n\
1898 This allows the buffer's full text to be seen and edited.")
1901 if (BEG
!= BEGV
|| Z
!= ZV
)
1902 current_buffer
->clip_changed
= 1;
1904 SET_BUF_ZV (current_buffer
, Z
);
1905 /* Changing the buffer bounds invalidates any recorded current column. */
1906 invalidate_current_column ();
1910 DEFUN ("narrow-to-region", Fnarrow_to_region
, Snarrow_to_region
, 2, 2, "r",
1911 "Restrict editing in this buffer to the current region.\n\
1912 The rest of the text becomes temporarily invisible and untouchable\n\
1913 but is not deleted; if you save the buffer in a file, the invisible\n\
1914 text is included in the file. \\[widen] makes all visible again.\n\
1915 See also `save-restriction'.\n\
1917 When calling from a program, pass two arguments; positions (integers\n\
1918 or markers) bounding the text that should remain visible.")
1920 register Lisp_Object start
, end
;
1922 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1923 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1925 if (XINT (start
) > XINT (end
))
1928 tem
= start
; start
= end
; end
= tem
;
1931 if (!(BEG
<= XINT (start
) && XINT (start
) <= XINT (end
) && XINT (end
) <= Z
))
1932 args_out_of_range (start
, end
);
1934 if (BEGV
!= XFASTINT (start
) || ZV
!= XFASTINT (end
))
1935 current_buffer
->clip_changed
= 1;
1937 BEGV
= XFASTINT (start
);
1938 SET_BUF_ZV (current_buffer
, XFASTINT (end
));
1939 if (PT
< XFASTINT (start
))
1940 SET_PT (XFASTINT (start
));
1941 if (PT
> XFASTINT (end
))
1942 SET_PT (XFASTINT (end
));
1943 /* Changing the buffer bounds invalidates any recorded current column. */
1944 invalidate_current_column ();
1949 save_restriction_save ()
1951 register Lisp_Object bottom
, top
;
1952 /* Note: I tried using markers here, but it does not win
1953 because insertion at the end of the saved region
1954 does not advance mh and is considered "outside" the saved region. */
1955 XSETFASTINT (bottom
, BEGV
- BEG
);
1956 XSETFASTINT (top
, Z
- ZV
);
1958 return Fcons (Fcurrent_buffer (), Fcons (bottom
, top
));
1962 save_restriction_restore (data
)
1965 register struct buffer
*buf
;
1966 register int newhead
, newtail
;
1967 register Lisp_Object tem
;
1970 buf
= XBUFFER (XCONS (data
)->car
);
1972 data
= XCONS (data
)->cdr
;
1974 tem
= XCONS (data
)->car
;
1975 newhead
= XINT (tem
);
1976 tem
= XCONS (data
)->cdr
;
1977 newtail
= XINT (tem
);
1978 if (newhead
+ newtail
> BUF_Z (buf
) - BUF_BEG (buf
))
1984 obegv
= BUF_BEGV (buf
);
1987 BUF_BEGV (buf
) = BUF_BEG (buf
) + newhead
;
1988 SET_BUF_ZV (buf
, BUF_Z (buf
) - newtail
);
1990 if (obegv
!= BUF_BEGV (buf
) || ozv
!= BUF_ZV (buf
))
1991 current_buffer
->clip_changed
= 1;
1993 /* If point is outside the new visible range, move it inside. */
1995 clip_to_bounds (BUF_BEGV (buf
), BUF_PT (buf
), BUF_ZV (buf
)));
2000 DEFUN ("save-restriction", Fsave_restriction
, Ssave_restriction
, 0, UNEVALLED
, 0,
2001 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2002 The buffer's restrictions make parts of the beginning and end invisible.\n\
2003 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2004 This special form, `save-restriction', saves the current buffer's restrictions\n\
2005 when it is entered, and restores them when it is exited.\n\
2006 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2007 The old restrictions settings are restored\n\
2008 even in case of abnormal exit (throw or error).\n\
2010 The value returned is the value of the last form in BODY.\n\
2012 `save-restriction' can get confused if, within the BODY, you widen\n\
2013 and then make changes outside the area within the saved restrictions.\n\
2015 Note: if you are using both `save-excursion' and `save-restriction',\n\
2016 use `save-excursion' outermost:\n\
2017 (save-excursion (save-restriction ...))")
2021 register Lisp_Object val
;
2022 int count
= specpdl_ptr
- specpdl
;
2024 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
2025 val
= Fprogn (body
);
2026 return unbind_to (count
, val
);
2029 /* Buffer for the most recent text displayed by Fmessage. */
2030 static char *message_text
;
2032 /* Allocated length of that buffer. */
2033 static int message_length
;
2035 DEFUN ("message", Fmessage
, Smessage
, 1, MANY
, 0,
2036 "Print a one-line message at the bottom of the screen.\n\
2037 The first argument is a format control string, and the rest are data\n\
2038 to be formatted under control of the string. See `format' for details.\n\
2040 If the first argument is nil, clear any existing message; let the\n\
2041 minibuffer contents show.")
2053 register Lisp_Object val
;
2054 val
= Fformat (nargs
, args
);
2055 /* Copy the data so that it won't move when we GC. */
2058 message_text
= (char *)xmalloc (80);
2059 message_length
= 80;
2061 if (XSTRING (val
)->size
> message_length
)
2063 message_length
= XSTRING (val
)->size
;
2064 message_text
= (char *)xrealloc (message_text
, message_length
);
2066 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
2067 message2 (message_text
, XSTRING (val
)->size
);
2072 DEFUN ("message-box", Fmessage_box
, Smessage_box
, 1, MANY
, 0,
2073 "Display a message, in a dialog box if possible.\n\
2074 If a dialog box is not available, use the echo area.\n\
2075 The first argument is a format control string, and the rest are data\n\
2076 to be formatted under control of the string. See `format' for details.\n\
2078 If the first argument is nil, clear any existing message; let the\n\
2079 minibuffer contents show.")
2091 register Lisp_Object val
;
2092 val
= Fformat (nargs
, args
);
2095 Lisp_Object pane
, menu
, obj
;
2096 struct gcpro gcpro1
;
2097 pane
= Fcons (Fcons (build_string ("OK"), Qt
), Qnil
);
2099 menu
= Fcons (val
, pane
);
2100 obj
= Fx_popup_dialog (Qt
, menu
);
2104 #else /* not HAVE_MENUS */
2105 /* Copy the data so that it won't move when we GC. */
2108 message_text
= (char *)xmalloc (80);
2109 message_length
= 80;
2111 if (XSTRING (val
)->size
> message_length
)
2113 message_length
= XSTRING (val
)->size
;
2114 message_text
= (char *)xrealloc (message_text
, message_length
);
2116 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
2117 message2 (message_text
, XSTRING (val
)->size
);
2119 #endif /* not HAVE_MENUS */
2123 extern Lisp_Object last_nonmenu_event
;
2126 DEFUN ("message-or-box", Fmessage_or_box
, Smessage_or_box
, 1, MANY
, 0,
2127 "Display a message in a dialog box or in the echo area.\n\
2128 If this command was invoked with the mouse, use a dialog box.\n\
2129 Otherwise, use the echo area.\n\
2130 The first argument is a format control string, and the rest are data\n\
2131 to be formatted under control of the string. See `format' for details.\n\
2133 If the first argument is nil, clear any existing message; let the\n\
2134 minibuffer contents show.")
2140 if (NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2141 return Fmessage_box (nargs
, args
);
2143 return Fmessage (nargs
, args
);
2146 DEFUN ("current-message", Fcurrent_message
, Scurrent_message
, 0, 0, 0,
2147 "Return the string currently displayed in the echo area, or nil if none.")
2150 return (echo_area_glyphs
2151 ? make_string (echo_area_glyphs
, echo_area_glyphs_length
)
2155 DEFUN ("format", Fformat
, Sformat
, 1, MANY
, 0,
2156 "Format a string out of a control-string and arguments.\n\
2157 The first argument is a control string.\n\
2158 The other arguments are substituted into it to make the result, a string.\n\
2159 It may contain %-sequences meaning to substitute the next argument.\n\
2160 %s means print a string argument. Actually, prints any object, with `princ'.\n\
2161 %d means print as number in decimal (%o octal, %x hex).\n\
2162 %e means print a number in exponential notation.\n\
2163 %f means print a number in decimal-point notation.\n\
2164 %g means print a number in exponential notation\n\
2165 or decimal-point notation, whichever uses fewer characters.\n\
2166 %c means print a number as a single character.\n\
2167 %S means print any object as an s-expression (using prin1).\n\
2168 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2169 Use %% to put a single % into the output.")
2172 register Lisp_Object
*args
;
2174 register int n
; /* The number of the next arg to substitute */
2175 register int total
= 5; /* An estimate of the final length */
2177 register unsigned char *format
, *end
;
2179 extern char *index ();
2180 /* It should not be necessary to GCPRO ARGS, because
2181 the caller in the interpreter should take care of that. */
2183 CHECK_STRING (args
[0], 0);
2184 format
= XSTRING (args
[0])->data
;
2185 end
= format
+ XSTRING (args
[0])->size
;
2188 while (format
!= end
)
2189 if (*format
++ == '%')
2193 /* Process a numeric arg and skip it. */
2194 minlen
= atoi (format
);
2198 while ((*format
>= '0' && *format
<= '9')
2199 || *format
== '-' || *format
== ' ' || *format
== '.')
2204 else if (++n
>= nargs
)
2205 error ("Not enough arguments for format string");
2206 else if (*format
== 'S')
2208 /* For `S', prin1 the argument and then treat like a string. */
2209 register Lisp_Object tem
;
2210 tem
= Fprin1_to_string (args
[n
], Qnil
);
2214 else if (SYMBOLP (args
[n
]))
2216 XSETSTRING (args
[n
], XSYMBOL (args
[n
])->name
);
2219 else if (STRINGP (args
[n
]))
2222 if (*format
!= 's' && *format
!= 'S')
2223 error ("format specifier doesn't match argument type");
2224 total
+= XSTRING (args
[n
])->size
;
2225 /* We have to put an arbitrary limit on minlen
2226 since otherwise it could make alloca fail. */
2227 if (minlen
< XSTRING (args
[n
])->size
+ 1000)
2230 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
2231 else if (INTEGERP (args
[n
]) && *format
!= 's')
2233 #ifdef LISP_FLOAT_TYPE
2234 /* The following loop assumes the Lisp type indicates
2235 the proper way to pass the argument.
2236 So make sure we have a flonum if the argument should
2238 if (*format
== 'e' || *format
== 'f' || *format
== 'g')
2239 args
[n
] = Ffloat (args
[n
]);
2242 /* We have to put an arbitrary limit on minlen
2243 since otherwise it could make alloca fail. */
2247 #ifdef LISP_FLOAT_TYPE
2248 else if (FLOATP (args
[n
]) && *format
!= 's')
2250 if (! (*format
== 'e' || *format
== 'f' || *format
== 'g'))
2251 args
[n
] = Ftruncate (args
[n
], Qnil
);
2253 /* We have to put an arbitrary limit on minlen
2254 since otherwise it could make alloca fail. */
2261 /* Anything but a string, convert to a string using princ. */
2262 register Lisp_Object tem
;
2263 tem
= Fprin1_to_string (args
[n
], Qt
);
2270 register int nstrings
= n
+ 1;
2272 /* Allocate twice as many strings as we have %-escapes; floats occupy
2273 two slots, and we're not sure how many of those we have. */
2274 register unsigned char **strings
2275 = (unsigned char **) alloca (2 * nstrings
* sizeof (unsigned char *));
2279 for (n
= 0; n
< nstrings
; n
++)
2282 strings
[i
++] = (unsigned char *) "";
2283 else if (INTEGERP (args
[n
]))
2284 /* We checked above that the corresponding format effector
2285 isn't %s, which would cause MPV. */
2286 strings
[i
++] = (unsigned char *) XINT (args
[n
]);
2287 #ifdef LISP_FLOAT_TYPE
2288 else if (FLOATP (args
[n
]))
2290 union { double d
; char *half
[2]; } u
;
2292 u
.d
= XFLOAT (args
[n
])->data
;
2293 strings
[i
++] = (unsigned char *) u
.half
[0];
2294 strings
[i
++] = (unsigned char *) u
.half
[1];
2298 /* The first string is treated differently
2299 because it is the format string. */
2300 strings
[i
++] = XSTRING (args
[n
])->data
;
2302 strings
[i
++] = (unsigned char *) XSTRING (args
[n
]);
2305 /* Make room in result for all the non-%-codes in the control string. */
2306 total
+= XSTRING (args
[0])->size
;
2308 /* Format it in bigger and bigger buf's until it all fits. */
2311 buf
= (char *) alloca (total
+ 1);
2314 length
= doprnt_lisp (buf
, total
+ 1, strings
[0],
2315 end
, i
-1, strings
+ 1);
2316 if (buf
[total
- 1] == 0)
2324 return make_string (buf
, length
);
2330 format1 (string1
, arg0
, arg1
, arg2
, arg3
, arg4
)
2331 EMACS_INT arg0
, arg1
, arg2
, arg3
, arg4
;
2345 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, args
);
2347 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, &string1
+ 1);
2349 return build_string (buf
);
2352 DEFUN ("char-equal", Fchar_equal
, Schar_equal
, 2, 2, 0,
2353 "Return t if two characters match, optionally ignoring case.\n\
2354 Both arguments must be characters (i.e. integers).\n\
2355 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2357 register Lisp_Object c1
, c2
;
2359 CHECK_NUMBER (c1
, 0);
2360 CHECK_NUMBER (c2
, 1);
2362 if (XINT (c1
) == XINT (c2
)
2363 && (NILP (current_buffer
->case_fold_search
)
2364 || DOWNCASE (XFASTINT (c1
)) == DOWNCASE (XFASTINT (c2
))))
2369 /* Transpose the markers in two regions of the current buffer, and
2370 adjust the ones between them if necessary (i.e.: if the regions
2373 Traverses the entire marker list of the buffer to do so, adding an
2374 appropriate amount to some, subtracting from some, and leaving the
2375 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2377 It's the caller's job to see that (start1 <= end1 <= start2 <= end2). */
2380 transpose_markers (start1
, end1
, start2
, end2
)
2381 register int start1
, end1
, start2
, end2
;
2383 register int amt1
, amt2
, diff
, mpos
;
2384 register Lisp_Object marker
;
2386 /* Update point as if it were a marker. */
2390 TEMP_SET_PT (PT
+ (end2
- end1
));
2391 else if (PT
< start2
)
2392 TEMP_SET_PT (PT
+ (end2
- start2
) - (end1
- start1
));
2394 TEMP_SET_PT (PT
- (start2
- start1
));
2396 /* We used to adjust the endpoints here to account for the gap, but that
2397 isn't good enough. Even if we assume the caller has tried to move the
2398 gap out of our way, it might still be at start1 exactly, for example;
2399 and that places it `inside' the interval, for our purposes. The amount
2400 of adjustment is nontrivial if there's a `denormalized' marker whose
2401 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2402 the dirty work to Fmarker_position, below. */
2404 /* The difference between the region's lengths */
2405 diff
= (end2
- start2
) - (end1
- start1
);
2407 /* For shifting each marker in a region by the length of the other
2408 * region plus the distance between the regions.
2410 amt1
= (end2
- start2
) + (start2
- end1
);
2411 amt2
= (end1
- start1
) + (start2
- end1
);
2413 for (marker
= BUF_MARKERS (current_buffer
); !NILP (marker
);
2414 marker
= XMARKER (marker
)->chain
)
2416 mpos
= marker_position (marker
);
2417 if (mpos
>= start1
&& mpos
< end2
)
2421 else if (mpos
< start2
)
2425 if (mpos
> GPT
) mpos
+= GAP_SIZE
;
2426 XMARKER (marker
)->bufpos
= mpos
;
2431 DEFUN ("transpose-regions", Ftranspose_regions
, Stranspose_regions
, 4, 5, 0,
2432 "Transpose region START1 to END1 with START2 to END2.\n\
2433 The regions may not be overlapping, because the size of the buffer is\n\
2434 never changed in a transposition.\n\
2436 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't transpose\n\
2437 any markers that happen to be located in the regions.\n\
2439 Transposing beyond buffer boundaries is an error.")
2440 (startr1
, endr1
, startr2
, endr2
, leave_markers
)
2441 Lisp_Object startr1
, endr1
, startr2
, endr2
, leave_markers
;
2443 register int start1
, end1
, start2
, end2
,
2444 gap
, len1
, len_mid
, len2
;
2445 unsigned char *start1_addr
, *start2_addr
, *temp
;
2447 #ifdef USE_TEXT_PROPERTIES
2448 INTERVAL cur_intv
, tmp_interval1
, tmp_interval_mid
, tmp_interval2
;
2449 cur_intv
= BUF_INTERVALS (current_buffer
);
2450 #endif /* USE_TEXT_PROPERTIES */
2452 validate_region (&startr1
, &endr1
);
2453 validate_region (&startr2
, &endr2
);
2455 start1
= XFASTINT (startr1
);
2456 end1
= XFASTINT (endr1
);
2457 start2
= XFASTINT (startr2
);
2458 end2
= XFASTINT (endr2
);
2461 /* Swap the regions if they're reversed. */
2464 register int glumph
= start1
;
2472 len1
= end1
- start1
;
2473 len2
= end2
- start2
;
2476 error ("transposed regions not properly ordered");
2477 else if (start1
== end1
|| start2
== end2
)
2478 error ("transposed region may not be of length 0");
2480 /* The possibilities are:
2481 1. Adjacent (contiguous) regions, or separate but equal regions
2482 (no, really equal, in this case!), or
2483 2. Separate regions of unequal size.
2485 The worst case is usually No. 2. It means that (aside from
2486 potential need for getting the gap out of the way), there also
2487 needs to be a shifting of the text between the two regions. So
2488 if they are spread far apart, we are that much slower... sigh. */
2490 /* It must be pointed out that the really studly thing to do would
2491 be not to move the gap at all, but to leave it in place and work
2492 around it if necessary. This would be extremely efficient,
2493 especially considering that people are likely to do
2494 transpositions near where they are working interactively, which
2495 is exactly where the gap would be found. However, such code
2496 would be much harder to write and to read. So, if you are
2497 reading this comment and are feeling squirrely, by all means have
2498 a go! I just didn't feel like doing it, so I will simply move
2499 the gap the minimum distance to get it out of the way, and then
2500 deal with an unbroken array. */
2502 /* Make sure the gap won't interfere, by moving it out of the text
2503 we will operate on. */
2504 if (start1
< gap
&& gap
< end2
)
2506 if (gap
- start1
< end2
- gap
)
2512 /* Hmmm... how about checking to see if the gap is large
2513 enough to use as the temporary storage? That would avoid an
2514 allocation... interesting. Later, don't fool with it now. */
2516 /* Working without memmove, for portability (sigh), so must be
2517 careful of overlapping subsections of the array... */
2519 if (end1
== start2
) /* adjacent regions */
2521 modify_region (current_buffer
, start1
, end2
);
2522 record_change (start1
, len1
+ len2
);
2524 #ifdef USE_TEXT_PROPERTIES
2525 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2526 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2527 Fset_text_properties (make_number (start1
), make_number (end2
),
2529 #endif /* USE_TEXT_PROPERTIES */
2531 /* First region smaller than second. */
2534 /* We use alloca only if it is small,
2535 because we want to avoid stack overflow. */
2537 temp
= (unsigned char *) xmalloc (len2
);
2539 temp
= (unsigned char *) alloca (len2
);
2541 /* Don't precompute these addresses. We have to compute them
2542 at the last minute, because the relocating allocator might
2543 have moved the buffer around during the xmalloc. */
2544 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2545 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2547 bcopy (start2_addr
, temp
, len2
);
2548 bcopy (start1_addr
, start1_addr
+ len2
, len1
);
2549 bcopy (temp
, start1_addr
, len2
);
2554 /* First region not smaller than second. */
2557 temp
= (unsigned char *) xmalloc (len1
);
2559 temp
= (unsigned char *) alloca (len1
);
2560 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2561 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2562 bcopy (start1_addr
, temp
, len1
);
2563 bcopy (start2_addr
, start1_addr
, len2
);
2564 bcopy (temp
, start1_addr
+ len2
, len1
);
2568 #ifdef USE_TEXT_PROPERTIES
2569 graft_intervals_into_buffer (tmp_interval1
, start1
+ len2
,
2570 len1
, current_buffer
, 0);
2571 graft_intervals_into_buffer (tmp_interval2
, start1
,
2572 len2
, current_buffer
, 0);
2573 #endif /* USE_TEXT_PROPERTIES */
2575 /* Non-adjacent regions, because end1 != start2, bleagh... */
2579 /* Regions are same size, though, how nice. */
2581 modify_region (current_buffer
, start1
, end1
);
2582 modify_region (current_buffer
, start2
, end2
);
2583 record_change (start1
, len1
);
2584 record_change (start2
, len2
);
2585 #ifdef USE_TEXT_PROPERTIES
2586 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2587 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2588 Fset_text_properties (make_number (start1
), make_number (end1
),
2590 Fset_text_properties (make_number (start2
), make_number (end2
),
2592 #endif /* USE_TEXT_PROPERTIES */
2595 temp
= (unsigned char *) xmalloc (len1
);
2597 temp
= (unsigned char *) alloca (len1
);
2598 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2599 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2600 bcopy (start1_addr
, temp
, len1
);
2601 bcopy (start2_addr
, start1_addr
, len2
);
2602 bcopy (temp
, start2_addr
, len1
);
2605 #ifdef USE_TEXT_PROPERTIES
2606 graft_intervals_into_buffer (tmp_interval1
, start2
,
2607 len1
, current_buffer
, 0);
2608 graft_intervals_into_buffer (tmp_interval2
, start1
,
2609 len2
, current_buffer
, 0);
2610 #endif /* USE_TEXT_PROPERTIES */
2613 else if (len1
< len2
) /* Second region larger than first */
2614 /* Non-adjacent & unequal size, area between must also be shifted. */
2616 len_mid
= start2
- end1
;
2617 modify_region (current_buffer
, start1
, end2
);
2618 record_change (start1
, (end2
- start1
));
2619 #ifdef USE_TEXT_PROPERTIES
2620 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2621 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2622 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2623 Fset_text_properties (make_number (start1
), make_number (end2
),
2625 #endif /* USE_TEXT_PROPERTIES */
2627 /* holds region 2 */
2629 temp
= (unsigned char *) xmalloc (len2
);
2631 temp
= (unsigned char *) alloca (len2
);
2632 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2633 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2634 bcopy (start2_addr
, temp
, len2
);
2635 bcopy (start1_addr
, start1_addr
+ len_mid
+ len2
, len1
);
2636 safe_bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2637 bcopy (temp
, start1_addr
, len2
);
2640 #ifdef USE_TEXT_PROPERTIES
2641 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2642 len1
, current_buffer
, 0);
2643 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2644 len_mid
, current_buffer
, 0);
2645 graft_intervals_into_buffer (tmp_interval2
, start1
,
2646 len2
, current_buffer
, 0);
2647 #endif /* USE_TEXT_PROPERTIES */
2650 /* Second region smaller than first. */
2652 len_mid
= start2
- end1
;
2653 record_change (start1
, (end2
- start1
));
2654 modify_region (current_buffer
, start1
, end2
);
2656 #ifdef USE_TEXT_PROPERTIES
2657 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2658 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2659 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2660 Fset_text_properties (make_number (start1
), make_number (end2
),
2662 #endif /* USE_TEXT_PROPERTIES */
2664 /* holds region 1 */
2666 temp
= (unsigned char *) xmalloc (len1
);
2668 temp
= (unsigned char *) alloca (len1
);
2669 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2670 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2671 bcopy (start1_addr
, temp
, len1
);
2672 bcopy (start2_addr
, start1_addr
, len2
);
2673 bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2674 bcopy (temp
, start1_addr
+ len2
+ len_mid
, len1
);
2677 #ifdef USE_TEXT_PROPERTIES
2678 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2679 len1
, current_buffer
, 0);
2680 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2681 len_mid
, current_buffer
, 0);
2682 graft_intervals_into_buffer (tmp_interval2
, start1
,
2683 len2
, current_buffer
, 0);
2684 #endif /* USE_TEXT_PROPERTIES */
2688 /* todo: this will be slow, because for every transposition, we
2689 traverse the whole friggin marker list. Possible solutions:
2690 somehow get a list of *all* the markers across multiple
2691 transpositions and do it all in one swell phoop. Or maybe modify
2692 Emacs' marker code to keep an ordered list or tree. This might
2693 be nicer, and more beneficial in the long run, but would be a
2694 bunch of work. Plus the way they're arranged now is nice. */
2695 if (NILP (leave_markers
))
2697 transpose_markers (start1
, end1
, start2
, end2
);
2698 fix_overlays_in_range (start1
, end2
);
2710 Qbuffer_access_fontify_functions
2711 = intern ("buffer-access-fontify-functions");
2712 staticpro (&Qbuffer_access_fontify_functions
);
2714 DEFVAR_LISP ("buffer-access-fontify-functions",
2715 &Vbuffer_access_fontify_functions
,
2716 "List of functions called by `buffer-substring' to fontify if necessary.\n\
2717 Each function is called with two arguments which specify the range\n\
2718 of the buffer being accessed.");
2719 Vbuffer_access_fontify_functions
= Qnil
;
2723 extern Lisp_Object Vprin1_to_string_buffer
;
2724 obuf
= Fcurrent_buffer ();
2725 /* Do this here, because init_buffer_once is too early--it won't work. */
2726 Fset_buffer (Vprin1_to_string_buffer
);
2727 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
2728 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
2733 DEFVAR_LISP ("buffer-access-fontified-property",
2734 &Vbuffer_access_fontified_property
,
2735 "Property which (if non-nil) indicates text has been fontified.\n\
2736 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
2737 functions if all the text being accessed has this property.");
2738 Vbuffer_access_fontified_property
= Qnil
;
2740 DEFVAR_LISP ("system-name", &Vsystem_name
,
2741 "The name of the machine Emacs is running on.");
2743 DEFVAR_LISP ("user-full-name", &Vuser_full_name
,
2744 "The full name of the user logged in.");
2746 DEFVAR_LISP ("user-login-name", &Vuser_login_name
,
2747 "The user's name, taken from environment variables if possible.");
2749 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name
,
2750 "The user's name, based upon the real uid only.");
2752 defsubr (&Schar_equal
);
2753 defsubr (&Sgoto_char
);
2754 defsubr (&Sstring_to_char
);
2755 defsubr (&Schar_to_string
);
2757 defsubr (&Sbuffer_substring
);
2758 defsubr (&Sbuffer_substring_no_properties
);
2759 defsubr (&Sbuffer_string
);
2761 defsubr (&Spoint_marker
);
2762 defsubr (&Smark_marker
);
2764 defsubr (&Sregion_beginning
);
2765 defsubr (&Sregion_end
);
2766 /* defsubr (&Smark); */
2767 /* defsubr (&Sset_mark); */
2768 defsubr (&Ssave_excursion
);
2769 defsubr (&Ssave_current_buffer
);
2771 defsubr (&Sbufsize
);
2772 defsubr (&Spoint_max
);
2773 defsubr (&Spoint_min
);
2774 defsubr (&Spoint_min_marker
);
2775 defsubr (&Spoint_max_marker
);
2777 defsubr (&Sline_beginning_position
);
2778 defsubr (&Sline_end_position
);
2784 defsubr (&Sfollowing_char
);
2785 defsubr (&Sprevious_char
);
2786 defsubr (&Schar_after
);
2787 defsubr (&Schar_before
);
2789 defsubr (&Sinsert_before_markers
);
2790 defsubr (&Sinsert_and_inherit
);
2791 defsubr (&Sinsert_and_inherit_before_markers
);
2792 defsubr (&Sinsert_char
);
2794 defsubr (&Suser_login_name
);
2795 defsubr (&Suser_real_login_name
);
2796 defsubr (&Suser_uid
);
2797 defsubr (&Suser_real_uid
);
2798 defsubr (&Suser_full_name
);
2799 defsubr (&Semacs_pid
);
2800 defsubr (&Scurrent_time
);
2801 defsubr (&Sformat_time_string
);
2802 defsubr (&Sdecode_time
);
2803 defsubr (&Sencode_time
);
2804 defsubr (&Scurrent_time_string
);
2805 defsubr (&Scurrent_time_zone
);
2806 defsubr (&Sset_time_zone_rule
);
2807 defsubr (&Ssystem_name
);
2808 defsubr (&Smessage
);
2809 defsubr (&Smessage_box
);
2810 defsubr (&Smessage_or_box
);
2811 defsubr (&Scurrent_message
);
2814 defsubr (&Sinsert_buffer_substring
);
2815 defsubr (&Scompare_buffer_substrings
);
2816 defsubr (&Ssubst_char_in_region
);
2817 defsubr (&Stranslate_region
);
2818 defsubr (&Sdelete_region
);
2820 defsubr (&Snarrow_to_region
);
2821 defsubr (&Ssave_restriction
);
2822 defsubr (&Stranspose_regions
);