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 size_t emacs_strftime ();
53 void set_time_zone_rule ();
55 Lisp_Object Vbuffer_access_fontify_functions
;
56 Lisp_Object Qbuffer_access_fontify_functions
;
57 Lisp_Object Vbuffer_access_fontified_property
;
59 Lisp_Object
Fuser_full_name ();
61 /* Some static data, and a function to initialize it for each run */
63 Lisp_Object Vsystem_name
;
64 Lisp_Object Vuser_real_login_name
; /* login name of current user ID */
65 Lisp_Object Vuser_full_name
; /* full name of current user */
66 Lisp_Object Vuser_login_name
; /* user name from LOGNAME or USER */
72 register unsigned char *p
, *q
, *r
;
73 struct passwd
*pw
; /* password entry for the current user */
76 /* Set up system_name even when dumping. */
80 /* Don't bother with this on initial start when just dumping out */
83 #endif /* not CANNOT_DUMP */
85 pw
= (struct passwd
*) getpwuid (getuid ());
87 /* We let the real user name default to "root" because that's quite
88 accurate on MSDOG and because it lets Emacs find the init file.
89 (The DVX libraries override the Djgpp libraries here.) */
90 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "root");
92 Vuser_real_login_name
= build_string (pw
? pw
->pw_name
: "unknown");
95 /* Get the effective user name, by consulting environment variables,
96 or the effective uid if those are unset. */
97 user_name
= (char *) getenv ("LOGNAME");
100 user_name
= (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
101 #else /* WINDOWSNT */
102 user_name
= (char *) getenv ("USER");
103 #endif /* WINDOWSNT */
106 pw
= (struct passwd
*) getpwuid (geteuid ());
107 user_name
= (char *) (pw
? pw
->pw_name
: "unknown");
109 Vuser_login_name
= build_string (user_name
);
111 /* If the user name claimed in the environment vars differs from
112 the real uid, use the claimed name to find the full name. */
113 tem
= Fstring_equal (Vuser_login_name
, Vuser_real_login_name
);
114 Vuser_full_name
= Fuser_full_name (NILP (tem
)? make_number (geteuid())
117 p
= (unsigned char *) getenv ("NAME");
119 Vuser_full_name
= build_string (p
);
120 else if (NILP (Vuser_full_name
))
121 Vuser_full_name
= build_string ("unknown");
124 DEFUN ("char-to-string", Fchar_to_string
, Schar_to_string
, 1, 1, 0,
125 "Convert arg CHAR to a string containing multi-byte form of that character.")
127 Lisp_Object character
;
130 unsigned char workbuf
[4], *str
;
132 CHECK_NUMBER (character
, 0);
134 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
135 return make_string (str
, len
);
138 DEFUN ("string-to-char", Fstring_to_char
, Sstring_to_char
, 1, 1, 0,
139 "Convert arg STRING to a character, the first character of that string.\n\
140 A multibyte character is handled correctly.")
142 register Lisp_Object string
;
144 register Lisp_Object val
;
145 register struct Lisp_String
*p
;
146 CHECK_STRING (string
, 0);
147 p
= XSTRING (string
);
149 XSETFASTINT (val
, STRING_CHAR (p
->data
, p
->size
));
151 XSETFASTINT (val
, 0);
155 DEFUN ("sref", Fsref
, Ssref
, 2, 2, 0,
156 "Return the character in STRING at INDEX. INDEX starts at 0.\n\
157 A multibyte character is handled correctly.\n\
158 INDEX not pointing at character boundary is an error.")
160 Lisp_Object str
, idx
;
162 register int idxval
, len
, i
;
163 register unsigned char *p
, *q
;
164 register Lisp_Object val
;
166 CHECK_STRING (str
, 0);
167 CHECK_NUMBER (idx
, 1);
169 if (idxval
< 0 || idxval
>= (len
= XVECTOR (str
)->size
))
170 args_out_of_range (str
, idx
);
172 p
= XSTRING (str
)->data
+ idxval
;
173 if (!NILP (current_buffer
->enable_multibyte_characters
)
177 /* We must check if P points to a tailing byte of a multibyte
178 form. If so, we signal error. */
181 while (i
> 0 && *q
>= 0xA0) i
--, q
--;
183 if (*q
== LEADING_CODE_COMPOSITION
)
184 i
= multibyte_form_length (XSTRING (str
)->data
+ i
, len
- i
);
186 i
= BYTES_BY_CHAR_HEAD (*q
);
188 error ("Not character boundary");
191 len
= XSTRING (str
)->size
- idxval
;
192 XSETFASTINT (val
, STRING_CHAR (p
, len
));
201 register Lisp_Object mark
;
202 mark
= Fmake_marker ();
203 Fset_marker (mark
, make_number (val
), Qnil
);
207 DEFUN ("point", Fpoint
, Spoint
, 0, 0, 0,
208 "Return value of point, as an integer.\n\
209 Beginning of buffer is position (point-min)")
213 XSETFASTINT (temp
, PT
);
217 DEFUN ("point-marker", Fpoint_marker
, Spoint_marker
, 0, 0, 0,
218 "Return value of point, as a marker object.")
221 return buildmark (PT
);
225 clip_to_bounds (lower
, num
, upper
)
226 int lower
, num
, upper
;
230 else if (num
> upper
)
236 DEFUN ("goto-char", Fgoto_char
, Sgoto_char
, 1, 1, "NGoto char: ",
237 "Set point to POSITION, a number or marker.\n\
238 Beginning of buffer is position (point-min), end is (point-max).\n\
239 If the position is in the middle of a multibyte form,\n\
240 the actual point is set at the head of the multibyte form\n\
241 except in the case that `enable-multibyte-characters' is nil.")
243 register Lisp_Object position
;
248 CHECK_NUMBER_COERCE_MARKER (position
, 0);
250 pos
= clip_to_bounds (BEGV
, XINT (position
), ZV
);
251 /* If POS is in a middle of multi-byte form (i.e. *P >= 0xA0), we
252 must decrement POS until it points the head of the multi-byte
254 if (!NILP (current_buffer
->enable_multibyte_characters
)
255 && *(p
= POS_ADDR (pos
)) >= 0xA0
258 /* Since a multi-byte form does not contain the gap, POS should
259 not stride over the gap while it is being decreased. So, we
260 set the limit as below. */
261 unsigned char *p_min
= pos
< GPT
? BEG_ADDR
: GAP_END_ADDR
;
262 unsigned int saved_pos
= pos
;
266 } while (p
> p_min
&& *p
>= 0xA0);
268 /* This was an invalid multi-byte form. */
270 XSETFASTINT (position
, pos
);
277 region_limit (beginningp
)
280 extern Lisp_Object Vmark_even_if_inactive
; /* Defined in callint.c. */
281 register Lisp_Object m
;
282 if (!NILP (Vtransient_mark_mode
) && NILP (Vmark_even_if_inactive
)
283 && NILP (current_buffer
->mark_active
))
284 Fsignal (Qmark_inactive
, Qnil
);
285 m
= Fmarker_position (current_buffer
->mark
);
286 if (NILP (m
)) error ("There is no region now");
287 if ((PT
< XFASTINT (m
)) == beginningp
)
288 return (make_number (PT
));
293 DEFUN ("region-beginning", Fregion_beginning
, Sregion_beginning
, 0, 0, 0,
294 "Return position of beginning of region, as an integer.")
297 return (region_limit (1));
300 DEFUN ("region-end", Fregion_end
, Sregion_end
, 0, 0, 0,
301 "Return position of end of region, as an integer.")
304 return (region_limit (0));
307 DEFUN ("mark-marker", Fmark_marker
, Smark_marker
, 0, 0, 0,
308 "Return this buffer's mark, as a marker object.\n\
309 Watch out! Moving this marker changes the mark position.\n\
310 If you set the marker not to point anywhere, the buffer will have no mark.")
313 return current_buffer
->mark
;
316 DEFUN ("line-beginning-position", Fline_beginning_position
, Sline_beginning_position
,
318 "Return the character position of the first character on the current line.\n\
319 With argument N not nil or 1, move forward N - 1 lines first.\n\
320 If scan reaches end of buffer, return that position.\n\
321 This function does not move point.")
325 register int orig
, end
;
333 Fforward_line (make_number (XINT (n
) - 1));
337 return make_number (end
);
340 DEFUN ("line-end-position", Fline_end_position
, Sline_end_position
,
342 "Return the character position of the last character on the current line.\n\
343 With argument N not nil or 1, move forward N - 1 lines first.\n\
344 If scan reaches end of buffer, return that position.\n\
345 This function does not move point.")
354 return make_number (find_before_next_newline
355 (PT
, 0, XINT (n
) - (XINT (n
) <= 0)));
359 save_excursion_save ()
361 register int visible
= (XBUFFER (XWINDOW (selected_window
)->buffer
)
364 return Fcons (Fpoint_marker (),
365 Fcons (Fcopy_marker (current_buffer
->mark
, Qnil
),
366 Fcons (visible
? Qt
: Qnil
,
367 current_buffer
->mark_active
)));
371 save_excursion_restore (info
)
374 Lisp_Object tem
, tem1
, omark
, nmark
;
375 struct gcpro gcpro1
, gcpro2
, gcpro3
;
377 tem
= Fmarker_buffer (Fcar (info
));
378 /* If buffer being returned to is now deleted, avoid error */
379 /* Otherwise could get error here while unwinding to top level
381 /* In that case, Fmarker_buffer returns nil now. */
385 omark
= nmark
= Qnil
;
386 GCPRO3 (info
, omark
, nmark
);
391 unchain_marker (tem
);
392 tem
= Fcar (Fcdr (info
));
393 omark
= Fmarker_position (current_buffer
->mark
);
394 Fset_marker (current_buffer
->mark
, tem
, Fcurrent_buffer ());
395 nmark
= Fmarker_position (tem
);
396 unchain_marker (tem
);
397 tem
= Fcdr (Fcdr (info
));
398 #if 0 /* We used to make the current buffer visible in the selected window
399 if that was true previously. That avoids some anomalies.
400 But it creates others, and it wasn't documented, and it is simpler
401 and cleaner never to alter the window/buffer connections. */
404 && current_buffer
!= XBUFFER (XWINDOW (selected_window
)->buffer
))
405 Fswitch_to_buffer (Fcurrent_buffer (), Qnil
);
408 tem1
= current_buffer
->mark_active
;
409 current_buffer
->mark_active
= Fcdr (tem
);
410 if (!NILP (Vrun_hooks
))
412 /* If mark is active now, and either was not active
413 or was at a different place, run the activate hook. */
414 if (! NILP (current_buffer
->mark_active
))
416 if (! EQ (omark
, nmark
))
417 call1 (Vrun_hooks
, intern ("activate-mark-hook"));
419 /* If mark has ceased to be active, run deactivate hook. */
420 else if (! NILP (tem1
))
421 call1 (Vrun_hooks
, intern ("deactivate-mark-hook"));
427 DEFUN ("save-excursion", Fsave_excursion
, Ssave_excursion
, 0, UNEVALLED
, 0,
428 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
429 Executes BODY just like `progn'.\n\
430 The values of point, mark and the current buffer are restored\n\
431 even in case of abnormal exit (throw or error).\n\
432 The state of activation of the mark is also restored.")
436 register Lisp_Object val
;
437 int count
= specpdl_ptr
- specpdl
;
439 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
442 return unbind_to (count
, val
);
445 DEFUN ("save-current-buffer", Fsave_current_buffer
, Ssave_current_buffer
, 0, UNEVALLED
, 0,
446 "Save the current buffer; execute BODY; restore the current buffer.\n\
447 Executes BODY just like `progn'.")
451 register Lisp_Object val
;
452 int count
= specpdl_ptr
- specpdl
;
454 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
457 return unbind_to (count
, val
);
460 DEFUN ("buffer-size", Fbufsize
, Sbufsize
, 0, 0, 0,
461 "Return the number of characters in the current buffer.")
465 XSETFASTINT (temp
, Z
- BEG
);
469 DEFUN ("point-min", Fpoint_min
, Spoint_min
, 0, 0, 0,
470 "Return the minimum permissible value of point in the current buffer.\n\
471 This is 1, unless narrowing (a buffer restriction) is in effect.")
475 XSETFASTINT (temp
, BEGV
);
479 DEFUN ("point-min-marker", Fpoint_min_marker
, Spoint_min_marker
, 0, 0, 0,
480 "Return a marker to the minimum permissible value of point in this buffer.\n\
481 This is the beginning, unless narrowing (a buffer restriction) is in effect.")
484 return buildmark (BEGV
);
487 DEFUN ("point-max", Fpoint_max
, Spoint_max
, 0, 0, 0,
488 "Return the maximum permissible value of point in the current buffer.\n\
489 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
490 is in effect, in which case it is less.")
494 XSETFASTINT (temp
, ZV
);
498 DEFUN ("point-max-marker", Fpoint_max_marker
, Spoint_max_marker
, 0, 0, 0,
499 "Return a marker to the maximum permissible value of point in this buffer.\n\
500 This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
501 is in effect, in which case it is less.")
504 return buildmark (ZV
);
507 DEFUN ("following-char", Ffollowing_char
, Sfollowing_char
, 0, 0, 0,
508 "Return the character following point, as a number.\n\
509 At the end of the buffer or accessible region, return 0.\n\
510 If `enable-multibyte-characters' is nil or point is not\n\
511 at character boundary, multibyte form is ignored,\n\
512 and only one byte following point is returned as a character.")
517 XSETFASTINT (temp
, 0);
519 XSETFASTINT (temp
, FETCH_CHAR (PT
));
523 DEFUN ("preceding-char", Fprevious_char
, Sprevious_char
, 0, 0, 0,
524 "Return the character preceding point, as a number.\n\
525 At the beginning of the buffer or accessible region, return 0.\n\
526 If `enable-multibyte-characters' is nil or point is not\n\
527 at character boundary, multi-byte form is ignored,\n\
528 and only one byte preceding point is returned as a character.")
533 XSETFASTINT (temp
, 0);
534 else if (!NILP (current_buffer
->enable_multibyte_characters
))
538 XSETFASTINT (temp
, FETCH_CHAR (pos
));
541 XSETFASTINT (temp
, FETCH_BYTE (PT
- 1));
545 DEFUN ("bobp", Fbobp
, Sbobp
, 0, 0, 0,
546 "Return T if point is at the beginning of the buffer.\n\
547 If the buffer is narrowed, this means the beginning of the narrowed part.")
555 DEFUN ("eobp", Feobp
, Seobp
, 0, 0, 0,
556 "Return T if point is at the end of the buffer.\n\
557 If the buffer is narrowed, this means the end of the narrowed part.")
565 DEFUN ("bolp", Fbolp
, Sbolp
, 0, 0, 0,
566 "Return T if point is at the beginning of a line.")
569 if (PT
== BEGV
|| FETCH_BYTE (PT
- 1) == '\n')
574 DEFUN ("eolp", Feolp
, Seolp
, 0, 0, 0,
575 "Return T if point is at the end of a line.\n\
576 `End of a line' includes point being at the end of the buffer.")
579 if (PT
== ZV
|| FETCH_BYTE (PT
) == '\n')
584 DEFUN ("char-after", Fchar_after
, Schar_after
, 0, 1, 0,
585 "Return character in current buffer at position POS.\n\
586 POS is an integer or a buffer pointer.\n\
587 If POS is out of range, the value is nil.\n\
588 If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
589 multi-byte form is ignored, and only one byte at POS\n\
590 is returned as a character.")
594 register Lisp_Object val
;
601 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
604 if (n
< BEGV
|| n
>= ZV
)
608 XSETFASTINT (val
, FETCH_CHAR (n
));
612 DEFUN ("char-before", Fchar_before
, Schar_before
, 0, 1, 0,
613 "Return character in current buffer preceding position POS.\n\
614 POS is an integer or a buffer pointer.\n\
615 If POS is out of range, the value is nil.\n\
616 If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
617 multi-byte form is ignored, and only one byte preceding POS\n\
618 is returned as a character.")
622 register Lisp_Object val
;
629 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
634 if (n
<= BEGV
|| n
> ZV
)
637 if (!NILP (current_buffer
->enable_multibyte_characters
))
640 XSETFASTINT (val
, FETCH_CHAR (n
));
645 XSETFASTINT (val
, FETCH_BYTE (n
));
650 DEFUN ("user-login-name", Fuser_login_name
, Suser_login_name
, 0, 1, 0,
651 "Return the name under which the user logged in, as a string.\n\
652 This is based on the effective uid, not the real uid.\n\
653 Also, if the environment variable LOGNAME or USER is set,\n\
654 that determines the value of this function.\n\n\
655 If optional argument UID is an integer, return the login name of the user\n\
656 with that uid, or nil if there is no such user.")
662 /* Set up the user name info if we didn't do it before.
663 (That can happen if Emacs is dumpable
664 but you decide to run `temacs -l loadup' and not dump. */
665 if (INTEGERP (Vuser_login_name
))
669 return Vuser_login_name
;
671 CHECK_NUMBER (uid
, 0);
672 pw
= (struct passwd
*) getpwuid (XINT (uid
));
673 return (pw
? build_string (pw
->pw_name
) : Qnil
);
676 DEFUN ("user-real-login-name", Fuser_real_login_name
, Suser_real_login_name
,
678 "Return the name of the user's real uid, as a string.\n\
679 This ignores the environment variables LOGNAME and USER, so it differs from\n\
680 `user-login-name' when running under `su'.")
683 /* Set up the user name info if we didn't do it before.
684 (That can happen if Emacs is dumpable
685 but you decide to run `temacs -l loadup' and not dump. */
686 if (INTEGERP (Vuser_login_name
))
688 return Vuser_real_login_name
;
691 DEFUN ("user-uid", Fuser_uid
, Suser_uid
, 0, 0, 0,
692 "Return the effective uid of Emacs, as an integer.")
695 return make_number (geteuid ());
698 DEFUN ("user-real-uid", Fuser_real_uid
, Suser_real_uid
, 0, 0, 0,
699 "Return the real uid of Emacs, as an integer.")
702 return make_number (getuid ());
705 DEFUN ("user-full-name", Fuser_full_name
, Suser_full_name
, 0, 1, 0,
706 "Return the full name of the user logged in, as a string.\n\
707 If optional argument UID is an integer, return the full name of the user\n\
708 with that uid, or \"unknown\" if there is no such user.\n\
709 If UID is a string, return the full name of the user with that login\n\
710 name, or \"unknown\" if no such user could be found.")
715 register unsigned char *p
, *q
;
716 extern char *index ();
720 return Vuser_full_name
;
721 else if (NUMBERP (uid
))
722 pw
= (struct passwd
*) getpwuid (XINT (uid
));
723 else if (STRINGP (uid
))
724 pw
= (struct passwd
*) getpwnam (XSTRING (uid
)->data
);
726 error ("Invalid UID specification");
731 p
= (unsigned char *) USER_FULL_NAME
;
732 /* Chop off everything after the first comma. */
733 q
= (unsigned char *) index (p
, ',');
734 full
= make_string (p
, q
? q
- p
: strlen (p
));
736 #ifdef AMPERSAND_FULL_NAME
737 p
= XSTRING (full
)->data
;
738 q
= (unsigned char *) index (p
, '&');
739 /* Substitute the login name for the &, upcasing the first character. */
742 register unsigned char *r
;
745 login
= Fuser_login_name (make_number (pw
->pw_uid
));
746 r
= (unsigned char *) alloca (strlen (p
) + XSTRING (login
)->size
+ 1);
749 strcat (r
, XSTRING (login
)->data
);
750 r
[q
- p
] = UPCASE (r
[q
- p
]);
752 full
= build_string (r
);
754 #endif /* AMPERSAND_FULL_NAME */
759 DEFUN ("system-name", Fsystem_name
, Ssystem_name
, 0, 0, 0,
760 "Return the name of the machine you are running on, as a string.")
766 /* For the benefit of callers who don't want to include lisp.h */
770 if (STRINGP (Vsystem_name
))
771 return (char *) XSTRING (Vsystem_name
)->data
;
776 DEFUN ("emacs-pid", Femacs_pid
, Semacs_pid
, 0, 0, 0,
777 "Return the process ID of Emacs, as an integer.")
780 return make_number (getpid ());
783 DEFUN ("current-time", Fcurrent_time
, Scurrent_time
, 0, 0, 0,
784 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
785 The time is returned as a list of three integers. The first has the\n\
786 most significant 16 bits of the seconds, while the second has the\n\
787 least significant 16 bits. The third integer gives the microsecond\n\
790 The microsecond count is zero on systems that do not provide\n\
791 resolution finer than a second.")
795 Lisp_Object result
[3];
798 XSETINT (result
[0], (EMACS_SECS (t
) >> 16) & 0xffff);
799 XSETINT (result
[1], (EMACS_SECS (t
) >> 0) & 0xffff);
800 XSETINT (result
[2], EMACS_USECS (t
));
802 return Flist (3, result
);
807 lisp_time_argument (specified_time
, result
)
808 Lisp_Object specified_time
;
811 if (NILP (specified_time
))
812 return time (result
) != -1;
815 Lisp_Object high
, low
;
816 high
= Fcar (specified_time
);
817 CHECK_NUMBER (high
, 0);
818 low
= Fcdr (specified_time
);
821 CHECK_NUMBER (low
, 0);
822 *result
= (XINT (high
) << 16) + (XINT (low
) & 0xffff);
823 return *result
>> 16 == XINT (high
);
828 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
829 "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
830 TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
831 `current-time' or `file-attributes'.\n\
832 The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
833 as Universal Time; nil means describe TIME in the local time zone.\n\
834 The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
835 by text that describes the specified date and time in TIME:\n\
837 %Y is the year, %y within the century, %C the century.\n\
838 %G is the year corresponding to the ISO week, %g within the century.\n\
839 %m is the numeric month.\n\
840 %b and %h are the locale's abbreviated month name, %B the full name.\n\
841 %d is the day of the month, zero-padded, %e is blank-padded.\n\
842 %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\
843 %a is the locale's abbreviated name of the day of week, %A the full name.\n\
844 %U is the week number starting on Sunday, %W starting on Monday,\n\
845 %V according to ISO 8601.\n\
846 %j is the day of the year.\n\
848 %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\
849 only blank-padded, %l is like %I blank-padded.\n\
850 %p is the locale's equivalent of either AM or PM.\n\
853 %Z is the time zone name, %z is the numeric form.\n\
854 %s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\
856 %c is the locale's date and time format.\n\
857 %x is the locale's \"preferred\" date format.\n\
858 %D is like \"%m/%d/%y\".\n\
860 %R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\
861 %X is the locale's \"preferred\" time format.\n\
863 Finally, %n is a newline, %t is a tab, %% is a literal %.\n\
865 Certain flags and modifiers are available with some format controls.\n\
866 The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
867 but padded with blanks; %-X is like %X, but without padding.\n\
868 %NX (where N stands for an integer) is like %X,\n\
869 but takes up at least N (a number) positions.\n\
870 The modifiers are `E' and `O'. For certain characters X,\n\
871 %EX is a locale's alternative version of %X;\n\
872 %OX is like %X, but uses the locale's number symbols.\n\
874 For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
875 (format_string, time, universal)
878 DEFUN ("format-time-string", Fformat_time_string
, Sformat_time_string
, 1, 3, 0,
879 0 /* See immediately above */)
880 (format_string
, time
, universal
)
881 Lisp_Object format_string
, time
, universal
;
886 CHECK_STRING (format_string
, 1);
888 if (! lisp_time_argument (time
, &value
))
889 error ("Invalid time specification");
891 /* This is probably enough. */
892 size
= XSTRING (format_string
)->size
* 6 + 50;
896 char *buf
= (char *) alloca (size
+ 1);
900 result
= emacs_strftime (buf
, size
, XSTRING (format_string
)->data
,
901 (NILP (universal
) ? localtime (&value
)
903 if ((result
> 0 && result
< size
) || (result
== 0 && buf
[0] == '\0'))
904 return build_string (buf
);
906 /* If buffer was too small, make it bigger and try again. */
907 result
= emacs_strftime (NULL
, 0x7fffffff, XSTRING (format_string
)->data
,
908 (NILP (universal
) ? localtime (&value
)
914 DEFUN ("decode-time", Fdecode_time
, Sdecode_time
, 0, 1, 0,
915 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
916 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
917 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
918 to use the current time. The list has the following nine members:\n\
919 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
920 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
921 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
922 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
923 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
924 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
925 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
926 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
928 Lisp_Object specified_time
;
932 struct tm
*decoded_time
;
933 Lisp_Object list_args
[9];
935 if (! lisp_time_argument (specified_time
, &time_spec
))
936 error ("Invalid time specification");
938 decoded_time
= localtime (&time_spec
);
939 XSETFASTINT (list_args
[0], decoded_time
->tm_sec
);
940 XSETFASTINT (list_args
[1], decoded_time
->tm_min
);
941 XSETFASTINT (list_args
[2], decoded_time
->tm_hour
);
942 XSETFASTINT (list_args
[3], decoded_time
->tm_mday
);
943 XSETFASTINT (list_args
[4], decoded_time
->tm_mon
+ 1);
944 XSETINT (list_args
[5], decoded_time
->tm_year
+ 1900);
945 XSETFASTINT (list_args
[6], decoded_time
->tm_wday
);
946 list_args
[7] = (decoded_time
->tm_isdst
)? Qt
: Qnil
;
948 /* Make a copy, in case gmtime modifies the struct. */
949 save_tm
= *decoded_time
;
950 decoded_time
= gmtime (&time_spec
);
951 if (decoded_time
== 0)
954 XSETINT (list_args
[8], tm_diff (&save_tm
, decoded_time
));
955 return Flist (9, list_args
);
958 DEFUN ("encode-time", Fencode_time
, Sencode_time
, 6, MANY
, 0,
959 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
960 This is the reverse operation of `decode-time', which see.\n\
961 ZONE defaults to the current time zone rule. This can\n\
962 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
963 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
964 applied without consideration for daylight savings time.\n\
966 You can pass more than 7 arguments; then the first six arguments\n\
967 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
968 The intervening arguments are ignored.\n\
969 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
971 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
972 for example, a DAY of 0 means the day preceding the given month.\n\
973 Year numbers less than 100 are treated just like other year numbers.\n\
974 If you want them to stand for years in this century, you must do that yourself.")
977 register Lisp_Object
*args
;
981 Lisp_Object zone
= (nargs
> 6 ? args
[nargs
- 1] : Qnil
);
983 CHECK_NUMBER (args
[0], 0); /* second */
984 CHECK_NUMBER (args
[1], 1); /* minute */
985 CHECK_NUMBER (args
[2], 2); /* hour */
986 CHECK_NUMBER (args
[3], 3); /* day */
987 CHECK_NUMBER (args
[4], 4); /* month */
988 CHECK_NUMBER (args
[5], 5); /* year */
990 tm
.tm_sec
= XINT (args
[0]);
991 tm
.tm_min
= XINT (args
[1]);
992 tm
.tm_hour
= XINT (args
[2]);
993 tm
.tm_mday
= XINT (args
[3]);
994 tm
.tm_mon
= XINT (args
[4]) - 1;
995 tm
.tm_year
= XINT (args
[5]) - 1900;
1001 time
= mktime (&tm
);
1006 char **oldenv
= environ
, **newenv
;
1010 else if (STRINGP (zone
))
1011 tzstring
= (char *) XSTRING (zone
)->data
;
1012 else if (INTEGERP (zone
))
1014 int abszone
= abs (XINT (zone
));
1015 sprintf (tzbuf
, "XXX%s%d:%02d:%02d", "-" + (XINT (zone
) < 0),
1016 abszone
/ (60*60), (abszone
/60) % 60, abszone
% 60);
1020 error ("Invalid time zone specification");
1022 /* Set TZ before calling mktime; merely adjusting mktime's returned
1023 value doesn't suffice, since that would mishandle leap seconds. */
1024 set_time_zone_rule (tzstring
);
1026 time
= mktime (&tm
);
1028 /* Restore TZ to previous value. */
1032 #ifdef LOCALTIME_CACHE
1037 if (time
== (time_t) -1)
1038 error ("Specified time is not representable");
1040 return make_time (time
);
1043 DEFUN ("current-time-string", Fcurrent_time_string
, Scurrent_time_string
, 0, 1, 0,
1044 "Return the current time, as a human-readable string.\n\
1045 Programs can use this function to decode a time,\n\
1046 since the number of columns in each field is fixed.\n\
1047 The format is `Sun Sep 16 01:03:52 1973'.\n\
1048 However, see also the functions `decode-time' and `format-time-string'\n\
1049 which provide a much more powerful and general facility.\n\
1051 If an argument is given, it specifies a time to format\n\
1052 instead of the current time. The argument should have the form:\n\
1055 (HIGH LOW . IGNORED).\n\
1056 Thus, you can use times obtained from `current-time'\n\
1057 and from `file-attributes'.")
1059 Lisp_Object specified_time
;
1065 if (! lisp_time_argument (specified_time
, &value
))
1067 tem
= (char *) ctime (&value
);
1069 strncpy (buf
, tem
, 24);
1072 return build_string (buf
);
1075 #define TM_YEAR_BASE 1900
1077 /* Yield A - B, measured in seconds.
1078 This function is copied from the GNU C Library. */
1083 /* Compute intervening leap days correctly even if year is negative.
1084 Take care to avoid int overflow in leap day calculations,
1085 but it's OK to assume that A and B are close to each other. */
1086 int a4
= (a
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (a
->tm_year
& 3);
1087 int b4
= (b
->tm_year
>> 2) + (TM_YEAR_BASE
>> 2) - ! (b
->tm_year
& 3);
1088 int a100
= a4
/ 25 - (a4
% 25 < 0);
1089 int b100
= b4
/ 25 - (b4
% 25 < 0);
1090 int a400
= a100
>> 2;
1091 int b400
= b100
>> 2;
1092 int intervening_leap_days
= (a4
- b4
) - (a100
- b100
) + (a400
- b400
);
1093 int years
= a
->tm_year
- b
->tm_year
;
1094 int days
= (365 * years
+ intervening_leap_days
1095 + (a
->tm_yday
- b
->tm_yday
));
1096 return (60 * (60 * (24 * days
+ (a
->tm_hour
- b
->tm_hour
))
1097 + (a
->tm_min
- b
->tm_min
))
1098 + (a
->tm_sec
- b
->tm_sec
));
1101 DEFUN ("current-time-zone", Fcurrent_time_zone
, Scurrent_time_zone
, 0, 1, 0,
1102 "Return the offset and name for the local time zone.\n\
1103 This returns a list of the form (OFFSET NAME).\n\
1104 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1105 A negative value means west of Greenwich.\n\
1106 NAME is a string giving the name of the time zone.\n\
1107 If an argument is given, it specifies when the time zone offset is determined\n\
1108 instead of using the current time. The argument should have the form:\n\
1111 (HIGH LOW . IGNORED).\n\
1112 Thus, you can use times obtained from `current-time'\n\
1113 and from `file-attributes'.\n\
1115 Some operating systems cannot provide all this information to Emacs;\n\
1116 in this case, `current-time-zone' returns a list containing nil for\n\
1117 the data it can't find.")
1119 Lisp_Object specified_time
;
1124 if (lisp_time_argument (specified_time
, &value
)
1125 && (t
= gmtime (&value
)) != 0)
1131 gmt
= *t
; /* Make a copy, in case localtime modifies *t. */
1132 t
= localtime (&value
);
1133 offset
= tm_diff (t
, &gmt
);
1137 s
= (char *)t
->tm_zone
;
1138 #else /* not HAVE_TM_ZONE */
1140 if (t
->tm_isdst
== 0 || t
->tm_isdst
== 1)
1141 s
= tzname
[t
->tm_isdst
];
1143 #endif /* not HAVE_TM_ZONE */
1146 /* No local time zone name is available; use "+-NNNN" instead. */
1147 int am
= (offset
< 0 ? -offset
: offset
) / 60;
1148 sprintf (buf
, "%c%02d%02d", (offset
< 0 ? '-' : '+'), am
/60, am
%60);
1151 return Fcons (make_number (offset
), Fcons (build_string (s
), Qnil
));
1154 return Fmake_list (make_number (2), Qnil
);
1157 /* This holds the value of `environ' produced by the previous
1158 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1159 has never been called. */
1160 static char **environbuf
;
1162 DEFUN ("set-time-zone-rule", Fset_time_zone_rule
, Sset_time_zone_rule
, 1, 1, 0,
1163 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
1164 If TZ is nil, use implementation-defined default time zone information.\n\
1165 If TZ is t, use Universal Time.")
1173 else if (EQ (tz
, Qt
))
1177 CHECK_STRING (tz
, 0);
1178 tzstring
= (char *) XSTRING (tz
)->data
;
1181 set_time_zone_rule (tzstring
);
1184 environbuf
= environ
;
1189 #ifdef LOCALTIME_CACHE
1191 /* These two values are known to load tz files in buggy implementations,
1192 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1193 Their values shouldn't matter in non-buggy implementations.
1194 We don't use string literals for these strings,
1195 since if a string in the environment is in readonly
1196 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1197 See Sun bugs 1113095 and 1114114, ``Timezone routines
1198 improperly modify environment''. */
1200 static char set_time_zone_rule_tz1
[] = "TZ=GMT+0";
1201 static char set_time_zone_rule_tz2
[] = "TZ=GMT+1";
1205 /* Set the local time zone rule to TZSTRING.
1206 This allocates memory into `environ', which it is the caller's
1207 responsibility to free. */
1209 set_time_zone_rule (tzstring
)
1213 char **from
, **to
, **newenv
;
1215 /* Make the ENVIRON vector longer with room for TZSTRING. */
1216 for (from
= environ
; *from
; from
++)
1218 envptrs
= from
- environ
+ 2;
1219 newenv
= to
= (char **) xmalloc (envptrs
* sizeof (char *)
1220 + (tzstring
? strlen (tzstring
) + 4 : 0));
1222 /* Add TZSTRING to the end of environ, as a value for TZ. */
1225 char *t
= (char *) (to
+ envptrs
);
1227 strcat (t
, tzstring
);
1231 /* Copy the old environ vector elements into NEWENV,
1232 but don't copy the TZ variable.
1233 So we have only one definition of TZ, which came from TZSTRING. */
1234 for (from
= environ
; *from
; from
++)
1235 if (strncmp (*from
, "TZ=", 3) != 0)
1241 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1242 the TZ variable is stored. If we do not have a TZSTRING,
1243 TO points to the vector slot which has the terminating null. */
1245 #ifdef LOCALTIME_CACHE
1247 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1248 "US/Pacific" that loads a tz file, then changes to a value like
1249 "XXX0" that does not load a tz file, and then changes back to
1250 its original value, the last change is (incorrectly) ignored.
1251 Also, if TZ changes twice in succession to values that do
1252 not load a tz file, tzset can dump core (see Sun bug#1225179).
1253 The following code works around these bugs. */
1257 /* Temporarily set TZ to a value that loads a tz file
1258 and that differs from tzstring. */
1260 *newenv
= (strcmp (tzstring
, set_time_zone_rule_tz1
+ 3) == 0
1261 ? set_time_zone_rule_tz2
: set_time_zone_rule_tz1
);
1267 /* The implied tzstring is unknown, so temporarily set TZ to
1268 two different values that each load a tz file. */
1269 *to
= set_time_zone_rule_tz1
;
1272 *to
= set_time_zone_rule_tz2
;
1277 /* Now TZ has the desired value, and tzset can be invoked safely. */
1284 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1285 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1286 type of object is Lisp_String). INHERIT is passed to
1287 INSERT_FROM_STRING_FUNC as the last argument. */
1290 general_insert_function (insert_func
, insert_from_string_func
,
1291 inherit
, nargs
, args
)
1292 void (*insert_func
) P_ ((unsigned char *, int));
1293 void (*insert_from_string_func
) P_ ((Lisp_Object
, int, int, int));
1295 register Lisp_Object
*args
;
1297 register int argnum
;
1298 register Lisp_Object val
;
1300 for (argnum
= 0; argnum
< nargs
; argnum
++)
1306 unsigned char workbuf
[4], *str
;
1309 if (!NILP (current_buffer
->enable_multibyte_characters
))
1310 len
= CHAR_STRING (XFASTINT (val
), workbuf
, str
);
1312 workbuf
[0] = XINT (val
), str
= workbuf
, len
= 1;
1313 (*insert_func
) (str
, len
);
1315 else if (STRINGP (val
))
1317 (*insert_from_string_func
) (val
, 0, XSTRING (val
)->size
, inherit
);
1321 val
= wrong_type_argument (Qchar_or_string_p
, val
);
1335 /* Callers passing one argument to Finsert need not gcpro the
1336 argument "array", since the only element of the array will
1337 not be used after calling insert or insert_from_string, so
1338 we don't care if it gets trashed. */
1340 DEFUN ("insert", Finsert
, Sinsert
, 0, MANY
, 0,
1341 "Insert the arguments, either strings or characters, at point.\n\
1342 Point and before-insertion-markers move forward so that it ends up\n\
1343 after the inserted text.\n\
1344 Any other markers at the point of insertion remain before the text.")
1347 register Lisp_Object
*args
;
1349 general_insert_function (insert
, insert_from_string
, 0, nargs
, args
);
1353 DEFUN ("insert-and-inherit", Finsert_and_inherit
, Sinsert_and_inherit
,
1355 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1356 Point and before-insertion-markers move forward so that it ends up\n\
1357 after the inserted text.\n\
1358 Any other markers at the point of insertion remain before the text.")
1361 register Lisp_Object
*args
;
1363 general_insert_function (insert_and_inherit
, insert_from_string
, 1,
1368 DEFUN ("insert-before-markers", Finsert_before_markers
, Sinsert_before_markers
, 0, MANY
, 0,
1369 "Insert strings or characters at point, relocating markers after the text.\n\
1370 Point and before-insertion-markers move forward so that it ends up\n\
1371 after the inserted text.\n\
1372 Any other markers at the point of insertion also end up after the text.")
1375 register Lisp_Object
*args
;
1377 general_insert_function (insert_before_markers
,
1378 insert_from_string_before_markers
, 0,
1383 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers
,
1384 Sinsert_and_inherit_before_markers
, 0, MANY
, 0,
1385 "Insert text at point, relocating markers and inheriting properties.\n\
1386 Point moves forward so that it ends up after the inserted text.\n\
1387 Any other markers at the point of insertion also end up after the text.")
1390 register Lisp_Object
*args
;
1392 general_insert_function (insert_before_markers_and_inherit
,
1393 insert_from_string_before_markers
, 1,
1398 DEFUN ("insert-char", Finsert_char
, Sinsert_char
, 2, 3, 0,
1399 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1400 Point and before-insertion-markers are affected as in the function `insert'.\n\
1401 Both arguments are required.\n\
1402 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1403 from adjoining text, if those properties are sticky.")
1404 (character
, count
, inherit
)
1405 Lisp_Object character
, count
, inherit
;
1407 register unsigned char *string
;
1408 register int strlen
;
1411 unsigned char workbuf
[4], *str
;
1413 CHECK_NUMBER (character
, 0);
1414 CHECK_NUMBER (count
, 1);
1416 if (!NILP (current_buffer
->enable_multibyte_characters
))
1417 len
= CHAR_STRING (XFASTINT (character
), workbuf
, str
);
1419 workbuf
[0] = XFASTINT (character
), str
= workbuf
, len
= 1;
1420 n
= XINT (count
) * len
;
1423 strlen
= min (n
, 256 * len
);
1424 string
= (unsigned char *) alloca (strlen
);
1425 for (i
= 0; i
< strlen
; i
++)
1426 string
[i
] = str
[i
% len
];
1430 if (!NILP (inherit
))
1431 insert_and_inherit (string
, strlen
);
1433 insert (string
, strlen
);
1438 if (!NILP (inherit
))
1439 insert_and_inherit (string
, n
);
1447 /* Making strings from buffer contents. */
1449 /* Return a Lisp_String containing the text of the current buffer from
1450 START to END. If text properties are in use and the current buffer
1451 has properties in the range specified, the resulting string will also
1452 have them, if PROPS is nonzero.
1454 We don't want to use plain old make_string here, because it calls
1455 make_uninit_string, which can cause the buffer arena to be
1456 compacted. make_string has no way of knowing that the data has
1457 been moved, and thus copies the wrong data into the string. This
1458 doesn't effect most of the other users of make_string, so it should
1459 be left as is. But we should use this function when conjuring
1460 buffer substrings. */
1463 make_buffer_string (start
, end
, props
)
1467 Lisp_Object result
, tem
, tem1
;
1469 if (start
< GPT
&& GPT
< end
)
1472 result
= make_uninit_string (end
- start
);
1473 bcopy (POS_ADDR (start
), XSTRING (result
)->data
, end
- start
);
1475 /* If desired, update and copy the text properties. */
1476 #ifdef USE_TEXT_PROPERTIES
1479 update_buffer_properties (start
, end
);
1481 tem
= Fnext_property_change (make_number (start
), Qnil
, make_number (end
));
1482 tem1
= Ftext_properties_at (make_number (start
), Qnil
);
1484 if (XINT (tem
) != end
|| !NILP (tem1
))
1485 copy_intervals_to_string (result
, current_buffer
, start
, end
- start
);
1492 /* Call Vbuffer_access_fontify_functions for the range START ... END
1493 in the current buffer, if necessary. */
1496 update_buffer_properties (start
, end
)
1499 #ifdef USE_TEXT_PROPERTIES
1500 /* If this buffer has some access functions,
1501 call them, specifying the range of the buffer being accessed. */
1502 if (!NILP (Vbuffer_access_fontify_functions
))
1504 Lisp_Object args
[3];
1507 args
[0] = Qbuffer_access_fontify_functions
;
1508 XSETINT (args
[1], start
);
1509 XSETINT (args
[2], end
);
1511 /* But don't call them if we can tell that the work
1512 has already been done. */
1513 if (!NILP (Vbuffer_access_fontified_property
))
1515 tem
= Ftext_property_any (args
[1], args
[2],
1516 Vbuffer_access_fontified_property
,
1519 Frun_hook_with_args (3, args
);
1522 Frun_hook_with_args (3, args
);
1527 DEFUN ("buffer-substring", Fbuffer_substring
, Sbuffer_substring
, 2, 2, 0,
1528 "Return the contents of part of the current buffer as a string.\n\
1529 The two arguments START and END are character positions;\n\
1530 they can be in either order.")
1532 Lisp_Object start
, end
;
1536 validate_region (&start
, &end
);
1540 return make_buffer_string (b
, e
, 1);
1543 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties
,
1544 Sbuffer_substring_no_properties
, 2, 2, 0,
1545 "Return the characters of part of the buffer, without the text properties.\n\
1546 The two arguments START and END are character positions;\n\
1547 they can be in either order.")
1549 Lisp_Object start
, end
;
1553 validate_region (&start
, &end
);
1557 return make_buffer_string (b
, e
, 0);
1560 DEFUN ("buffer-string", Fbuffer_string
, Sbuffer_string
, 0, 0, 0,
1561 "Return the contents of the current buffer as a string.\n\
1562 If narrowing is in effect, this function returns only the visible part\n\
1566 return make_buffer_string (BEGV
, ZV
, 1);
1569 DEFUN ("insert-buffer-substring", Finsert_buffer_substring
, Sinsert_buffer_substring
,
1571 "Insert before point a substring of the contents of buffer BUFFER.\n\
1572 BUFFER may be a buffer or a buffer name.\n\
1573 Arguments START and END are character numbers specifying the substring.\n\
1574 They default to the beginning and the end of BUFFER.")
1576 Lisp_Object buf
, start
, end
;
1578 register int b
, e
, temp
;
1579 register struct buffer
*bp
, *obuf
;
1582 buffer
= Fget_buffer (buf
);
1585 bp
= XBUFFER (buffer
);
1586 if (NILP (bp
->name
))
1587 error ("Selecting deleted buffer");
1593 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1600 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1605 temp
= b
, b
= e
, e
= temp
;
1607 if (!(BUF_BEGV (bp
) <= b
&& e
<= BUF_ZV (bp
)))
1608 args_out_of_range (start
, end
);
1610 obuf
= current_buffer
;
1611 set_buffer_internal_1 (bp
);
1612 update_buffer_properties (b
, e
);
1613 set_buffer_internal_1 (obuf
);
1615 insert_from_buffer (bp
, b
, e
- b
, 0);
1619 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings
, Scompare_buffer_substrings
,
1621 "Compare two substrings of two buffers; return result as number.\n\
1622 the value is -N if first string is less after N-1 chars,\n\
1623 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
1624 Each substring is represented as three arguments: BUFFER, START and END.\n\
1625 That makes six args in all, three for each substring.\n\n\
1626 The value of `case-fold-search' in the current buffer\n\
1627 determines whether case is significant or ignored.")
1628 (buffer1
, start1
, end1
, buffer2
, start2
, end2
)
1629 Lisp_Object buffer1
, start1
, end1
, buffer2
, start2
, end2
;
1631 register int begp1
, endp1
, begp2
, endp2
, temp
, len1
, len2
, length
, i
;
1632 register struct buffer
*bp1
, *bp2
;
1633 register Lisp_Object
*trt
1634 = (!NILP (current_buffer
->case_fold_search
)
1635 ? XCHAR_TABLE (current_buffer
->case_canon_table
)->contents
: 0);
1637 /* Find the first buffer and its substring. */
1640 bp1
= current_buffer
;
1644 buf1
= Fget_buffer (buffer1
);
1647 bp1
= XBUFFER (buf1
);
1648 if (NILP (bp1
->name
))
1649 error ("Selecting deleted buffer");
1653 begp1
= BUF_BEGV (bp1
);
1656 CHECK_NUMBER_COERCE_MARKER (start1
, 1);
1657 begp1
= XINT (start1
);
1660 endp1
= BUF_ZV (bp1
);
1663 CHECK_NUMBER_COERCE_MARKER (end1
, 2);
1664 endp1
= XINT (end1
);
1668 temp
= begp1
, begp1
= endp1
, endp1
= temp
;
1670 if (!(BUF_BEGV (bp1
) <= begp1
1672 && endp1
<= BUF_ZV (bp1
)))
1673 args_out_of_range (start1
, end1
);
1675 /* Likewise for second substring. */
1678 bp2
= current_buffer
;
1682 buf2
= Fget_buffer (buffer2
);
1685 bp2
= XBUFFER (buf2
);
1686 if (NILP (bp2
->name
))
1687 error ("Selecting deleted buffer");
1691 begp2
= BUF_BEGV (bp2
);
1694 CHECK_NUMBER_COERCE_MARKER (start2
, 4);
1695 begp2
= XINT (start2
);
1698 endp2
= BUF_ZV (bp2
);
1701 CHECK_NUMBER_COERCE_MARKER (end2
, 5);
1702 endp2
= XINT (end2
);
1706 temp
= begp2
, begp2
= endp2
, endp2
= temp
;
1708 if (!(BUF_BEGV (bp2
) <= begp2
1710 && endp2
<= BUF_ZV (bp2
)))
1711 args_out_of_range (start2
, end2
);
1713 len1
= endp1
- begp1
;
1714 len2
= endp2
- begp2
;
1719 for (i
= 0; i
< length
; i
++)
1721 int c1
= *BUF_CHAR_ADDRESS (bp1
, begp1
+ i
);
1722 int c2
= *BUF_CHAR_ADDRESS (bp2
, begp2
+ i
);
1725 c1
= XINT (trt
[c1
]);
1726 c2
= XINT (trt
[c2
]);
1729 return make_number (- 1 - i
);
1731 return make_number (i
+ 1);
1734 /* The strings match as far as they go.
1735 If one is shorter, that one is less. */
1737 return make_number (length
+ 1);
1738 else if (length
< len2
)
1739 return make_number (- length
- 1);
1741 /* Same length too => they are equal. */
1742 return make_number (0);
1746 subst_char_in_region_unwind (arg
)
1749 return current_buffer
->undo_list
= arg
;
1753 subst_char_in_region_unwind_1 (arg
)
1756 return current_buffer
->filename
= arg
;
1759 DEFUN ("subst-char-in-region", Fsubst_char_in_region
,
1760 Ssubst_char_in_region
, 4, 5, 0,
1761 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1762 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1763 and don't mark the buffer as really changed.\n\
1764 Both characters must have the same length of multi-byte form.")
1765 (start
, end
, fromchar
, tochar
, noundo
)
1766 Lisp_Object start
, end
, fromchar
, tochar
, noundo
;
1768 register int pos
, stop
, i
, len
;
1770 unsigned char fromwork
[4], *fromstr
, towork
[4], *tostr
, *p
;
1771 int count
= specpdl_ptr
- specpdl
;
1773 validate_region (&start
, &end
);
1774 CHECK_NUMBER (fromchar
, 2);
1775 CHECK_NUMBER (tochar
, 3);
1777 if (! NILP (current_buffer
->enable_multibyte_characters
))
1779 len
= CHAR_STRING (XFASTINT (fromchar
), fromwork
, fromstr
);
1780 if (CHAR_STRING (XFASTINT (tochar
), towork
, tostr
) != len
)
1781 error ("Characters in subst-char-in-region have different byte-lengths");
1786 fromwork
[0] = XFASTINT (fromchar
), fromstr
= fromwork
;
1787 towork
[0] = XFASTINT (tochar
), tostr
= towork
;
1793 /* If we don't want undo, turn off putting stuff on the list.
1794 That's faster than getting rid of things,
1795 and it prevents even the entry for a first change.
1796 Also inhibit locking the file. */
1799 record_unwind_protect (subst_char_in_region_unwind
,
1800 current_buffer
->undo_list
);
1801 current_buffer
->undo_list
= Qt
;
1802 /* Don't do file-locking. */
1803 record_unwind_protect (subst_char_in_region_unwind_1
,
1804 current_buffer
->filename
);
1805 current_buffer
->filename
= Qnil
;
1809 stop
= min(stop
, GPT
);
1815 if (pos
>= XINT (end
)) break;
1819 if (p
[0] == fromstr
[0]
1821 || (p
[1] == fromstr
[1]
1822 && (len
== 2 || (p
[2] == fromstr
[2]
1823 && (len
== 3 || p
[3] == fromstr
[3]))))))
1827 modify_region (current_buffer
, XINT (start
), XINT (end
));
1829 if (! NILP (noundo
))
1831 if (MODIFF
- 1 == SAVE_MODIFF
)
1833 if (MODIFF
- 1 == current_buffer
->auto_save_modified
)
1834 current_buffer
->auto_save_modified
++;
1841 record_change (pos
, len
);
1842 for (i
= 0; i
< len
; i
++) *p
++ = tostr
[i
];
1850 signal_after_change (XINT (start
),
1851 stop
- XINT (start
), stop
- XINT (start
));
1853 unbind_to (count
, Qnil
);
1857 DEFUN ("translate-region", Ftranslate_region
, Stranslate_region
, 3, 3, 0,
1858 "From START to END, translate characters according to TABLE.\n\
1859 TABLE is a string; the Nth character in it is the mapping\n\
1860 for the character with code N. Returns the number of characters changed.")
1864 register Lisp_Object table
;
1866 register int pos
, stop
; /* Limits of the region. */
1867 register unsigned char *tt
; /* Trans table. */
1868 register int oc
; /* Old character. */
1869 register int nc
; /* New character. */
1870 int cnt
; /* Number of changes made. */
1871 Lisp_Object z
; /* Return. */
1872 int size
; /* Size of translate table. */
1874 validate_region (&start
, &end
);
1875 CHECK_STRING (table
, 2);
1877 size
= XSTRING (table
)->size
;
1878 tt
= XSTRING (table
)->data
;
1882 modify_region (current_buffer
, pos
, stop
);
1885 for (; pos
< stop
; ++pos
)
1887 oc
= FETCH_BYTE (pos
);
1893 record_change (pos
, 1);
1894 *(POS_ADDR (pos
)) = nc
;
1895 signal_after_change (pos
, 1, 1);
1901 XSETFASTINT (z
, cnt
);
1905 DEFUN ("delete-region", Fdelete_region
, Sdelete_region
, 2, 2, "r",
1906 "Delete the text between point and mark.\n\
1907 When called from a program, expects two arguments,\n\
1908 positions (integers or markers) specifying the stretch to be deleted.")
1910 Lisp_Object start
, end
;
1912 validate_region (&start
, &end
);
1913 del_range (XINT (start
), XINT (end
));
1917 DEFUN ("widen", Fwiden
, Swiden
, 0, 0, "",
1918 "Remove restrictions (narrowing) from current buffer.\n\
1919 This allows the buffer's full text to be seen and edited.")
1922 if (BEG
!= BEGV
|| Z
!= ZV
)
1923 current_buffer
->clip_changed
= 1;
1925 SET_BUF_ZV (current_buffer
, Z
);
1926 /* Changing the buffer bounds invalidates any recorded current column. */
1927 invalidate_current_column ();
1931 DEFUN ("narrow-to-region", Fnarrow_to_region
, Snarrow_to_region
, 2, 2, "r",
1932 "Restrict editing in this buffer to the current region.\n\
1933 The rest of the text becomes temporarily invisible and untouchable\n\
1934 but is not deleted; if you save the buffer in a file, the invisible\n\
1935 text is included in the file. \\[widen] makes all visible again.\n\
1936 See also `save-restriction'.\n\
1938 When calling from a program, pass two arguments; positions (integers\n\
1939 or markers) bounding the text that should remain visible.")
1941 register Lisp_Object start
, end
;
1943 CHECK_NUMBER_COERCE_MARKER (start
, 0);
1944 CHECK_NUMBER_COERCE_MARKER (end
, 1);
1946 if (XINT (start
) > XINT (end
))
1949 tem
= start
; start
= end
; end
= tem
;
1952 if (!(BEG
<= XINT (start
) && XINT (start
) <= XINT (end
) && XINT (end
) <= Z
))
1953 args_out_of_range (start
, end
);
1955 if (BEGV
!= XFASTINT (start
) || ZV
!= XFASTINT (end
))
1956 current_buffer
->clip_changed
= 1;
1958 BEGV
= XFASTINT (start
);
1959 SET_BUF_ZV (current_buffer
, XFASTINT (end
));
1960 if (PT
< XFASTINT (start
))
1961 SET_PT (XFASTINT (start
));
1962 if (PT
> XFASTINT (end
))
1963 SET_PT (XFASTINT (end
));
1964 /* Changing the buffer bounds invalidates any recorded current column. */
1965 invalidate_current_column ();
1970 save_restriction_save ()
1972 register Lisp_Object bottom
, top
;
1973 /* Note: I tried using markers here, but it does not win
1974 because insertion at the end of the saved region
1975 does not advance mh and is considered "outside" the saved region. */
1976 XSETFASTINT (bottom
, BEGV
- BEG
);
1977 XSETFASTINT (top
, Z
- ZV
);
1979 return Fcons (Fcurrent_buffer (), Fcons (bottom
, top
));
1983 save_restriction_restore (data
)
1986 register struct buffer
*buf
;
1987 register int newhead
, newtail
;
1988 register Lisp_Object tem
;
1991 buf
= XBUFFER (XCONS (data
)->car
);
1993 data
= XCONS (data
)->cdr
;
1995 tem
= XCONS (data
)->car
;
1996 newhead
= XINT (tem
);
1997 tem
= XCONS (data
)->cdr
;
1998 newtail
= XINT (tem
);
1999 if (newhead
+ newtail
> BUF_Z (buf
) - BUF_BEG (buf
))
2005 obegv
= BUF_BEGV (buf
);
2008 BUF_BEGV (buf
) = BUF_BEG (buf
) + newhead
;
2009 SET_BUF_ZV (buf
, BUF_Z (buf
) - newtail
);
2011 if (obegv
!= BUF_BEGV (buf
) || ozv
!= BUF_ZV (buf
))
2012 current_buffer
->clip_changed
= 1;
2014 /* If point is outside the new visible range, move it inside. */
2016 clip_to_bounds (BUF_BEGV (buf
), BUF_PT (buf
), BUF_ZV (buf
)));
2021 DEFUN ("save-restriction", Fsave_restriction
, Ssave_restriction
, 0, UNEVALLED
, 0,
2022 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2023 The buffer's restrictions make parts of the beginning and end invisible.\n\
2024 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2025 This special form, `save-restriction', saves the current buffer's restrictions\n\
2026 when it is entered, and restores them when it is exited.\n\
2027 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2028 The old restrictions settings are restored\n\
2029 even in case of abnormal exit (throw or error).\n\
2031 The value returned is the value of the last form in BODY.\n\
2033 `save-restriction' can get confused if, within the BODY, you widen\n\
2034 and then make changes outside the area within the saved restrictions.\n\
2036 Note: if you are using both `save-excursion' and `save-restriction',\n\
2037 use `save-excursion' outermost:\n\
2038 (save-excursion (save-restriction ...))")
2042 register Lisp_Object val
;
2043 int count
= specpdl_ptr
- specpdl
;
2045 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
2046 val
= Fprogn (body
);
2047 return unbind_to (count
, val
);
2050 /* Buffer for the most recent text displayed by Fmessage. */
2051 static char *message_text
;
2053 /* Allocated length of that buffer. */
2054 static int message_length
;
2056 DEFUN ("message", Fmessage
, Smessage
, 1, MANY
, 0,
2057 "Print a one-line message at the bottom of the screen.\n\
2058 The first argument is a format control string, and the rest are data\n\
2059 to be formatted under control of the string. See `format' for details.\n\
2061 If the first argument is nil, clear any existing message; let the\n\
2062 minibuffer contents show.")
2074 register Lisp_Object val
;
2075 val
= Fformat (nargs
, args
);
2076 /* Copy the data so that it won't move when we GC. */
2079 message_text
= (char *)xmalloc (80);
2080 message_length
= 80;
2082 if (XSTRING (val
)->size
> message_length
)
2084 message_length
= XSTRING (val
)->size
;
2085 message_text
= (char *)xrealloc (message_text
, message_length
);
2087 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
2088 message2 (message_text
, XSTRING (val
)->size
);
2093 DEFUN ("message-box", Fmessage_box
, Smessage_box
, 1, MANY
, 0,
2094 "Display a message, in a dialog box if possible.\n\
2095 If a dialog box is not available, use the echo area.\n\
2096 The first argument is a format control string, and the rest are data\n\
2097 to be formatted under control of the string. See `format' for details.\n\
2099 If the first argument is nil, clear any existing message; let the\n\
2100 minibuffer contents show.")
2112 register Lisp_Object val
;
2113 val
= Fformat (nargs
, args
);
2116 Lisp_Object pane
, menu
, obj
;
2117 struct gcpro gcpro1
;
2118 pane
= Fcons (Fcons (build_string ("OK"), Qt
), Qnil
);
2120 menu
= Fcons (val
, pane
);
2121 obj
= Fx_popup_dialog (Qt
, menu
);
2125 #else /* not HAVE_MENUS */
2126 /* Copy the data so that it won't move when we GC. */
2129 message_text
= (char *)xmalloc (80);
2130 message_length
= 80;
2132 if (XSTRING (val
)->size
> message_length
)
2134 message_length
= XSTRING (val
)->size
;
2135 message_text
= (char *)xrealloc (message_text
, message_length
);
2137 bcopy (XSTRING (val
)->data
, message_text
, XSTRING (val
)->size
);
2138 message2 (message_text
, XSTRING (val
)->size
);
2140 #endif /* not HAVE_MENUS */
2144 extern Lisp_Object last_nonmenu_event
;
2147 DEFUN ("message-or-box", Fmessage_or_box
, Smessage_or_box
, 1, MANY
, 0,
2148 "Display a message in a dialog box or in the echo area.\n\
2149 If this command was invoked with the mouse, use a dialog box.\n\
2150 Otherwise, use the echo area.\n\
2151 The first argument is a format control string, and the rest are data\n\
2152 to be formatted under control of the string. See `format' for details.\n\
2154 If the first argument is nil, clear any existing message; let the\n\
2155 minibuffer contents show.")
2161 if (NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2162 return Fmessage_box (nargs
, args
);
2164 return Fmessage (nargs
, args
);
2167 DEFUN ("current-message", Fcurrent_message
, Scurrent_message
, 0, 0, 0,
2168 "Return the string currently displayed in the echo area, or nil if none.")
2171 return (echo_area_glyphs
2172 ? make_string (echo_area_glyphs
, echo_area_glyphs_length
)
2176 DEFUN ("format", Fformat
, Sformat
, 1, MANY
, 0,
2177 "Format a string out of a control-string and arguments.\n\
2178 The first argument is a control string.\n\
2179 The other arguments are substituted into it to make the result, a string.\n\
2180 It may contain %-sequences meaning to substitute the next argument.\n\
2181 %s means print a string argument. Actually, prints any object, with `princ'.\n\
2182 %d means print as number in decimal (%o octal, %x hex).\n\
2183 %e means print a number in exponential notation.\n\
2184 %f means print a number in decimal-point notation.\n\
2185 %g means print a number in exponential notation\n\
2186 or decimal-point notation, whichever uses fewer characters.\n\
2187 %c means print a number as a single character.\n\
2188 %S means print any object as an s-expression (using prin1).\n\
2189 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2190 Use %% to put a single % into the output.")
2193 register Lisp_Object
*args
;
2195 register int n
; /* The number of the next arg to substitute */
2196 register int total
= 5; /* An estimate of the final length */
2198 register unsigned char *format
, *end
;
2200 extern char *index ();
2201 /* It should not be necessary to GCPRO ARGS, because
2202 the caller in the interpreter should take care of that. */
2204 CHECK_STRING (args
[0], 0);
2205 format
= XSTRING (args
[0])->data
;
2206 end
= format
+ XSTRING (args
[0])->size
;
2209 while (format
!= end
)
2210 if (*format
++ == '%')
2214 /* Process a numeric arg and skip it. */
2215 minlen
= atoi (format
);
2219 while ((*format
>= '0' && *format
<= '9')
2220 || *format
== '-' || *format
== ' ' || *format
== '.')
2225 else if (++n
>= nargs
)
2226 error ("Not enough arguments for format string");
2227 else if (*format
== 'S')
2229 /* For `S', prin1 the argument and then treat like a string. */
2230 register Lisp_Object tem
;
2231 tem
= Fprin1_to_string (args
[n
], Qnil
);
2235 else if (SYMBOLP (args
[n
]))
2237 XSETSTRING (args
[n
], XSYMBOL (args
[n
])->name
);
2240 else if (STRINGP (args
[n
]))
2243 if (*format
!= 's' && *format
!= 'S')
2244 error ("format specifier doesn't match argument type");
2245 total
+= XSTRING (args
[n
])->size
;
2246 /* We have to put an arbitrary limit on minlen
2247 since otherwise it could make alloca fail. */
2248 if (minlen
< XSTRING (args
[n
])->size
+ 1000)
2251 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
2252 else if (INTEGERP (args
[n
]) && *format
!= 's')
2254 #ifdef LISP_FLOAT_TYPE
2255 /* The following loop assumes the Lisp type indicates
2256 the proper way to pass the argument.
2257 So make sure we have a flonum if the argument should
2259 if (*format
== 'e' || *format
== 'f' || *format
== 'g')
2260 args
[n
] = Ffloat (args
[n
]);
2263 /* We have to put an arbitrary limit on minlen
2264 since otherwise it could make alloca fail. */
2268 #ifdef LISP_FLOAT_TYPE
2269 else if (FLOATP (args
[n
]) && *format
!= 's')
2271 if (! (*format
== 'e' || *format
== 'f' || *format
== 'g'))
2272 args
[n
] = Ftruncate (args
[n
], Qnil
);
2274 /* We have to put an arbitrary limit on minlen
2275 since otherwise it could make alloca fail. */
2282 /* Anything but a string, convert to a string using princ. */
2283 register Lisp_Object tem
;
2284 tem
= Fprin1_to_string (args
[n
], Qt
);
2291 register int nstrings
= n
+ 1;
2293 /* Allocate twice as many strings as we have %-escapes; floats occupy
2294 two slots, and we're not sure how many of those we have. */
2295 register unsigned char **strings
2296 = (unsigned char **) alloca (2 * nstrings
* sizeof (unsigned char *));
2300 for (n
= 0; n
< nstrings
; n
++)
2303 strings
[i
++] = (unsigned char *) "";
2304 else if (INTEGERP (args
[n
]))
2305 /* We checked above that the corresponding format effector
2306 isn't %s, which would cause MPV. */
2307 strings
[i
++] = (unsigned char *) XINT (args
[n
]);
2308 #ifdef LISP_FLOAT_TYPE
2309 else if (FLOATP (args
[n
]))
2311 union { double d
; char *half
[2]; } u
;
2313 u
.d
= XFLOAT (args
[n
])->data
;
2314 strings
[i
++] = (unsigned char *) u
.half
[0];
2315 strings
[i
++] = (unsigned char *) u
.half
[1];
2319 /* The first string is treated differently
2320 because it is the format string. */
2321 strings
[i
++] = XSTRING (args
[n
])->data
;
2323 strings
[i
++] = (unsigned char *) XSTRING (args
[n
]);
2326 /* Make room in result for all the non-%-codes in the control string. */
2327 total
+= XSTRING (args
[0])->size
;
2329 /* Format it in bigger and bigger buf's until it all fits. */
2332 buf
= (char *) alloca (total
+ 1);
2335 length
= doprnt_lisp (buf
, total
+ 1, strings
[0],
2336 end
, i
-1, strings
+ 1);
2337 if (buf
[total
- 1] == 0)
2345 return make_string (buf
, length
);
2351 format1 (string1
, arg0
, arg1
, arg2
, arg3
, arg4
)
2352 EMACS_INT arg0
, arg1
, arg2
, arg3
, arg4
;
2366 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, args
);
2368 doprnt (buf
, sizeof buf
, string1
, (char *)0, 5, &string1
+ 1);
2370 return build_string (buf
);
2373 DEFUN ("char-equal", Fchar_equal
, Schar_equal
, 2, 2, 0,
2374 "Return t if two characters match, optionally ignoring case.\n\
2375 Both arguments must be characters (i.e. integers).\n\
2376 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2378 register Lisp_Object c1
, c2
;
2380 CHECK_NUMBER (c1
, 0);
2381 CHECK_NUMBER (c2
, 1);
2383 if (XINT (c1
) == XINT (c2
)
2384 && (NILP (current_buffer
->case_fold_search
)
2385 || DOWNCASE (XFASTINT (c1
)) == DOWNCASE (XFASTINT (c2
))))
2390 /* Transpose the markers in two regions of the current buffer, and
2391 adjust the ones between them if necessary (i.e.: if the regions
2394 Traverses the entire marker list of the buffer to do so, adding an
2395 appropriate amount to some, subtracting from some, and leaving the
2396 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2398 It's the caller's job to see that (start1 <= end1 <= start2 <= end2). */
2401 transpose_markers (start1
, end1
, start2
, end2
)
2402 register int start1
, end1
, start2
, end2
;
2404 register int amt1
, amt2
, diff
, mpos
;
2405 register Lisp_Object marker
;
2407 /* Update point as if it were a marker. */
2411 TEMP_SET_PT (PT
+ (end2
- end1
));
2412 else if (PT
< start2
)
2413 TEMP_SET_PT (PT
+ (end2
- start2
) - (end1
- start1
));
2415 TEMP_SET_PT (PT
- (start2
- start1
));
2417 /* We used to adjust the endpoints here to account for the gap, but that
2418 isn't good enough. Even if we assume the caller has tried to move the
2419 gap out of our way, it might still be at start1 exactly, for example;
2420 and that places it `inside' the interval, for our purposes. The amount
2421 of adjustment is nontrivial if there's a `denormalized' marker whose
2422 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2423 the dirty work to Fmarker_position, below. */
2425 /* The difference between the region's lengths */
2426 diff
= (end2
- start2
) - (end1
- start1
);
2428 /* For shifting each marker in a region by the length of the other
2429 * region plus the distance between the regions.
2431 amt1
= (end2
- start2
) + (start2
- end1
);
2432 amt2
= (end1
- start1
) + (start2
- end1
);
2434 for (marker
= BUF_MARKERS (current_buffer
); !NILP (marker
);
2435 marker
= XMARKER (marker
)->chain
)
2437 mpos
= marker_position (marker
);
2438 if (mpos
>= start1
&& mpos
< end2
)
2442 else if (mpos
< start2
)
2446 if (mpos
> GPT
) mpos
+= GAP_SIZE
;
2447 XMARKER (marker
)->bufpos
= mpos
;
2452 DEFUN ("transpose-regions", Ftranspose_regions
, Stranspose_regions
, 4, 5, 0,
2453 "Transpose region START1 to END1 with START2 to END2.\n\
2454 The regions may not be overlapping, because the size of the buffer is\n\
2455 never changed in a transposition.\n\
2457 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't transpose\n\
2458 any markers that happen to be located in the regions.\n\
2460 Transposing beyond buffer boundaries is an error.")
2461 (startr1
, endr1
, startr2
, endr2
, leave_markers
)
2462 Lisp_Object startr1
, endr1
, startr2
, endr2
, leave_markers
;
2464 register int start1
, end1
, start2
, end2
,
2465 gap
, len1
, len_mid
, len2
;
2466 unsigned char *start1_addr
, *start2_addr
, *temp
;
2468 #ifdef USE_TEXT_PROPERTIES
2469 INTERVAL cur_intv
, tmp_interval1
, tmp_interval_mid
, tmp_interval2
;
2470 cur_intv
= BUF_INTERVALS (current_buffer
);
2471 #endif /* USE_TEXT_PROPERTIES */
2473 validate_region (&startr1
, &endr1
);
2474 validate_region (&startr2
, &endr2
);
2476 start1
= XFASTINT (startr1
);
2477 end1
= XFASTINT (endr1
);
2478 start2
= XFASTINT (startr2
);
2479 end2
= XFASTINT (endr2
);
2482 /* Swap the regions if they're reversed. */
2485 register int glumph
= start1
;
2493 len1
= end1
- start1
;
2494 len2
= end2
- start2
;
2497 error ("transposed regions not properly ordered");
2498 else if (start1
== end1
|| start2
== end2
)
2499 error ("transposed region may not be of length 0");
2501 /* The possibilities are:
2502 1. Adjacent (contiguous) regions, or separate but equal regions
2503 (no, really equal, in this case!), or
2504 2. Separate regions of unequal size.
2506 The worst case is usually No. 2. It means that (aside from
2507 potential need for getting the gap out of the way), there also
2508 needs to be a shifting of the text between the two regions. So
2509 if they are spread far apart, we are that much slower... sigh. */
2511 /* It must be pointed out that the really studly thing to do would
2512 be not to move the gap at all, but to leave it in place and work
2513 around it if necessary. This would be extremely efficient,
2514 especially considering that people are likely to do
2515 transpositions near where they are working interactively, which
2516 is exactly where the gap would be found. However, such code
2517 would be much harder to write and to read. So, if you are
2518 reading this comment and are feeling squirrely, by all means have
2519 a go! I just didn't feel like doing it, so I will simply move
2520 the gap the minimum distance to get it out of the way, and then
2521 deal with an unbroken array. */
2523 /* Make sure the gap won't interfere, by moving it out of the text
2524 we will operate on. */
2525 if (start1
< gap
&& gap
< end2
)
2527 if (gap
- start1
< end2
- gap
)
2533 /* Hmmm... how about checking to see if the gap is large
2534 enough to use as the temporary storage? That would avoid an
2535 allocation... interesting. Later, don't fool with it now. */
2537 /* Working without memmove, for portability (sigh), so must be
2538 careful of overlapping subsections of the array... */
2540 if (end1
== start2
) /* adjacent regions */
2542 modify_region (current_buffer
, start1
, end2
);
2543 record_change (start1
, len1
+ len2
);
2545 #ifdef USE_TEXT_PROPERTIES
2546 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2547 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2548 Fset_text_properties (make_number (start1
), make_number (end2
),
2550 #endif /* USE_TEXT_PROPERTIES */
2552 /* First region smaller than second. */
2555 /* We use alloca only if it is small,
2556 because we want to avoid stack overflow. */
2558 temp
= (unsigned char *) xmalloc (len2
);
2560 temp
= (unsigned char *) alloca (len2
);
2562 /* Don't precompute these addresses. We have to compute them
2563 at the last minute, because the relocating allocator might
2564 have moved the buffer around during the xmalloc. */
2565 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2566 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2568 bcopy (start2_addr
, temp
, len2
);
2569 bcopy (start1_addr
, start1_addr
+ len2
, len1
);
2570 bcopy (temp
, start1_addr
, len2
);
2575 /* First region not smaller than second. */
2578 temp
= (unsigned char *) xmalloc (len1
);
2580 temp
= (unsigned char *) alloca (len1
);
2581 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2582 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2583 bcopy (start1_addr
, temp
, len1
);
2584 bcopy (start2_addr
, start1_addr
, len2
);
2585 bcopy (temp
, start1_addr
+ len2
, len1
);
2589 #ifdef USE_TEXT_PROPERTIES
2590 graft_intervals_into_buffer (tmp_interval1
, start1
+ len2
,
2591 len1
, current_buffer
, 0);
2592 graft_intervals_into_buffer (tmp_interval2
, start1
,
2593 len2
, current_buffer
, 0);
2594 #endif /* USE_TEXT_PROPERTIES */
2596 /* Non-adjacent regions, because end1 != start2, bleagh... */
2600 /* Regions are same size, though, how nice. */
2602 modify_region (current_buffer
, start1
, end1
);
2603 modify_region (current_buffer
, start2
, end2
);
2604 record_change (start1
, len1
);
2605 record_change (start2
, len2
);
2606 #ifdef USE_TEXT_PROPERTIES
2607 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2608 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2609 Fset_text_properties (make_number (start1
), make_number (end1
),
2611 Fset_text_properties (make_number (start2
), make_number (end2
),
2613 #endif /* USE_TEXT_PROPERTIES */
2616 temp
= (unsigned char *) xmalloc (len1
);
2618 temp
= (unsigned char *) alloca (len1
);
2619 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2620 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2621 bcopy (start1_addr
, temp
, len1
);
2622 bcopy (start2_addr
, start1_addr
, len2
);
2623 bcopy (temp
, start2_addr
, len1
);
2626 #ifdef USE_TEXT_PROPERTIES
2627 graft_intervals_into_buffer (tmp_interval1
, start2
,
2628 len1
, current_buffer
, 0);
2629 graft_intervals_into_buffer (tmp_interval2
, start1
,
2630 len2
, current_buffer
, 0);
2631 #endif /* USE_TEXT_PROPERTIES */
2634 else if (len1
< len2
) /* Second region larger than first */
2635 /* Non-adjacent & unequal size, area between must also be shifted. */
2637 len_mid
= start2
- end1
;
2638 modify_region (current_buffer
, start1
, end2
);
2639 record_change (start1
, (end2
- start1
));
2640 #ifdef USE_TEXT_PROPERTIES
2641 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2642 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2643 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2644 Fset_text_properties (make_number (start1
), make_number (end2
),
2646 #endif /* USE_TEXT_PROPERTIES */
2648 /* holds region 2 */
2650 temp
= (unsigned char *) xmalloc (len2
);
2652 temp
= (unsigned char *) alloca (len2
);
2653 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2654 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2655 bcopy (start2_addr
, temp
, len2
);
2656 bcopy (start1_addr
, start1_addr
+ len_mid
+ len2
, len1
);
2657 safe_bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2658 bcopy (temp
, start1_addr
, len2
);
2661 #ifdef USE_TEXT_PROPERTIES
2662 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2663 len1
, current_buffer
, 0);
2664 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2665 len_mid
, current_buffer
, 0);
2666 graft_intervals_into_buffer (tmp_interval2
, start1
,
2667 len2
, current_buffer
, 0);
2668 #endif /* USE_TEXT_PROPERTIES */
2671 /* Second region smaller than first. */
2673 len_mid
= start2
- end1
;
2674 record_change (start1
, (end2
- start1
));
2675 modify_region (current_buffer
, start1
, end2
);
2677 #ifdef USE_TEXT_PROPERTIES
2678 tmp_interval1
= copy_intervals (cur_intv
, start1
, len1
);
2679 tmp_interval_mid
= copy_intervals (cur_intv
, end1
, len_mid
);
2680 tmp_interval2
= copy_intervals (cur_intv
, start2
, len2
);
2681 Fset_text_properties (make_number (start1
), make_number (end2
),
2683 #endif /* USE_TEXT_PROPERTIES */
2685 /* holds region 1 */
2687 temp
= (unsigned char *) xmalloc (len1
);
2689 temp
= (unsigned char *) alloca (len1
);
2690 start1_addr
= BUF_CHAR_ADDRESS (current_buffer
, start1
);
2691 start2_addr
= BUF_CHAR_ADDRESS (current_buffer
, start2
);
2692 bcopy (start1_addr
, temp
, len1
);
2693 bcopy (start2_addr
, start1_addr
, len2
);
2694 bcopy (start1_addr
+ len1
, start1_addr
+ len2
, len_mid
);
2695 bcopy (temp
, start1_addr
+ len2
+ len_mid
, len1
);
2698 #ifdef USE_TEXT_PROPERTIES
2699 graft_intervals_into_buffer (tmp_interval1
, end2
- len1
,
2700 len1
, current_buffer
, 0);
2701 graft_intervals_into_buffer (tmp_interval_mid
, start1
+ len2
,
2702 len_mid
, current_buffer
, 0);
2703 graft_intervals_into_buffer (tmp_interval2
, start1
,
2704 len2
, current_buffer
, 0);
2705 #endif /* USE_TEXT_PROPERTIES */
2709 /* todo: this will be slow, because for every transposition, we
2710 traverse the whole friggin marker list. Possible solutions:
2711 somehow get a list of *all* the markers across multiple
2712 transpositions and do it all in one swell phoop. Or maybe modify
2713 Emacs' marker code to keep an ordered list or tree. This might
2714 be nicer, and more beneficial in the long run, but would be a
2715 bunch of work. Plus the way they're arranged now is nice. */
2716 if (NILP (leave_markers
))
2718 transpose_markers (start1
, end1
, start2
, end2
);
2719 fix_overlays_in_range (start1
, end2
);
2731 Qbuffer_access_fontify_functions
2732 = intern ("buffer-access-fontify-functions");
2733 staticpro (&Qbuffer_access_fontify_functions
);
2735 DEFVAR_LISP ("buffer-access-fontify-functions",
2736 &Vbuffer_access_fontify_functions
,
2737 "List of functions called by `buffer-substring' to fontify if necessary.\n\
2738 Each function is called with two arguments which specify the range\n\
2739 of the buffer being accessed.");
2740 Vbuffer_access_fontify_functions
= Qnil
;
2744 extern Lisp_Object Vprin1_to_string_buffer
;
2745 obuf
= Fcurrent_buffer ();
2746 /* Do this here, because init_buffer_once is too early--it won't work. */
2747 Fset_buffer (Vprin1_to_string_buffer
);
2748 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
2749 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
2754 DEFVAR_LISP ("buffer-access-fontified-property",
2755 &Vbuffer_access_fontified_property
,
2756 "Property which (if non-nil) indicates text has been fontified.\n\
2757 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
2758 functions if all the text being accessed has this property.");
2759 Vbuffer_access_fontified_property
= Qnil
;
2761 DEFVAR_LISP ("system-name", &Vsystem_name
,
2762 "The name of the machine Emacs is running on.");
2764 DEFVAR_LISP ("user-full-name", &Vuser_full_name
,
2765 "The full name of the user logged in.");
2767 DEFVAR_LISP ("user-login-name", &Vuser_login_name
,
2768 "The user's name, taken from environment variables if possible.");
2770 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name
,
2771 "The user's name, based upon the real uid only.");
2773 defsubr (&Schar_equal
);
2774 defsubr (&Sgoto_char
);
2775 defsubr (&Sstring_to_char
);
2776 defsubr (&Schar_to_string
);
2778 defsubr (&Sbuffer_substring
);
2779 defsubr (&Sbuffer_substring_no_properties
);
2780 defsubr (&Sbuffer_string
);
2782 defsubr (&Spoint_marker
);
2783 defsubr (&Smark_marker
);
2785 defsubr (&Sregion_beginning
);
2786 defsubr (&Sregion_end
);
2787 /* defsubr (&Smark); */
2788 /* defsubr (&Sset_mark); */
2789 defsubr (&Ssave_excursion
);
2790 defsubr (&Ssave_current_buffer
);
2792 defsubr (&Sbufsize
);
2793 defsubr (&Spoint_max
);
2794 defsubr (&Spoint_min
);
2795 defsubr (&Spoint_min_marker
);
2796 defsubr (&Spoint_max_marker
);
2798 defsubr (&Sline_beginning_position
);
2799 defsubr (&Sline_end_position
);
2805 defsubr (&Sfollowing_char
);
2806 defsubr (&Sprevious_char
);
2807 defsubr (&Schar_after
);
2808 defsubr (&Schar_before
);
2810 defsubr (&Sinsert_before_markers
);
2811 defsubr (&Sinsert_and_inherit
);
2812 defsubr (&Sinsert_and_inherit_before_markers
);
2813 defsubr (&Sinsert_char
);
2815 defsubr (&Suser_login_name
);
2816 defsubr (&Suser_real_login_name
);
2817 defsubr (&Suser_uid
);
2818 defsubr (&Suser_real_uid
);
2819 defsubr (&Suser_full_name
);
2820 defsubr (&Semacs_pid
);
2821 defsubr (&Scurrent_time
);
2822 defsubr (&Sformat_time_string
);
2823 defsubr (&Sdecode_time
);
2824 defsubr (&Sencode_time
);
2825 defsubr (&Scurrent_time_string
);
2826 defsubr (&Scurrent_time_zone
);
2827 defsubr (&Sset_time_zone_rule
);
2828 defsubr (&Ssystem_name
);
2829 defsubr (&Smessage
);
2830 defsubr (&Smessage_box
);
2831 defsubr (&Smessage_or_box
);
2832 defsubr (&Scurrent_message
);
2835 defsubr (&Sinsert_buffer_substring
);
2836 defsubr (&Scompare_buffer_substrings
);
2837 defsubr (&Ssubst_char_in_region
);
2838 defsubr (&Stranslate_region
);
2839 defsubr (&Sdelete_region
);
2841 defsubr (&Snarrow_to_region
);
2842 defsubr (&Ssave_restriction
);
2843 defsubr (&Stranspose_regions
);