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))
43 extern char **environ
;
44 extern Lisp_Object
make_time ();
45 extern void insert_from_buffer ();
46 static int tm_diff ();
47 static void update_buffer_properties ();
48 void set_time_zone_rule ();
50 Lisp_Object Vbuffer_access_fontify_functions
;
51 Lisp_Object Qbuffer_access_fontify_functions
;
52 Lisp_Object Vbuffer_access_fontified_property
;
54 Lisp_Object
Fuser_full_name ();
56 /* Some static data, and a function to initialize it for each run */
58 Lisp_Object Vsystem_name
;
59 Lisp_Object Vuser_real_login_name
; /* login name of current user ID */
60 Lisp_Object Vuser_full_name
; /* full name of current user */
61 Lisp_Object Vuser_login_name
; /* user name from LOGNAME or USER */
67 register unsigned char *p
, *q
, *r
;
68 struct passwd
*pw
; /* password entry for the current user */
71 /* Set up system_name even when dumping. */
75 /* Don't bother with this on initial start when just dumping out */
78 #endif /* not CANNOT_DUMP */
80 pw
= (struct passwd
*) getpwuid (getuid ());
82 /* We let the real user name default to "root" because that's quite
83 accurate on MSDOG and because it lets Emacs find the init file.
84 (The DVX libraries override the Djgpp libraries here.) */
85 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "root");
87 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "unknown");
90 /* Get the effective user name, by consulting environment variables,
91 or the effective uid if those are unset. */
92 user_name
= (char *) getenv ("LOGNAME");
95 user_name
= (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
97 user_name
= (char *) getenv ("USER");
98 #endif /* WINDOWSNT */
101 pw
= (struct passwd
*) getpwuid (geteuid ());
102 user_name
= (char *) (pw
? pw
->pw_name
: "unknown");
104 Vuser_login_name
= build_string (user_name
);
106 /* If the user name claimed in the environment vars differs from
107 the real uid, use the claimed name to find the full name. */
108 tem
= Fstring_equal (Vuser_login_name
, Vuser_real_login_name
);
109 Vuser_full_name
= Fuser_full_name (NILP (tem
)? make_number (geteuid())
112 p
= (unsigned char *) getenv ("NAME");
114 Vuser_full_name
= build_string (p
);
115 else if (NILP (Vuser_full_name
))
116 Vuser_full_name
= build_string ("unknown");
119 DEFUN ("char-to-string", Fchar_to_string
, Schar_to_string
, 1, 1, 0,
120 "Convert arg CHAR to a string containing multi-byte form of that character.")
122 Lisp_Object character
;
125 char workbuf
[4], *str
;
127 CHECK_NUMBER (character
, 0);
129 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
130 return make_string (str
, len
);
133 DEFUN ("string-to-char", Fstring_to_char
, Sstring_to_char
, 1, 1, 0,
134 "Convert arg STRING to a character, the first character of that string.\n\
135 A multibyte character is handled correctly.")
137 register Lisp_Object string
;
139 register Lisp_Object val
;
140 register struct Lisp_String
*p
;
141 CHECK_STRING (string
, 0);
142 p
= XSTRING (string
);
144 XSETFASTINT (val
, STRING_CHAR (p
->data
, p
->size
));
146 XSETFASTINT (val
, 0);
150 DEFUN ("sref", Fsref
, Ssref
, 2, 2, 0,
151 "Return the character in STRING at INDEX. INDEX starts at 0.\n\
152 A multibyte character is handled correctly.\n\
153 INDEX not pointing at character boundary is an error.")
155 Lisp_Object str
, idx
;
157 register int idxval
, len
;
158 register unsigned char *p
;
159 register Lisp_Object val
;
161 CHECK_STRING (str
, 0);
162 CHECK_NUMBER (idx
, 1);
164 if (idxval
< 0 || idxval
>= (len
= XVECTOR (str
)->size
))
165 args_out_of_range (str
, idx
);
166 p
= XSTRING (str
)->data
+ idxval
;
167 if (!CHAR_HEAD_P (p
))
168 error ("Not character boundary");
170 len
= XSTRING (str
)->size
- idxval
;
171 XSETFASTINT (val
, STRING_CHAR (p
, len
));
180 register Lisp_Object mark
;
181 mark
= Fmake_marker ();
182 Fset_marker (mark
, make_number (val
), Qnil
);
186 DEFUN ("point", Fpoint
, Spoint
, 0, 0, 0,
187 "Return value of point, as an integer.\n\
188 Beginning of buffer is position (point-min)")
192 XSETFASTINT (temp
, PT
);
196 DEFUN ("point-marker", Fpoint_marker
, Spoint_marker
, 0, 0, 0,
197 "Return value of point, as a marker object.")
200 return buildmark (PT
);
204 clip_to_bounds (lower
, num
, upper
)
205 int lower
, num
, upper
;
209 else if (num
> upper
)
215 DEFUN ("goto-char", Fgoto_char
, Sgoto_char
, 1, 1, "NGoto char: ",
216 "Set point to POSITION, a number or marker.\n\
217 Beginning of buffer is position (point-min), end is (point-max).\n\
218 If the position is in the middle of a multibyte form,\n\
219 the actual point is set at the head of the multibyte form\n\
220 except in the case that `enable-multibyte-characters' is nil.")
222 register Lisp_Object position
;
227 CHECK_NUMBER_COERCE_MARKER (position
, 0);
229 pos
= clip_to_bounds (BEGV
, XINT (position
), ZV
);
230 /* If POS is in a middle of multi-byte form (i.e. *P >= 0xA0), we
231 must decrement POS until it points the head of the multi-byte
233 if (!NILP (current_buffer
->enable_multibyte_characters
)
234 && *(p
= POS_ADDR (pos
)) >= 0xA0
237 /* Since a multi-byte form does not contain the gap, POS should
238 not stride over the gap while it is being decreased. So, we
239 set the limit as below. */
240 unsigned char *p_min
= pos
< GPT
? BEG_ADDR
: GAP_END_ADDR
;
241 unsigned int saved_pos
= pos
;
245 } while (p
> p_min
&& *p
>= 0xA0);
247 /* This was an invalid multi-byte form. */
249 XSETFASTINT (position
, pos
);
256 region_limit (beginningp
)
259 extern Lisp_Object Vmark_even_if_inactive
; /* Defined in callint.c. */
260 register Lisp_Object m
;
261 if (!NILP (Vtransient_mark_mode
) && NILP (Vmark_even_if_inactive
)
262 && NILP (current_buffer
->mark_active
))
263 Fsignal (Qmark_inactive
, Qnil
);
264 m
= Fmarker_position (current_buffer
->mark
);
265 if (NILP (m
)) error ("There is no region now");
266 if ((PT
< XFASTINT (m
)) == beginningp
)
267 return (make_number (PT
));
272 DEFUN ("region-beginning", Fregion_beginning
, Sregion_beginning
, 0, 0, 0,
273 "Return position of beginning of region, as an integer.")
276 return (region_limit (1));
279 DEFUN ("region-end", Fregion_end
, Sregion_end
, 0, 0, 0,
280 "Return position of end of region, as an integer.")
283 return (region_limit (0));
286 DEFUN ("mark-marker", Fmark_marker
, Smark_marker
, 0, 0, 0,
287 "Return this buffer's mark, as a marker object.\n\
288 Watch out! Moving this marker changes the mark position.\n\
289 If you set the marker not to point anywhere, the buffer will have no mark.")
292 return current_buffer
->mark
;
295 DEFUN ("line-beginning-position", Fline_beginning_position
, Sline_beginning_position
,
297 "Return the character position of the first character on the current line.\n\
298 With argument N not nil or 1, move forward N - 1 lines first.\n\
299 If scan reaches end of buffer, return that position.\n\
300 This function does not move point.")
304 register int orig
, end
;
312 Fforward_line (make_number (XINT (n
) - 1));
316 return make_number (end
);
319 DEFUN ("line-end-position", Fline_end_position
, Sline_end_position
,
321 "Return the character position of the last character on the current line.\n\
322 With argument N not nil or 1, move forward N - 1 lines first.\n\
323 If scan reaches end of buffer, return that position.\n\
324 This function does not move point.")
333 return make_number (find_before_next_newline
334 (PT
, 0, XINT (n
) - (XINT (n
) <= 0)));
338 save_excursion_save ()
340 register int visible
= (XBUFFER (XWINDOW (selected_window
)->buffer
)
343 return Fcons (Fpoint_marker (),
344 Fcons (Fcopy_marker (current_buffer
->mark
, Qnil
),
345 Fcons (visible
? Qt
: Qnil
,
346 current_buffer
->mark_active
)));
350 save_excursion_restore (info
)
353 Lisp_Object tem
, tem1
, omark
, nmark
;
354 struct gcpro gcpro1
, gcpro2
, gcpro3
;
356 tem
= Fmarker_buffer (Fcar (info
));
357 /* If buffer being returned to is now deleted, avoid error */
358 /* Otherwise could get error here while unwinding to top level
360 /* In that case, Fmarker_buffer returns nil now. */
364 omark
= nmark
= Qnil
;
365 GCPRO3 (info
, omark
, nmark
);
370 unchain_marker (tem
);
371 tem
= Fcar (Fcdr (info
));
372 omark
= Fmarker_position (current_buffer
->mark
);
373 Fset_marker (current_buffer
->mark
, tem
, Fcurrent_buffer ());
374 nmark
= Fmarker_position (tem
);
375 unchain_marker (tem
);
376 tem
= Fcdr (Fcdr (info
));
377 #if 0 /* We used to make the current buffer visible in the selected window
378 if that was true previously. That avoids some anomalies.
379 But it creates others, and it wasn't documented, and it is simpler
380 and cleaner never to alter the window/buffer connections. */
383 && current_buffer
!= XBUFFER (XWINDOW (selected_window
)->buffer
))
384 Fswitch_to_buffer (Fcurrent_buffer (), Qnil
);
387 tem1
= current_buffer
->mark_active
;
388 current_buffer
->mark_active
= Fcdr (tem
);
389 if (!NILP (Vrun_hooks
))
391 /* If mark is active now, and either was not active
392 or was at a different place, run the activate hook. */
393 if (! NILP (current_buffer
->mark_active
))
395 if (! EQ (omark
, nmark
))
396 call1 (Vrun_hooks
, intern ("activate-mark-hook"));
398 /* If mark has ceased to be active, run deactivate hook. */
399 else if (! NILP (tem1
))
400 call1 (Vrun_hooks
, intern ("deactivate-mark-hook"));
406 DEFUN ("save-excursion", Fsave_excursion
, Ssave_excursion
, 0, UNEVALLED
, 0,
407 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
408 Executes BODY just like `progn'.\n\
409 The values of point, mark and the current buffer are restored\n\
410 even in case of abnormal exit (throw or error).\n\
411 The state of activation of the mark is also restored.")
415 register Lisp_Object val
;
416 int count
= specpdl_ptr
- specpdl
;
418 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
421 return unbind_to (count
, val
);
424 DEFUN ("save-current-buffer", Fsave_current_buffer
, Ssave_current_buffer
, 0, UNEVALLED
, 0,
425 "Save the current buffer; execute BODY; restore the current buffer.\n\
426 Executes BODY just like `progn'.")
430 register Lisp_Object val
;
431 int count
= specpdl_ptr
- specpdl
;
433 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
436 return unbind_to (count
, val
);
439 DEFUN ("buffer-size", Fbufsize
, Sbufsize
, 0, 0, 0,
440 "Return the number of characters in the current buffer.")
444 XSETFASTINT (temp
, Z
- BEG
);
448 DEFUN ("point-min", Fpoint_min
, Spoint_min
, 0, 0, 0,
449 "Return the minimum permissible value of point in the current buffer.\n\
450 This is 1, unless narrowing (a buffer restriction) is in effect.")
454 XSETFASTINT (temp
, BEGV
);
458 DEFUN ("point-min-marker", Fpoint_min_marker
, Spoint_min_marker
, 0, 0, 0,
459 "Return a marker to the minimum permissible value of point in this buffer.\n\
460 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
463 return buildmark (BEGV
);
466 DEFUN ("point-max", Fpoint_max
, Spoint_max
, 0, 0, 0,
467 "Return the maximum permissible value of point in the current buffer.\n\
468 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
469 is in effect, in which case it is less.")
473 XSETFASTINT (temp
, ZV
);
477 DEFUN ("point-max-marker", Fpoint_max_marker
, Spoint_max_marker
, 0, 0, 0,
478 "Return a marker to the maximum permissible value of point in this buffer.\n\
479 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
480 is in effect, in which case it is less.")
483 return buildmark (ZV
);
486 DEFUN ("following-char", Ffollowing_char
, Sfollowing_char
, 0, 0, 0,
487 "Return the character following point, as a number.\n\
488 At the end of the buffer or accessible region, return 0.\n\
489 If `enable-multibyte-characters' is nil or point is not\n\
490 at character boundary, multibyte form is ignored,\n\
491 and only one byte following point is returned as a character.")
496 XSETFASTINT (temp
, 0);
498 XSETFASTINT (temp
, FETCH_CHAR (PT
));
502 DEFUN ("preceding-char", Fprevious_char
, Sprevious_char
, 0, 0, 0,
503 "Return the character preceding point, as a number.\n\
504 At the beginning of the buffer or accessible region, return 0.\n\
505 If `enable-multibyte-characters' is nil or point is not\n\
506 at character boundary, multi-byte form is ignored,\n\
507 and only one byte preceding point is returned as a character.")
512 XSETFASTINT (temp
, 0);
513 else if (!NILP (current_buffer
->enable_multibyte_characters
))
517 XSETFASTINT (temp
, FETCH_CHAR (pos
));
520 XSETFASTINT (temp
, FETCH_BYTE (point
- 1));
524 DEFUN ("bobp", Fbobp
, Sbobp
, 0, 0, 0,
525 "Return T if point is at the beginning of the buffer.\n\
526 If the buffer is narrowed, this means the beginning of the narrowed part.")
534 DEFUN ("eobp", Feobp
, Seobp
, 0, 0, 0,
535 "Return T if point is at the end of the buffer.\n\
536 If the buffer is narrowed, this means the end of the narrowed part.")
544 DEFUN ("bolp", Fbolp
, Sbolp
, 0, 0, 0,
545 "Return T if point is at the beginning of a line.")
548 if (PT
== BEGV
|| FETCH_BYTE (PT
- 1) == '\n')
553 DEFUN ("eolp", Feolp
, Seolp
, 0, 0, 0,
554 "Return T if point is at the end of a line.\n\
555 `End of a line' includes point being at the end of the buffer.")
558 if (PT
== ZV
|| FETCH_BYTE (PT
) == '\n')
563 DEFUN ("char-after", Fchar_after
, Schar_after
, 1, 1, 0,
564 "Return character in current buffer at position POS.\n\
565 POS is an integer or a buffer pointer.\n\
566 If POS is out of range, the value is nil.\n\
567 If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
568 multi-byte form is ignored, and only one byte at POS\n\
569 is returned as a character.")
573 register Lisp_Object val
;
576 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
579 if (n
< BEGV
|| n
>= ZV
) return Qnil
;
581 XSETFASTINT (val
, FETCH_CHAR (n
));
585 DEFUN ("char-before", Fchar_before
, Schar_before
, 1, 1, 0,
586 "Return character in current buffer preceding position POS.\n\
587 POS is an integer or a buffer pointer.\n\
588 If POS is out of range, the value is nil.\n\
589 If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
590 multi-byte form is ignored, and only one byte preceding POS\n\
591 is returned as a character.")
595 register Lisp_Object val
;
598 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
601 if (n
<= BEGV
|| n
> ZV
) return Qnil
;
603 if (!NILP (current_buffer
->enable_multibyte_characters
))
606 XSETFASTINT (val
, FETCH_CHAR (pos
));
611 XSETFASTINT (val
, FETCH_BYTE (pos
));
616 DEFUN ("user-login-name", Fuser_login_name
, Suser_login_name
, 0, 1, 0,
617 "Return the name under which the user logged in, as a string.\n\
618 This is based on the effective uid, not the real uid.\n\
619 Also, if the environment variable LOGNAME or USER is set,\n\
620 that determines the value of this function.\n\n\
621 If optional argument UID is an integer, return the login name of the user\n\
622 with that uid, or nil if there is no such user.")
628 /* Set up the user name info if we didn't do it before.
629 (That can happen if Emacs is dumpable
630 but you decide to run `temacs -l loadup' and not dump. */
631 if (INTEGERP (Vuser_login_name
))
635 return Vuser_login_name
;
637 CHECK_NUMBER (uid
, 0);
638 pw
= (struct passwd
*) getpwuid (XINT (uid
));
639 return (pw
? build_string (pw
->pw_name
) : Qnil
);
642 DEFUN ("user-real-login-name", Fuser_real_login_name
, Suser_real_login_name
,
644 "Return the name of the user's real uid, as a string.\n\
645 This ignores the environment variables LOGNAME and USER, so it differs from\n\
646 `user-login-name' when running under `su'.")
649 /* Set up the user name info if we didn't do it before.
650 (That can happen if Emacs is dumpable
651 but you decide to run `temacs -l loadup' and not dump. */
652 if (INTEGERP (Vuser_login_name
))
654 return Vuser_real_login_name
;
657 DEFUN ("user-uid", Fuser_uid
, Suser_uid
, 0, 0, 0,
658 "Return the effective uid of Emacs, as an integer.")
661 return make_number (geteuid ());
664 DEFUN ("user-real-uid", Fuser_real_uid
, Suser_real_uid
, 0, 0, 0,
665 "Return the real uid of Emacs, as an integer.")
668 return make_number (getuid ());
671 DEFUN ("user-full-name", Fuser_full_name
, Suser_full_name
, 0, 1, 0,
672 "Return the full name of the user logged in, as a string.\n\
673 If optional argument UID is an integer, return the full name of the user\n\
674 with that uid, or \"unknown\" if there is no such user.\n\
675 If UID is a string, return the full name of the user with that login\n\
676 name, or \"unknown\" if no such user could be found.")
681 register char *p
, *q
;
682 extern char *index ();
686 return Vuser_full_name
;
687 else if (NUMBERP (uid
))
688 pw
= (struct passwd
*) getpwuid (XINT (uid
));
689 else if (STRINGP (uid
))
690 pw
= (struct passwd
*) getpwnam (XSTRING (uid
)->data
);
692 error ("Invalid UID specification");
697 p
= (unsigned char *) USER_FULL_NAME
;
698 /* Chop off everything after the first comma. */
699 q
= (unsigned char *) index (p
, ',');
700 full
= make_string (p
, q
? q
- p
: strlen (p
));
702 #ifdef AMPERSAND_FULL_NAME
703 p
= XSTRING (full
)->data
;
704 q
= (unsigned char *) index (p
, '&');
705 /* Substitute the login name for the &, upcasing the first character. */
711 login
= Fuser_login_name (make_number (pw
->pw_uid
));
712 r
= (unsigned char *) alloca (strlen (p
) + XSTRING (login
)->size
+ 1);
715 strcat (r
, XSTRING (login
)->data
);
716 r
[q
- p
] = UPCASE (r
[q
- p
]);
718 full
= build_string (r
);
720 #endif /* AMPERSAND_FULL_NAME */
725 DEFUN ("system-name", Fsystem_name
, Ssystem_name
, 0, 0, 0,
726 "Return the name of the machine you are running on, as a string.")
732 /* For the benefit of callers who don't want to include lisp.h */
736 return (char *) XSTRING (Vsystem_name
)->data
;
739 DEFUN ("emacs-pid", Femacs_pid
, Semacs_pid
, 0, 0, 0,
740 "Return the process ID of Emacs, as an integer.")
743 return make_number (getpid ());
746 DEFUN ("current-time", Fcurrent_time
, Scurrent_time
, 0, 0, 0,
747 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
748 The time is returned as a list of three integers. The first has the\n\
749 most significant 16 bits of the seconds, while the second has the\n\
750 least significant 16 bits. The third integer gives the microsecond\n\
753 The microsecond count is zero on systems that do not provide\n\
754 resolution finer than a second.")
758 Lisp_Object result
[3];
761 XSETINT (result
[0], (EMACS_SECS (t
) >> 16) & 0xffff);
762 XSETINT (result
[1], (EMACS_SECS (t
) >> 0) & 0xffff);
763 XSETINT (result
[2], EMACS_USECS (t
));
765 return Flist (3, result
);
770 lisp_time_argument (specified_time
, result
)
771 Lisp_Object specified_time
;
774 if (NILP (specified_time
))
775 return time (result
) != -1;
778 Lisp_Object high
, low
;
779 high
= Fcar (specified_time
);
780 CHECK_NUMBER (high
, 0);
781 low
= Fcdr (specified_time
);
784 CHECK_NUMBER (low
, 0);
785 *result
= (XINT (high
) << 16) + (XINT (low
) & 0xffff);
786 return *result
>> 16 == XINT (high
);
790 DEFUN ("format-time-string", Fformat_time_string
, Sformat_time_string
, 1, 3, 0,
791 "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
792 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
793 `current-time' or `file-attributes'.\n\
794 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
795 as Universal Time; nil means describe TIME in the local time zone.\n\
796 The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
797 by text that describes the specified date and time in TIME:\n\
799 %Y is the year, %y within the century, %C the century.\n\
800 %G is the year corresponding to the ISO week, %g within the century.\n\
801 %m is the numeric month, %b and %h the abbreviated name, %B the full name.\n\
802 %d is the day of the month, zero-padded, %e is blank-padded.\n\
803 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\
804 %a is the abbreviated name of the day of week, %A the full name.\n\
805 %U is the week number starting on Sunday, %W starting on Monday,\n\
806 %V according to ISO 8601.\n\
807 %j is the day of the year.\n\
809 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\
810 only blank-padded, %l is like %I blank-padded.\n\
814 %Z is the time zone name, %z is the numeric form.\n\
815 %s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\
817 %c is the locale's date and time format.\n\
818 %x is the locale's \"preferred\" date format.\n\
819 %D is like \"%m/%d/%y\".\n\
821 %R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\
822 %X is the locale's \"preferred\" time format.\n\
824 Finally, %n is like \n, %t is like \t, %% is a literal %.\n\
826 Certain flags and modifiers are available with some format controls.
827 The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
828 but padded with blanks; %-X is like %X, but without padding.\n\
829 %NX (where N stands for an integer) is like %X,\n\
830 but takes up at least N (a number) positions.\n\
831 The modifiers are `E' and `O'. For certain characters X,\n\
832 %EX is a locale's alternative version of %X;\n\
833 %OX is like %X, but uses the locale's number symbols.\n\
835 For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
836 (format_string
, time
, universal
)
837 Lisp_Object format_string
, time
, universal
;
842 CHECK_STRING (format_string
, 1);
844 if (! lisp_time_argument (time
, &value
))
845 error ("Invalid time specification");
847 /* This is probably enough. */
848 size
= XSTRING (format_string
)->size
* 6 + 50;
852 char *buf
= (char *) alloca (size
+ 1);
855 result
= emacs_strftime (buf
, size
, XSTRING (format_string
)->data
,
856 (NILP (universal
) ? localtime (&value
)
858 if (result
> 0 && result
< size
)
859 return build_string (buf
);
861 error ("Invalid time format specification");
863 /* If buffer was too small, make it bigger and try again. */
864 result
= emacs_strftime (buf
, 0, XSTRING (format_string
)->data
,
865 (NILP (universal
) ? localtime (&value
)
871 DEFUN ("decode-time", Fdecode_time
, Sdecode_time
, 0, 1, 0,
872 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
873 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
874 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
875 to use the current time. The list has the following nine members:\n\
876 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
877 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
878 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
879 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
880 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
881 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
882 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
883 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
885 Lisp_Object specified_time
;
889 struct tm
*decoded_time
;
890 Lisp_Object list_args
[9];
892 if (! lisp_time_argument (specified_time
, &time_spec
))
893 error ("Invalid time specification");
895 decoded_time
= localtime (&time_spec
);
896 XSETFASTINT (list_args
[0], decoded_time
->tm_sec
);
897 XSETFASTINT (list_args
[1], decoded_time
->tm_min
);
898 XSETFASTINT (list_args
[2], decoded_time
->tm_hour
);
899 XSETFASTINT (list_args
[3], decoded_time
->tm_mday
);
900 XSETFASTINT (list_args
[4], decoded_time
->tm_mon
+ 1);
901 XSETINT (list_args
[5], decoded_time
->tm_year
+ 1900);
902 XSETFASTINT (list_args
[6], decoded_time
->tm_wday
);
903 list_args
[7] = (decoded_time
->tm_isdst
)? Qt
: Qnil
;
905 /* Make a copy, in case gmtime modifies the struct. */
906 save_tm
= *decoded_time
;
907 decoded_time
= gmtime (&time_spec
);
908 if (decoded_time
== 0)
911 XSETINT (list_args
[8], tm_diff (&save_tm
, decoded_time
));
912 return Flist (9, list_args
);
915 DEFUN ("encode-time", Fencode_time
, Sencode_time
, 6, MANY
, 0,
916 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
917 This is the reverse operation of `decode-time', which see.\n\
918 ZONE defaults to the current time zone rule. This can\n\
919 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
920 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
921 applied without consideration for daylight savings time.\n\
923 You can pass more than 7 arguments; then the first six arguments\n\
924 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
925 The intervening arguments are ignored.\n\
926 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
928 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
929 for example, a DAY of 0 means the day preceding the given month.\n\
930 Year numbers less than 100 are treated just like other year numbers.\n\
931 If you want them to stand for years in this century, you must do that yourself.")
934 register Lisp_Object
*args
;
938 Lisp_Object zone
= (nargs
> 6 ? args
[nargs
- 1] : Qnil
);
940 CHECK_NUMBER (args
[0], 0); /* second */
941 CHECK_NUMBER (args
[1], 1); /* minute */
942 CHECK_NUMBER (args
[2], 2); /* hour */
943 CHECK_NUMBER (args
[3], 3); /* day */
944 CHECK_NUMBER (args
[4], 4); /* month */
945 CHECK_NUMBER (args
[5], 5); /* year */
947 tm
.tm_sec
= XINT (args
[0]);
948 tm
.tm_min
= XINT (args
[1]);
949 tm
.tm_hour
= XINT (args
[2]);
950 tm
.tm_mday
= XINT (args
[3]);
951 tm
.tm_mon
= XINT (args
[4]) - 1;
952 tm
.tm_year
= XINT (args
[5]) - 1900;
963 char **oldenv
= environ
, **newenv
;
967 else if (STRINGP (zone
))
968 tzstring
= (char *) XSTRING (zone
)->data
;
969 else if (INTEGERP (zone
))
971 int abszone
= abs (XINT (zone
));
972 sprintf (tzbuf
, "XXX%s%d:%02d:%02d", "-" + (XINT (zone
) < 0),
973 abszone
/ (60*60), (abszone
/60) % 60, abszone
% 60);
977 error ("Invalid time zone specification");
979 /* Set TZ before calling mktime; merely adjusting mktime's returned
980 value doesn't suffice, since that would mishandle leap seconds. */
981 set_time_zone_rule (tzstring
);
985 /* Restore TZ to previous value. */
989 #ifdef LOCALTIME_CACHE
994 if (time
== (time_t) -1)
995 error ("Specified time is not representable");
997 return make_time (time
);
1000 DEFUN ("current-time-string", Fcurrent_time_string
, Scurrent_time_string
, 0, 1, 0,
1001 "Return the current time, as a human-readable string.\n\
1002 Programs can use this function to decode a time,\n\
1003 since the number of columns in each field is fixed.\n\
1004 The format is `Sun Sep 16 01:03:52 1973'.\n\
1005 However, see also the functions `decode-time' and `format-time-string'\n\
1006 which provide a much more powerful and general facility.\n\
1008 If an argument is given, it specifies a time to format\n\
1009 instead of the current time. The argument should have the form:\n\
1012 (HIGH LOW . IGNORED).\n\
1013 Thus, you can use times obtained from `current-time'\n\
1014 and from `file-attributes'.")
1016 Lisp_Object specified_time
;
1022 if (! lisp_time_argument (specified_time
, &value
))
1024 tem
= (char *) ctime (&value
);
1026 strncpy (buf
, tem
, 24);
1029 return build_string (buf
);
1032 #define TM_YEAR_BASE 1900
1034 /* Yield A - B, measured in seconds.
1035 This function is copied from the GNU C Library. */
1040 /* Compute intervening leap days correctly even if year is negative.
1041 Take care to avoid int overflow in leap day calculations,
1042 but it's OK to assume that A and B are close to each other. */
1043 int a4
= (a
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (a
->tm_year
& 3);
1044 int b4
= (b
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (b
->tm_year
& 3);
1045 int a100
= a4
/ 25 - (a4
% 25 < 0);
1046 int b100
= b4
/ 25 - (b4
% 25 < 0);
1047 int a400
= a100
>> 2;
1048 int b400
= b100
>> 2;
1049 int intervening_leap_days
= (a4
- b4
) - (a100
- b100
) + (a400
- b400
);
1050 int years
= a
->tm_year
- b
->tm_year
;
1051 int days
= (365 * years
+ intervening_leap_days
1052 + (a
->tm_yday
- b
->tm_yday
));
1053 return (60 * (60 * (24 * days
+ (a
->tm_hour
- b
->tm_hour
))
1054 + (a
->tm_min
- b
->tm_min
))
1055 + (a
->tm_sec
- b
->tm_sec
));
1058 DEFUN ("current-time-zone", Fcurrent_time_zone
, Scurrent_time_zone
, 0, 1, 0,
1059 "Return the offset and name for the local time zone.\n\
1060 This returns a list of the form (OFFSET NAME).\n\
1061 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1062 A negative value means west of Greenwich.\n\
1063 NAME is a string giving the name of the time zone.\n\
1064 If an argument is given, it specifies when the time zone offset is determined\n\
1065 instead of using the current time. The argument should have the form:\n\
1068 (HIGH LOW . IGNORED).\n\
1069 Thus, you can use times obtained from `current-time'\n\
1070 and from `file-attributes'.\n\
1072 Some operating systems cannot provide all this information to Emacs;\n\
1073 in this case, `current-time-zone' returns a list containing nil for\n\
1074 the data it can't find.")
1076 Lisp_Object specified_time
;
1081 if (lisp_time_argument (specified_time
, &value
)
1082 && (t
= gmtime (&value
)) != 0)
1088 gmt
= *t
; /* Make a copy, in case localtime modifies *t. */
1089 t
= localtime (&value
);
1090 offset
= tm_diff (t
, &gmt
);
1094 s
= (char *)t
->tm_zone
;
1095 #else /* not HAVE_TM_ZONE */
1097 if (t
->tm_isdst
== 0 || t
->tm_isdst
== 1)
1098 s
= tzname
[t
->tm_isdst
];
1100 #endif /* not HAVE_TM_ZONE */
1103 /* No local time zone name is available; use "+-NNNN" instead. */
1104 int am
= (offset
< 0 ? -offset
: offset
) / 60;
1105 sprintf (buf
, "%c%02d%02d", (offset
< 0 ? '-' : '+'), am
/60, am
%60);
1108 return Fcons (make_number (offset
), Fcons (build_string (s
), Qnil
));
1111 return Fmake_list (2, Qnil
);
1114 /* This holds the value of `environ' produced by the previous
1115 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1116 has never been called. */
1117 static char **environbuf
;
1119 DEFUN ("set-time-zone-rule", Fset_time_zone_rule
, Sset_time_zone_rule
, 1, 1, 0,
1120 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
1121 If TZ is nil, use implementation-defined default time zone information.\n\
1122 If TZ is t, use Universal Time.")
1134 CHECK_STRING (tz
, 0);
1135 tzstring
= (char *) XSTRING (tz
)->data
;
1138 set_time_zone_rule (tzstring
);
1141 environbuf
= environ
;
1146 #ifdef LOCALTIME_CACHE
1148 /* These two values are known to load tz files in buggy implementations,
1149 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1150 Their values shouldn't matter in non-buggy implementations.
1151 We don't use string literals for these strings,
1152 since if a string in the environment is in readonly
1153 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1154 See Sun bugs 1113095 and 1114114, ``Timezone routines
1155 improperly modify environment''. */
1157 static char set_time_zone_rule_tz1
[] = "TZ=GMT+0";
1158 static char set_time_zone_rule_tz2
[] = "TZ=GMT+1";
1162 /* Set the local time zone rule to TZSTRING.
1163 This allocates memory into `environ', which it is the caller's
1164 responsibility to free. */
1166 set_time_zone_rule (tzstring
)
1170 char **from
, **to
, **newenv
;
1172 /* Make the ENVIRON vector longer with room for TZSTRING. */
1173 for (from
= environ
; *from
; from
++)
1175 envptrs
= from
- environ
+ 2;
1176 newenv
= to
= (char **) xmalloc (envptrs
* sizeof (char *)
1177 + (tzstring
? strlen (tzstring
) + 4 : 0));
1179 /* Add TZSTRING to the end of environ, as a value for TZ. */
1182 char *t
= (char *) (to
+ envptrs
);
1184 strcat (t
, tzstring
);
1188 /* Copy the old environ vector elements into NEWENV,
1189 but don't copy the TZ variable.
1190 So we have only one definition of TZ, which came from TZSTRING. */
1191 for (from
= environ
; *from
; from
++)
1192 if (strncmp (*from
, "TZ=", 3) != 0)
1198 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1199 the TZ variable is stored. If we do not have a TZSTRING,
1200 TO points to the vector slot which has the terminating null. */
1202 #ifdef LOCALTIME_CACHE
1204 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1205 "US/Pacific" that loads a tz file, then changes to a value like
1206 "XXX0" that does not load a tz file, and then changes back to
1207 its original value, the last change is (incorrectly) ignored.
1208 Also, if TZ changes twice in succession to values that do
1209 not load a tz file, tzset can dump core (see Sun bug#1225179).
1210 The following code works around these bugs. */
1214 /* Temporarily set TZ to a value that loads a tz file
1215 and that differs from tzstring. */
1217 *newenv
= (strcmp (tzstring
, set_time_zone_rule_tz1
+ 3) == 0
1218 ? set_time_zone_rule_tz2
: set_time_zone_rule_tz1
);
1224 /* The implied tzstring is unknown, so temporarily set TZ to
1225 two different values that each load a tz file. */
1226 *to
= set_time_zone_rule_tz1
;
1229 *to
= set_time_zone_rule_tz2
;
1234 /* Now TZ has the desired value, and tzset can be invoked safely. */
1241 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1242 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1243 type of object is Lisp_String). INHERIT is passed to
1244 INSERT_FROM_STRING_FUNC as the last argument. */
1246 general_insert_function (insert_func
, insert_from_string_func
,
1247 inherit
, nargs
, args
)
1248 int (*insert_func
)(), (*insert_from_string_func
)();
1250 register Lisp_Object
*args
;
1252 register int argnum
;
1253 register Lisp_Object val
;
1255 for (argnum
= 0; argnum
< nargs
; argnum
++)
1261 char workbuf
[4], *str
;
1264 if (!NILP (current_buffer
->enable_multibyte_characters
))
1265 len
= CHAR_STRING (XFASTINT (val
), workbuf
, str
);
1267 workbuf
[0] = XINT (val
), str
= workbuf
, len
= 1;
1268 (*insert_func
) (str
, len
);
1270 else if (STRINGP (val
))
1272 (*insert_from_string_func
) (val
, 0, XSTRING (val
)->size
, inherit
);
1276 val
= wrong_type_argument (Qchar_or_string_p
, val
);
1290 /* Callers passing one argument to Finsert need not gcpro the
1291 argument "array", since the only element of the array will
1292 not be used after calling insert or insert_from_string, so
1293 we don't care if it gets trashed. */
1295 DEFUN ("insert", Finsert
, Sinsert
, 0, MANY
, 0,
1296 "Insert the arguments, either strings or characters, at point.\n\
1297 Point and before-insertion-markers move forward so that it ends up\n\
1298 after the inserted text.\n\
1299 Any other markers at the point of insertion remain before the text.")
1302 register Lisp_Object
*args
;
1304 general_insert_function (insert
, insert_from_string
, 0, nargs
, args
);
1308 DEFUN ("insert-and-inherit", Finsert_and_inherit
, Sinsert_and_inherit
,
1310 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1311 Point and before-insertion-markers move forward so that it ends up\n\
1312 after the inserted text.\n\
1313 Any other markers at the point of insertion remain before the text.")
1316 register Lisp_Object
*args
;
1318 general_insert_function (insert_and_inherit
, insert_from_string
, 1,
1323 DEFUN ("insert-before-markers", Finsert_before_markers
, Sinsert_before_markers
, 0, MANY
, 0,
1324 "Insert strings or characters at point, relocating markers after the text.\n\
1325 Point and before-insertion-markers move forward so that it ends up\n\
1326 after the inserted text.\n\
1327 Any other markers at the point of insertion also end up after the text.")
1330 register Lisp_Object
*args
;
1332 general_insert_function (insert_before_markers
,
1333 insert_from_string_before_markers
, 0,
1338 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers
,
1339 Sinsert_and_inherit_before_markers
, 0, MANY
, 0,
1340 "Insert text at point, relocating markers and inheriting properties.\n\
1341 Point moves forward so that it ends up after the inserted text.\n\
1342 Any other markers at the point of insertion also end up after the text.")
1345 register Lisp_Object
*args
;
1347 general_insert_function (insert_before_markers_and_inherit
,
1348 insert_from_string_before_markers
, 1,
1353 DEFUN ("insert-char", Finsert_char
, Sinsert_char
, 2, 3, 0,
1354 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1355 Point and before-insertion-markers are affected as in the function `insert'.\n\
1356 Both arguments are required.\n\
1357 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1358 from adjoining text, if those properties are sticky.")
1359 (character
, count
, inherit
)
1360 Lisp_Object character
, count
, inherit
;
1362 register unsigned char *string
;
1363 register int strlen
;
1366 unsigned char workbuf
[4], *str
;
1368 CHECK_NUMBER (character
, 0);
1369 CHECK_NUMBER (count
, 1);
1371 if (!NILP (current_buffer
->enable_multibyte_characters
))
1372 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
1374 workbuf
[0] = XFASTINT (character
), str
= workbuf
, len
= 1;
1375 n
= XINT (count
) * len
;
1378 strlen
= min (n
, 256 * len
);
1379 string
= (unsigned char *) alloca (strlen
);
1380 for (i
= 0; i
< strlen
; i
++)
1381 string
[i
] = str
[i
% len
];
1384 if (!NILP (inherit
))
1385 insert_and_inherit (string
, strlen
);
1387 insert (string
, strlen
);
1392 if (!NILP (inherit
))
1393 insert_and_inherit (string
, n
);
1401 /* Making strings from buffer contents. */
1403 /* Return a Lisp_String containing the text of the current buffer from
1404 START to END. If text properties are in use and the current buffer
1405 has properties in the range specified, the resulting string will also
1406 have them, if PROPS is nonzero.
1408 We don't want to use plain old make_string here, because it calls
1409 make_uninit_string, which can cause the buffer arena to be
1410 compacted. make_string has no way of knowing that the data has
1411 been moved, and thus copies the wrong data into the string. This
1412 doesn't effect most of the other users of make_string, so it should
1413 be left as is. But we should use this function when conjuring
1414 buffer substrings. */
1417 make_buffer_string (start
, end
, props
)
1421 Lisp_Object result
, tem
, tem1
;
1423 if (start
< GPT
&& GPT
< end
)
1426 result
= make_uninit_string (end
- start
);
1427 bcopy (POS_ADDR (start
), XSTRING (result
)->data
, end
- start
);
1429 /* If desired, update and copy the text properties. */
1430 #ifdef USE_TEXT_PROPERTIES
1433 update_buffer_properties (start
, end
);
1435 tem
= Fnext_property_change (make_number (start
), Qnil
, make_number (end
));
1436 tem1
= Ftext_properties_at (make_number (start
), Qnil
);
1438 if (XINT (tem
) != end
|| !NILP (tem1
))
1439 copy_intervals_to_string (result
, current_buffer
, start
, end
- start
);
1446 /* Call Vbuffer_access_fontify_functions for the range START ... END
1447 in the current buffer, if necessary. */
1450 update_buffer_properties (start
, end
)
1453 #ifdef USE_TEXT_PROPERTIES
1454 /* If this buffer has some access functions,
1455 call them, specifying the range of the buffer being accessed. */
1456 if (!NILP (Vbuffer_access_fontify_functions
))
1458 Lisp_Object args
[3];
1461 args
[0] = Qbuffer_access_fontify_functions
;
1462 XSETINT (args
[1], start
);
1463 XSETINT (args
[2], end
);
1465 /* But don't call them if we can tell that the work
1466 has already been done. */
1467 if (!NILP (Vbuffer_access_fontified_property
))
1469 tem
= Ftext_property_any (args
[1], args
[2],
1470 Vbuffer_access_fontified_property
,
1473 Frun_hook_with_args (3, args
);
1476 Frun_hook_with_args (3, args
);
1481 DEFUN ("buffer-substring", Fbuffer_substring
, Sbuffer_substring
, 2, 2, 0,
1482 "Return the contents of part of the current buffer as a string.\n\
1483 The two arguments START and END are character positions;\n\
1484 they can be in either order.")
1486 Lisp_Object start
, end
;
1490 validate_region (&start
, &end
);
1494 return make_buffer_string (b
, e
, 1);
1497 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties
,
1498 Sbuffer_substring_no_properties
, 2, 2, 0,
1499 "Return the characters of part of the buffer, without the text properties.\n\
1500 The two arguments START and END are character positions;\n\
1501 they can be in either order.")
1503 Lisp_Object start
, end
;
1507 validate_region (&start
, &end
);
1511 return make_buffer_string (b
, e
, 0);
1514 DEFUN ("buffer-string", Fbuffer_string
, Sbuffer_string
, 0, 0, 0,
1515 "Return the contents of the current buffer as a string.\n\
1516 If narrowing is in effect, this function returns only the visible part\n\
1520 return make_buffer_string (BEGV
, ZV
, 1);
1523 DEFUN ("insert-buffer-substring", Finsert_buffer_substring
, Sinsert_buffer_substring
,
1525 "Insert before point a substring of the contents of buffer BUFFER.\n\
1526 BUFFER may be a buffer or a buffer name.\n\
1527 Arguments START and END are character numbers specifying the substring.\n\
1528 They default to the beginning and the end of BUFFER.")
1530 Lisp_Object buf
, start
, end
;
1532 register int b
, e
, temp
;
1533 register struct buffer
*bp
, *obuf
;
1536 buffer
= Fget_buffer (buf
);
1539 bp
= XBUFFER (buffer
);
1540 if (NILP (bp
->name
))
1541 error ("Selecting deleted buffer");
1547 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1554 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1559 temp
= b
, b
= e
, e
= temp
;
1561 if (!(BUF_BEGV (bp
) <= b
&& e
<= BUF_ZV (bp
)))
1562 args_out_of_range (start
, end
);
1564 obuf
= current_buffer
;
1565 set_buffer_internal_1 (bp
);
1566 update_buffer_properties (b
, e
);
1567 set_buffer_internal_1 (obuf
);
1569 insert_from_buffer (bp
, b
, e
- b
, 0);
1573 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings
, Scompare_buffer_substrings
,
1575 "Compare two substrings of two buffers; return result as number.\n\
1576 the value is -N if first string is less after N-1 chars,\n\
1577 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
1578 Each substring is represented as three arguments: BUFFER, START and END.\n\
1579 That makes six args in all, three for each substring.\n\n\
1580 The value of `case-fold-search' in the current buffer\n\
1581 determines whether case is significant or ignored.")
1582 (buffer1
, start1
, end1
, buffer2
, start2
, end2
)
1583 Lisp_Object buffer1
, start1
, end1
, buffer2
, start2
, end2
;
1585 register int begp1
, endp1
, begp2
, endp2
, temp
, len1
, len2
, length
, i
;
1586 register struct buffer
*bp1
, *bp2
;
1587 register Lisp_Object
*trt
1588 = (!NILP (current_buffer
->case_fold_search
)
1589 ? XCHAR_TABLE (current_buffer
->case_canon_table
)->contents
: 0);
1591 /* Find the first buffer and its substring. */
1594 bp1
= current_buffer
;
1598 buf1
= Fget_buffer (buffer1
);
1601 bp1
= XBUFFER (buf1
);
1602 if (NILP (bp1
->name
))
1603 error ("Selecting deleted buffer");
1607 begp1
= BUF_BEGV (bp1
);
1610 CHECK_NUMBER_COERCE_MARKER (start1
, 1);
1611 begp1
= XINT (start1
);
1614 endp1
= BUF_ZV (bp1
);
1617 CHECK_NUMBER_COERCE_MARKER (end1
, 2);
1618 endp1
= XINT (end1
);
1622 temp
= begp1
, begp1
= endp1
, endp1
= temp
;
1624 if (!(BUF_BEGV (bp1
) <= begp1
1626 && endp1
<= BUF_ZV (bp1
)))
1627 args_out_of_range (start1
, end1
);
1629 /* Likewise for second substring. */
1632 bp2
= current_buffer
;
1636 buf2
= Fget_buffer (buffer2
);
1639 bp2
= XBUFFER (buf2
);
1640 if (NILP (bp2
->name
))
1641 error ("Selecting deleted buffer");
1645 begp2
= BUF_BEGV (bp2
);
1648 CHECK_NUMBER_COERCE_MARKER (start2
, 4);
1649 begp2
= XINT (start2
);
1652 endp2
= BUF_ZV (bp2
);
1655 CHECK_NUMBER_COERCE_MARKER (end2
, 5);
1656 endp2
= XINT (end2
);
1660 temp
= begp2
, begp2
= endp2
, endp2
= temp
;
1662 if (!(BUF_BEGV (bp2
) <= begp2
1664 && endp2
<= BUF_ZV (bp2
)))
1665 args_out_of_range (start2
, end2
);
1667 len1
= endp1
- begp1
;
1668 len2
= endp2
- begp2
;
1673 for (i
= 0; i
< length
; i
++)
1675 int c1
= *BUF_CHAR_ADDRESS (bp1
, begp1
+ i
);
1676 int c2
= *BUF_CHAR_ADDRESS (bp2
, begp2
+ i
);
1679 c1
= XINT (trt
[c1
]);
1680 c2
= XINT (trt
[c2
]);
1683 return make_number (- 1 - i
);
1685 return make_number (i
+ 1);
1688 /* The strings match as far as they go.
1689 If one is shorter, that one is less. */
1691 return make_number (length
+ 1);
1692 else if (length
< len2
)
1693 return make_number (- length
- 1);
1695 /* Same length too => they are equal. */
1696 return make_number (0);
1700 subst_char_in_region_unwind (arg
)
1703 return current_buffer
->undo_list
= arg
;
1707 subst_char_in_region_unwind_1 (arg
)
1710 return current_buffer
->filename
= arg
;
1713 DEFUN ("subst-char-in-region", Fsubst_char_in_region
,
1714 Ssubst_char_in_region
, 4, 5, 0,
1715 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1716 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1717 and don't mark the buffer as really changed.\n\
1718 Both characters must have the same length of multi-byte form.")
1719 (start
, end
, fromchar
, tochar
, noundo
)
1720 Lisp_Object start
, end
, fromchar
, tochar
, noundo
;
1722 register int pos
, stop
, i
, len
;
1724 unsigned char fromwork
[4], *fromstr
, towork
[4], *tostr
, *p
;
1725 int count
= specpdl_ptr
- specpdl
;
1727 validate_region (&start
, &end
);
1728 CHECK_NUMBER (fromchar
, 2);
1729 CHECK_NUMBER (tochar
, 3);
1731 if (! NILP (current_buffer
->enable_multibyte_characters
))
1733 len
= CHAR_STRING (XFASTINT (fromchar
), fromwork
, fromstr
);
1734 if (CHAR_STRING (XFASTINT (tochar
), towork
, tostr
) != len
)
1735 error ("Characters in subst-char-in-region have different byte-lengths");
1740 fromwork
[0] = XFASTINT (fromchar
), fromstr
= fromwork
;
1741 towork
[0] = XFASTINT (tochar
), tostr
= towork
;
1747 /* If we don't want undo, turn off putting stuff on the list.
1748 That's faster than getting rid of things,
1749 and it prevents even the entry for a first change.
1750 Also inhibit locking the file. */
1753 record_unwind_protect (subst_char_in_region_unwind
,
1754 current_buffer
->undo_list
);
1755 current_buffer
->undo_list
= Qt
;
1756 /* Don't do file-locking. */
1757 record_unwind_protect (subst_char_in_region_unwind_1
,
1758 current_buffer
->filename
);
1759 current_buffer
->filename
= Qnil
;
1763 stop
= min(stop
, GPT
);
1769 if (pos
>= XINT (end
)) break;
1773 if (p
[0] == fromstr
[0]
1775 || (p
[1] == fromstr
[1]
1776 && (len
== 2 || (p
[2] == fromstr
[2]
1777 && (len
== 3 || p
[3] == fromstr
[3]))))))
1781 modify_region (current_buffer
, XINT (start
), XINT (end
));
1783 if (! NILP (noundo
))
1785 if (MODIFF
- 1 == SAVE_MODIFF
)
1787 if (MODIFF
- 1 == current_buffer
->auto_save_modified
)
1788 current_buffer
->auto_save_modified
++;
1795 record_change (pos
, len
);
1796 for (i
= 0; i
< len
; i
++) *p
++ = tostr
[i
];
1804 signal_after_change (XINT (start
),
1805 stop
- XINT (start
), stop
- XINT (start
));
1807 unbind_to (count
, Qnil
);
1811 DEFUN ("translate-region", Ftranslate_region
, Stranslate_region
, 3, 3, 0,
1812 "From START to END, translate characters according to TABLE.\n\
1813 TABLE is a string; the Nth character in it is the mapping\n\
1814 for the character with code N. Returns the number of characters changed.")
1818 register Lisp_Object table
;
1820 register int pos
, stop
; /* Limits of the region. */
1821 register unsigned char *tt
; /* Trans table. */
1822 register int oc
; /* Old character. */
1823 register int nc
; /* New character. */
1824 int cnt
; /* Number of changes made. */
1825 Lisp_Object z
; /* Return. */
1826 int size
; /* Size of translate table. */
1828 validate_region (&start
, &end
);
1829 CHECK_STRING (table
, 2);
1831 size
= XSTRING (table
)->size
;
1832 tt
= XSTRING (table
)->data
;
1836 modify_region (current_buffer
, pos
, stop
);
1839 for (; pos
< stop
; ++pos
)
1841 oc
= FETCH_BYTE (pos
);
1847 record_change (pos
, 1);
1848 *(POS_ADDR (pos
)) = nc
;
1849 signal_after_change (pos
, 1, 1);
1855 XSETFASTINT (z
, cnt
);
1859 DEFUN ("delete-region", Fdelete_region
, Sdelete_region
, 2, 2, "r",
1860 "Delete the text between point and mark.\n\
1861 When called from a program, expects two arguments,\n\
1862 positions (integers or markers) specifying the stretch to be deleted.")
1864 Lisp_Object start
, end
;
1866 validate_region (&start
, &end
);
1867 del_range (XINT (start
), XINT (end
));
1871 DEFUN ("widen", Fwiden
, Swiden
, 0, 0, "",
1872 "Remove restrictions (narrowing) from current buffer.\n\
1873 This allows the buffer's full text to be seen and edited.")
1877 SET_BUF_ZV (current_buffer
, Z
);
1878 current_buffer
->clip_changed
= 1;
1879 /* Changing the buffer bounds invalidates any recorded current column. */
1880 invalidate_current_column ();
1884 DEFUN ("narrow-to-region", Fnarrow_to_region
, Snarrow_to_region
, 2, 2, "r",
1885 "Restrict editing in this buffer to the current region.\n\
1886 The rest of the text becomes temporarily invisible and untouchable\n\
1887 but is not deleted; if you save the buffer in a file, the invisible\n\
1888 text is included in the file. \\[widen] makes all visible again.\n\
1889 See also `save-restriction'.\n\
1891 When calling from a program, pass two arguments; positions (integers\n\
1892 or markers) bounding the text that should remain visible.")
1894 register Lisp_Object start
, end
;
1896 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1897 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1899 if (XINT (start
) > XINT (end
))
1902 tem
= start
; start
= end
; end
= tem
;
1905 if (!(BEG
<= XINT (start
) && XINT (start
) <= XINT (end
) && XINT (end
) <= Z
))
1906 args_out_of_range (start
, end
);
1908 BEGV
= XFASTINT (start
);
1909 SET_BUF_ZV (current_buffer
, XFASTINT (end
));
1910 if (PT
< XFASTINT (start
))
1911 SET_PT (XFASTINT (start
));
1912 if (PT
> XFASTINT (end
))
1913 SET_PT (XFASTINT (end
));
1914 current_buffer
->clip_changed
= 1;
1915 /* Changing the buffer bounds invalidates any recorded current column. */
1916 invalidate_current_column ();
1921 save_restriction_save ()
1923 register Lisp_Object bottom
, top
;
1924 /* Note: I tried using markers here, but it does not win
1925 because insertion at the end of the saved region
1926 does not advance mh and is considered "outside" the saved region. */
1927 XSETFASTINT (bottom
, BEGV
- BEG
);
1928 XSETFASTINT (top
, Z
- ZV
);
1930 return Fcons (Fcurrent_buffer (), Fcons (bottom
, top
));
1934 save_restriction_restore (data
)
1937 register struct buffer
*buf
;
1938 register int newhead
, newtail
;
1939 register Lisp_Object tem
;
1941 buf
= XBUFFER (XCONS (data
)->car
);
1943 data
= XCONS (data
)->cdr
;
1945 tem
= XCONS (data
)->car
;
1946 newhead
= XINT (tem
);
1947 tem
= XCONS (data
)->cdr
;
1948 newtail
= XINT (tem
);
1949 if (newhead
+ newtail
> BUF_Z (buf
) - BUF_BEG (buf
))
1954 BUF_BEGV (buf
) = BUF_BEG (buf
) + newhead
;
1955 SET_BUF_ZV (buf
, BUF_Z (buf
) - newtail
);
1956 current_buffer
->clip_changed
= 1;
1958 /* If point is outside the new visible range, move it inside. */
1960 clip_to_bounds (BUF_BEGV (buf
), BUF_PT (buf
), BUF_ZV (buf
)));
1965 DEFUN ("save-restriction", Fsave_restriction
, Ssave_restriction
, 0, UNEVALLED
, 0,
1966 "Execute BODY, saving and restoring current buffer's restrictions.\n\
1967 The buffer's restrictions make parts of the beginning and end invisible.\n\
1968 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
1969 This special form, `save-restriction', saves the current buffer's restrictions\n\
1970 when it is entered, and restores them when it is exited.\n\
1971 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
1972 The old restrictions settings are restored\n\
1973 even in case of abnormal exit (throw or error).\n\
1975 The value returned is the value of the last form in BODY.\n\
1977 `save-restriction' can get confused if, within the BODY, you widen\n\
1978 and then make changes outside the area within the saved restrictions.\n\
1980 Note: if you are using both `save-excursion' and `save-restriction',\n\
1981 use `save-excursion' outermost:\n\
1982 (save-excursion (save-restriction ...))")
1986 register Lisp_Object val
;
1987 int count
= specpdl_ptr
- specpdl
;
1989 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
1990 val
= Fprogn (body
);
1991 return unbind_to (count
, val
);
1994 /* Buffer for the most recent text displayed by Fmessage. */
1995 static char *message_text
;
1997 /* Allocated length of that buffer. */
1998 static int message_length
;
2000 DEFUN ("message", Fmessage
, Smessage
, 1, MANY
, 0,
2001 "Print a one-line message at the bottom of the screen.\n\
2002 The first argument is a format control string, and the rest are data\n\
2003 to be formatted under control of the string. See `format' for details.\n\
2005 If the first argument is nil, clear any existing message; let the\n\
2006 minibuffer contents show.")
2018 register Lisp_Object val
;
2019 val
= Fformat (nargs
, args
);
2020 /* Copy the data so that it won't move when we GC. */
2023 message_text
= (char *)xmalloc (80);
2024 message_length
= 80;
2026 if (XSTRING (val
)->size
> message_length
)
2028 message_length
= XSTRING (val
)->size
;
2029 message_text
= (char *)xrealloc (message_text
, message_length
);
2031 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
2032 message2 (message_text
, XSTRING (val
)->size
);
2037 DEFUN ("message-box", Fmessage_box
, Smessage_box
, 1, MANY
, 0,
2038 "Display a message, in a dialog box if possible.\n\
2039 If a dialog box is not available, use the echo area.\n\
2040 The first argument is a format control string, and the rest are data\n\
2041 to be formatted under control of the string. See `format' for details.\n\
2043 If the first argument is nil, clear any existing message; let the\n\
2044 minibuffer contents show.")
2056 register Lisp_Object val
;
2057 val
= Fformat (nargs
, args
);
2060 Lisp_Object pane
, menu
, obj
;
2061 struct gcpro gcpro1
;
2062 pane
= Fcons (Fcons (build_string ("OK"), Qt
), Qnil
);
2064 menu
= Fcons (val
, pane
);
2065 obj
= Fx_popup_dialog (Qt
, menu
);
2069 #else /* not HAVE_MENUS */
2070 /* Copy the data so that it won't move when we GC. */
2073 message_text
= (char *)xmalloc (80);
2074 message_length
= 80;
2076 if (XSTRING (val
)->size
> message_length
)
2078 message_length
= XSTRING (val
)->size
;
2079 message_text
= (char *)xrealloc (message_text
, message_length
);
2081 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
2082 message2 (message_text
, XSTRING (val
)->size
);
2084 #endif /* not HAVE_MENUS */
2088 extern Lisp_Object last_nonmenu_event
;
2091 DEFUN ("message-or-box", Fmessage_or_box
, Smessage_or_box
, 1, MANY
, 0,
2092 "Display a message in a dialog box or in the echo area.\n\
2093 If this command was invoked with the mouse, use a dialog box.\n\
2094 Otherwise, use the echo area.\n\
2095 The first argument is a format control string, and the rest are data\n\
2096 to be formatted under control of the string. See `format' for details.\n\
2098 If the first argument is nil, clear any existing message; let the\n\
2099 minibuffer contents show.")
2105 if (NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2106 return Fmessage_box (nargs
, args
);
2108 return Fmessage (nargs
, args
);
2111 DEFUN ("format", Fformat
, Sformat
, 1, MANY
, 0,
2112 "Format a string out of a control-string and arguments.\n\
2113 The first argument is a control string.\n\
2114 The other arguments are substituted into it to make the result, a string.\n\
2115 It may contain %-sequences meaning to substitute the next argument.\n\
2116 %s means print a string argument. Actually, prints any object, with `princ'.\n\
2117 %d means print as number in decimal (%o octal, %x hex).\n\
2118 %e means print a number in exponential notation.\n\
2119 %f means print a number in decimal-point notation.\n\
2120 %g means print a number in exponential notation\n\
2121 or decimal-point notation, whichever uses fewer characters.\n\
2122 %c means print a number as a single character.\n\
2123 %S means print any object as an s-expression (using prin1).\n\
2124 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2125 Use %% to put a single % into the output.")
2128 register Lisp_Object
*args
;
2130 register int n
; /* The number of the next arg to substitute */
2131 register int total
= 5; /* An estimate of the final length */
2133 register unsigned char *format
, *end
;
2135 extern char *index ();
2136 /* It should not be necessary to GCPRO ARGS, because
2137 the caller in the interpreter should take care of that. */
2139 CHECK_STRING (args
[0], 0);
2140 format
= XSTRING (args
[0])->data
;
2141 end
= format
+ XSTRING (args
[0])->size
;
2144 while (format
!= end
)
2145 if (*format
++ == '%')
2149 /* Process a numeric arg and skip it. */
2150 minlen
= atoi (format
);
2154 while ((*format
>= '0' && *format
<= '9')
2155 || *format
== '-' || *format
== ' ' || *format
== '.')
2160 else if (++n
>= nargs
)
2161 error ("Not enough arguments for format string");
2162 else if (*format
== 'S')
2164 /* For `S', prin1 the argument and then treat like a string. */
2165 register Lisp_Object tem
;
2166 tem
= Fprin1_to_string (args
[n
], Qnil
);
2170 else if (SYMBOLP (args
[n
]))
2172 XSETSTRING (args
[n
], XSYMBOL (args
[n
])->name
);
2175 else if (STRINGP (args
[n
]))
2178 if (*format
!= 's' && *format
!= 'S')
2179 error ("format specifier doesn't match argument type");
2180 total
+= XSTRING (args
[n
])->size
;
2181 /* We have to put an arbitrary limit on minlen
2182 since otherwise it could make alloca fail. */
2183 if (minlen
< XSTRING (args
[n
])->size
+ 1000)
2186 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
2187 else if (INTEGERP (args
[n
]) && *format
!= 's')
2189 #ifdef LISP_FLOAT_TYPE
2190 /* The following loop assumes the Lisp type indicates
2191 the proper way to pass the argument.
2192 So make sure we have a flonum if the argument should
2194 if (*format
== 'e' || *format
== 'f' || *format
== 'g')
2195 args
[n
] = Ffloat (args
[n
]);
2198 /* We have to put an arbitrary limit on minlen
2199 since otherwise it could make alloca fail. */
2203 #ifdef LISP_FLOAT_TYPE
2204 else if (FLOATP (args
[n
]) && *format
!= 's')
2206 if (! (*format
== 'e' || *format
== 'f' || *format
== 'g'))
2207 args
[n
] = Ftruncate (args
[n
]);
2209 /* We have to put an arbitrary limit on minlen
2210 since otherwise it could make alloca fail. */
2217 /* Anything but a string, convert to a string using princ. */
2218 register Lisp_Object tem
;
2219 tem
= Fprin1_to_string (args
[n
], Qt
);
2226 register int nstrings
= n
+ 1;
2228 /* Allocate twice as many strings as we have %-escapes; floats occupy
2229 two slots, and we're not sure how many of those we have. */
2230 register unsigned char **strings
2231 = (unsigned char **) alloca (2 * nstrings
* sizeof (unsigned char *));
2235 for (n
= 0; n
< nstrings
; n
++)
2238 strings
[i
++] = (unsigned char *) "";
2239 else if (INTEGERP (args
[n
]))
2240 /* We checked above that the corresponding format effector
2241 isn't %s, which would cause MPV. */
2242 strings
[i
++] = (unsigned char *) XINT (args
[n
]);
2243 #ifdef LISP_FLOAT_TYPE
2244 else if (FLOATP (args
[n
]))
2246 union { double d
; char *half
[2]; } u
;
2248 u
.d
= XFLOAT (args
[n
])->data
;
2249 strings
[i
++] = (unsigned char *) u
.half
[0];
2250 strings
[i
++] = (unsigned char *) u
.half
[1];
2254 /* The first string is treated differently
2255 because it is the format string. */
2256 strings
[i
++] = XSTRING (args
[n
])->data
;
2258 strings
[i
++] = (unsigned char *) XSTRING (args
[n
]);
2261 /* Make room in result for all the non-%-codes in the control string. */
2262 total
+= XSTRING (args
[0])->size
;
2264 /* Format it in bigger and bigger buf's until it all fits. */
2267 buf
= (char *) alloca (total
+ 1);
2270 length
= doprnt_lisp (buf
, total
+ 1, strings
[0],
2271 end
, i
-1, strings
+ 1);
2272 if (buf
[total
- 1] == 0)
2280 return make_string (buf
, length
);
2286 format1 (string1
, arg0
, arg1
, arg2
, arg3
, arg4
)
2287 EMACS_INT arg0
, arg1
, arg2
, arg3
, arg4
;
2301 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, args
);
2303 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, &string1
+ 1);
2305 return build_string (buf
);
2308 DEFUN ("char-equal", Fchar_equal
, Schar_equal
, 2, 2, 0,
2309 "Return t if two characters match, optionally ignoring case.\n\
2310 Both arguments must be characters (i.e. integers).\n\
2311 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2313 register Lisp_Object c1
, c2
;
2315 CHECK_NUMBER (c1
, 0);
2316 CHECK_NUMBER (c2
, 1);
2318 if (XINT (c1
) == XINT (c2
)
2319 && (NILP (current_buffer
->case_fold_search
)
2320 || DOWNCASE (XFASTINT (c1
)) == DOWNCASE (XFASTINT (c2
))))
2325 /* Transpose the markers in two regions of the current buffer, and
2326 adjust the ones between them if necessary (i.e.: if the regions
2329 Traverses the entire marker list of the buffer to do so, adding an
2330 appropriate amount to some, subtracting from some, and leaving the
2331 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2333 It's the caller's job to see that (start1 <= end1 <= start2 <= end2). */
2336 transpose_markers (start1
, end1
, start2
, end2
)
2337 register int start1
, end1
, start2
, end2
;
2339 register int amt1
, amt2
, diff
, mpos
;
2340 register Lisp_Object marker
;
2342 /* Update point as if it were a marker. */
2346 TEMP_SET_PT (PT
+ (end2
- end1
));
2347 else if (PT
< start2
)
2348 TEMP_SET_PT (PT
+ (end2
- start2
) - (end1
- start1
));
2350 TEMP_SET_PT (PT
- (start2
- start1
));
2352 /* We used to adjust the endpoints here to account for the gap, but that
2353 isn't good enough. Even if we assume the caller has tried to move the
2354 gap out of our way, it might still be at start1 exactly, for example;
2355 and that places it `inside' the interval, for our purposes. The amount
2356 of adjustment is nontrivial if there's a `denormalized' marker whose
2357 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2358 the dirty work to Fmarker_position, below. */
2360 /* The difference between the region's lengths */
2361 diff
= (end2
- start2
) - (end1
- start1
);
2363 /* For shifting each marker in a region by the length of the other
2364 * region plus the distance between the regions.
2366 amt1
= (end2
- start2
) + (start2
- end1
);
2367 amt2
= (end1
- start1
) + (start2
- end1
);
2369 for (marker
= BUF_MARKERS (current_buffer
); !NILP (marker
);
2370 marker
= XMARKER (marker
)->chain
)
2372 mpos
= marker_position (marker
);
2373 if (mpos
>= start1
&& mpos
< end2
)
2377 else if (mpos
< start2
)
2381 if (mpos
> GPT
) mpos
+= GAP_SIZE
;
2382 XMARKER (marker
)->bufpos
= mpos
;
2387 DEFUN ("transpose-regions", Ftranspose_regions
, Stranspose_regions
, 4, 5, 0,
2388 "Transpose region START1 to END1 with START2 to END2.\n\
2389 The regions may not be overlapping, because the size of the buffer is\n\
2390 never changed in a transposition.\n\
2392 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't transpose\n\
2393 any markers that happen to be located in the regions.\n\
2395 Transposing beyond buffer boundaries is an error.")
2396 (startr1
, endr1
, startr2
, endr2
, leave_markers
)
2397 Lisp_Object startr1
, endr1
, startr2
, endr2
, leave_markers
;
2399 register int start1
, end1
, start2
, end2
,
2400 gap
, len1
, len_mid
, len2
;
2401 unsigned char *start1_addr
, *start2_addr
, *temp
;
2403 #ifdef USE_TEXT_PROPERTIES
2404 INTERVAL cur_intv
, tmp_interval1
, tmp_interval_mid
, tmp_interval2
;
2405 cur_intv
= BUF_INTERVALS (current_buffer
);
2406 #endif /* USE_TEXT_PROPERTIES */
2408 validate_region (&startr1
, &endr1
);
2409 validate_region (&startr2
, &endr2
);
2411 start1
= XFASTINT (startr1
);
2412 end1
= XFASTINT (endr1
);
2413 start2
= XFASTINT (startr2
);
2414 end2
= XFASTINT (endr2
);
2417 /* Swap the regions if they're reversed. */
2420 register int glumph
= start1
;
2428 len1
= end1
- start1
;
2429 len2
= end2
- start2
;
2432 error ("transposed regions not properly ordered");
2433 else if (start1
== end1
|| start2
== end2
)
2434 error ("transposed region may not be of length 0");
2436 /* The possibilities are:
2437 1. Adjacent (contiguous) regions, or separate but equal regions
2438 (no, really equal, in this case!), or
2439 2. Separate regions of unequal size.
2441 The worst case is usually No. 2. It means that (aside from
2442 potential need for getting the gap out of the way), there also
2443 needs to be a shifting of the text between the two regions. So
2444 if they are spread far apart, we are that much slower... sigh. */
2446 /* It must be pointed out that the really studly thing to do would
2447 be not to move the gap at all, but to leave it in place and work
2448 around it if necessary. This would be extremely efficient,
2449 especially considering that people are likely to do
2450 transpositions near where they are working interactively, which
2451 is exactly where the gap would be found. However, such code
2452 would be much harder to write and to read. So, if you are
2453 reading this comment and are feeling squirrely, by all means have
2454 a go! I just didn't feel like doing it, so I will simply move
2455 the gap the minimum distance to get it out of the way, and then
2456 deal with an unbroken array. */
2458 /* Make sure the gap won't interfere, by moving it out of the text
2459 we will operate on. */
2460 if (start1
< gap
&& gap
< end2
)
2462 if (gap
- start1
< end2
- gap
)
2468 /* Hmmm... how about checking to see if the gap is large
2469 enough to use as the temporary storage? That would avoid an
2470 allocation... interesting. Later, don't fool with it now. */
2472 /* Working without memmove, for portability (sigh), so must be
2473 careful of overlapping subsections of the array... */
2475 if (end1
== start2
) /* adjacent regions */
2477 modify_region (current_buffer
, start1
, end2
);
2478 record_change (start1
, len1
+ len2
);
2480 #ifdef USE_TEXT_PROPERTIES
2481 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2482 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2483 Fset_text_properties (start1
, end2
, Qnil
, Qnil
);
2484 #endif /* USE_TEXT_PROPERTIES */
2486 /* First region smaller than second. */
2489 /* We use alloca only if it is small,
2490 because we want to avoid stack overflow. */
2492 temp
= (unsigned char *) xmalloc (len2
);
2494 temp
= (unsigned char *) alloca (len2
);
2496 /* Don't precompute these addresses. We have to compute them
2497 at the last minute, because the relocating allocator might
2498 have moved the buffer around during the xmalloc. */
2499 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2500 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2502 bcopy (start2_addr
, temp
, len2
);
2503 bcopy (start1_addr
, start1_addr
+ len2
, len1
);
2504 bcopy (temp
, start1_addr
, len2
);
2509 /* First region not smaller than second. */
2512 temp
= (unsigned char *) xmalloc (len1
);
2514 temp
= (unsigned char *) alloca (len1
);
2515 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2516 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2517 bcopy (start1_addr
, temp
, len1
);
2518 bcopy (start2_addr
, start1_addr
, len2
);
2519 bcopy (temp
, start1_addr
+ len2
, len1
);
2523 #ifdef USE_TEXT_PROPERTIES
2524 graft_intervals_into_buffer (tmp_interval1
, start1
+ len2
,
2525 len1
, current_buffer
, 0);
2526 graft_intervals_into_buffer (tmp_interval2
, start1
,
2527 len2
, current_buffer
, 0);
2528 #endif /* USE_TEXT_PROPERTIES */
2530 /* Non-adjacent regions, because end1 != start2, bleagh... */
2534 /* Regions are same size, though, how nice. */
2536 modify_region (current_buffer
, start1
, end1
);
2537 modify_region (current_buffer
, start2
, end2
);
2538 record_change (start1
, len1
);
2539 record_change (start2
, len2
);
2540 #ifdef USE_TEXT_PROPERTIES
2541 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2542 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2543 Fset_text_properties (start1
, end1
, Qnil
, Qnil
);
2544 Fset_text_properties (start2
, end2
, Qnil
, Qnil
);
2545 #endif /* USE_TEXT_PROPERTIES */
2548 temp
= (unsigned char *) xmalloc (len1
);
2550 temp
= (unsigned char *) alloca (len1
);
2551 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2552 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2553 bcopy (start1_addr
, temp
, len1
);
2554 bcopy (start2_addr
, start1_addr
, len2
);
2555 bcopy (temp
, start2_addr
, len1
);
2558 #ifdef USE_TEXT_PROPERTIES
2559 graft_intervals_into_buffer (tmp_interval1
, start2
,
2560 len1
, current_buffer
, 0);
2561 graft_intervals_into_buffer (tmp_interval2
, start1
,
2562 len2
, current_buffer
, 0);
2563 #endif /* USE_TEXT_PROPERTIES */
2566 else if (len1
< len2
) /* Second region larger than first */
2567 /* Non-adjacent & unequal size, area between must also be shifted. */
2569 len_mid
= start2
- end1
;
2570 modify_region (current_buffer
, start1
, end2
);
2571 record_change (start1
, (end2
- start1
));
2572 #ifdef USE_TEXT_PROPERTIES
2573 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2574 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2575 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2576 Fset_text_properties (start1
, end2
, Qnil
, Qnil
);
2577 #endif /* USE_TEXT_PROPERTIES */
2579 /* holds region 2 */
2581 temp
= (unsigned char *) xmalloc (len2
);
2583 temp
= (unsigned char *) alloca (len2
);
2584 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2585 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2586 bcopy (start2_addr
, temp
, len2
);
2587 bcopy (start1_addr
, start1_addr
+ len_mid
+ len2
, len1
);
2588 safe_bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2589 bcopy (temp
, start1_addr
, len2
);
2592 #ifdef USE_TEXT_PROPERTIES
2593 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2594 len1
, current_buffer
, 0);
2595 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2596 len_mid
, current_buffer
, 0);
2597 graft_intervals_into_buffer (tmp_interval2
, start1
,
2598 len2
, current_buffer
, 0);
2599 #endif /* USE_TEXT_PROPERTIES */
2602 /* Second region smaller than first. */
2604 len_mid
= start2
- end1
;
2605 record_change (start1
, (end2
- start1
));
2606 modify_region (current_buffer
, start1
, end2
);
2608 #ifdef USE_TEXT_PROPERTIES
2609 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2610 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2611 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2612 Fset_text_properties (start1
, end2
, Qnil
, Qnil
);
2613 #endif /* USE_TEXT_PROPERTIES */
2615 /* holds region 1 */
2617 temp
= (unsigned char *) xmalloc (len1
);
2619 temp
= (unsigned char *) alloca (len1
);
2620 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2621 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2622 bcopy (start1_addr
, temp
, len1
);
2623 bcopy (start2_addr
, start1_addr
, len2
);
2624 bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2625 bcopy (temp
, start1_addr
+ len2
+ len_mid
, len1
);
2628 #ifdef USE_TEXT_PROPERTIES
2629 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2630 len1
, current_buffer
, 0);
2631 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2632 len_mid
, current_buffer
, 0);
2633 graft_intervals_into_buffer (tmp_interval2
, start1
,
2634 len2
, current_buffer
, 0);
2635 #endif /* USE_TEXT_PROPERTIES */
2639 /* todo: this will be slow, because for every transposition, we
2640 traverse the whole friggin marker list. Possible solutions:
2641 somehow get a list of *all* the markers across multiple
2642 transpositions and do it all in one swell phoop. Or maybe modify
2643 Emacs' marker code to keep an ordered list or tree. This might
2644 be nicer, and more beneficial in the long run, but would be a
2645 bunch of work. Plus the way they're arranged now is nice. */
2646 if (NILP (leave_markers
))
2648 transpose_markers (start1
, end1
, start2
, end2
);
2649 fix_overlays_in_range (start1
, end2
);
2661 Qbuffer_access_fontify_functions
2662 = intern ("buffer-access-fontify-functions");
2663 staticpro (&Qbuffer_access_fontify_functions
);
2665 DEFVAR_LISP ("buffer-access-fontify-functions",
2666 &Vbuffer_access_fontify_functions
,
2667 "List of functions called by `buffer-substring' to fontify if necessary.\n\
2668 Each function is called with two arguments which specify the range\n\
2669 of the buffer being accessed.");
2670 Vbuffer_access_fontify_functions
= Qnil
;
2674 extern Lisp_Object Vprin1_to_string_buffer
;
2675 obuf
= Fcurrent_buffer ();
2676 /* Do this here, because init_buffer_once is too early--it won't work. */
2677 Fset_buffer (Vprin1_to_string_buffer
);
2678 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
2679 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
2684 DEFVAR_LISP ("buffer-access-fontified-property",
2685 &Vbuffer_access_fontified_property
,
2686 "Property which (if non-nil) indicates text has been fontified.\n\
2687 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
2688 functions if all the text being accessed has this property.");
2689 Vbuffer_access_fontified_property
= Qnil
;
2691 DEFVAR_LISP ("system-name", &Vsystem_name
,
2692 "The name of the machine Emacs is running on.");
2694 DEFVAR_LISP ("user-full-name", &Vuser_full_name
,
2695 "The full name of the user logged in.");
2697 DEFVAR_LISP ("user-login-name", &Vuser_login_name
,
2698 "The user's name, taken from environment variables if possible.");
2700 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name
,
2701 "The user's name, based upon the real uid only.");
2703 defsubr (&Schar_equal
);
2704 defsubr (&Sgoto_char
);
2705 defsubr (&Sstring_to_char
);
2706 defsubr (&Schar_to_string
);
2708 defsubr (&Sbuffer_substring
);
2709 defsubr (&Sbuffer_substring_no_properties
);
2710 defsubr (&Sbuffer_string
);
2712 defsubr (&Spoint_marker
);
2713 defsubr (&Smark_marker
);
2715 defsubr (&Sregion_beginning
);
2716 defsubr (&Sregion_end
);
2717 /* defsubr (&Smark); */
2718 /* defsubr (&Sset_mark); */
2719 defsubr (&Ssave_excursion
);
2720 defsubr (&Ssave_current_buffer
);
2722 defsubr (&Sbufsize
);
2723 defsubr (&Spoint_max
);
2724 defsubr (&Spoint_min
);
2725 defsubr (&Spoint_min_marker
);
2726 defsubr (&Spoint_max_marker
);
2728 defsubr (&Sline_beginning_position
);
2729 defsubr (&Sline_end_position
);
2735 defsubr (&Sfollowing_char
);
2736 defsubr (&Sprevious_char
);
2737 defsubr (&Schar_after
);
2738 defsubr (&Schar_before
);
2740 defsubr (&Sinsert_before_markers
);
2741 defsubr (&Sinsert_and_inherit
);
2742 defsubr (&Sinsert_and_inherit_before_markers
);
2743 defsubr (&Sinsert_char
);
2745 defsubr (&Suser_login_name
);
2746 defsubr (&Suser_real_login_name
);
2747 defsubr (&Suser_uid
);
2748 defsubr (&Suser_real_uid
);
2749 defsubr (&Suser_full_name
);
2750 defsubr (&Semacs_pid
);
2751 defsubr (&Scurrent_time
);
2752 defsubr (&Sformat_time_string
);
2753 defsubr (&Sdecode_time
);
2754 defsubr (&Sencode_time
);
2755 defsubr (&Scurrent_time_string
);
2756 defsubr (&Scurrent_time_zone
);
2757 defsubr (&Sset_time_zone_rule
);
2758 defsubr (&Ssystem_name
);
2759 defsubr (&Smessage
);
2760 defsubr (&Smessage_box
);
2761 defsubr (&Smessage_or_box
);
2764 defsubr (&Sinsert_buffer_substring
);
2765 defsubr (&Scompare_buffer_substrings
);
2766 defsubr (&Ssubst_char_in_region
);
2767 defsubr (&Stranslate_region
);
2768 defsubr (&Sdelete_region
);
2770 defsubr (&Snarrow_to_region
);
2771 defsubr (&Ssave_restriction
);
2772 defsubr (&Stranspose_regions
);