Fix -Wimplicit warnings.
[bpt/emacs.git] / src / editfns.c
CommitLineData
35692fe0 1/* Lisp functions pertaining to editing.
31c8f881 2 Copyright (C) 1985,86,87,89,93,94,95,96,97,98 Free Software Foundation, Inc.
35692fe0
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
7c938215 8the Free Software Foundation; either version 2, or (at your option)
35692fe0
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
5a7670bf
RS
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
35692fe0
JB
20
21
738429d1
JB
22#include <sys/types.h>
23
18160b98 24#include <config.h>
bfb61299
JB
25
26#ifdef VMS
956ace37 27#include "vms-pwd.h"
bfb61299 28#else
35692fe0 29#include <pwd.h>
bfb61299
JB
30#endif
31
dfcf069d
AS
32#ifdef STDC_HEADERS
33#include <stdlib.h>
34#endif
35
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
39
35692fe0 40#include "lisp.h"
74d6d8c5 41#include "intervals.h"
35692fe0 42#include "buffer.h"
fb8106e8 43#include "charset.h"
35692fe0
JB
44#include "window.h"
45
956ace37 46#include "systime.h"
35692fe0
JB
47
48#define min(a, b) ((a) < (b) ? (a) : (b))
49#define max(a, b) ((a) > (b) ? (a) : (b))
50
a03eaf1c
RS
51#ifndef NULL
52#define NULL 0
53#endif
54
c59b5089
PE
55extern char **environ;
56extern Lisp_Object make_time ();
b1b0ee5a 57extern void insert_from_buffer ();
94751666 58static int tm_diff ();
260e2e2a 59static void update_buffer_properties ();
b771d0da 60size_t emacs_strftime ();
a92ae0ce 61void set_time_zone_rule ();
260e2e2a
KH
62
63Lisp_Object Vbuffer_access_fontify_functions;
64Lisp_Object Qbuffer_access_fontify_functions;
65Lisp_Object Vbuffer_access_fontified_property;
b1b0ee5a 66
e3ed8469
KH
67Lisp_Object Fuser_full_name ();
68
35692fe0
JB
69/* Some static data, and a function to initialize it for each run */
70
71Lisp_Object Vsystem_name;
35b34f72
KH
72Lisp_Object Vuser_real_login_name; /* login name of current user ID */
73Lisp_Object Vuser_full_name; /* full name of current user */
74Lisp_Object Vuser_login_name; /* user name from LOGNAME or USER */
35692fe0
JB
75
76void
77init_editfns ()
78{
52b14ac0 79 char *user_name;
35692fe0
JB
80 register unsigned char *p, *q, *r;
81 struct passwd *pw; /* password entry for the current user */
35692fe0
JB
82 Lisp_Object tem;
83
84 /* Set up system_name even when dumping. */
ac988277 85 init_system_name ();
35692fe0
JB
86
87#ifndef CANNOT_DUMP
88 /* Don't bother with this on initial start when just dumping out */
89 if (!initialized)
90 return;
91#endif /* not CANNOT_DUMP */
92
93 pw = (struct passwd *) getpwuid (getuid ());
87485d6f
MW
94#ifdef MSDOS
95 /* We let the real user name default to "root" because that's quite
96 accurate on MSDOG and because it lets Emacs find the init file.
97 (The DVX libraries override the Djgpp libraries here.) */
35b34f72 98 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
87485d6f 99#else
35b34f72 100 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
87485d6f 101#endif
35692fe0 102
52b14ac0
JB
103 /* Get the effective user name, by consulting environment variables,
104 or the effective uid if those are unset. */
2c9ae24e 105 user_name = (char *) getenv ("LOGNAME");
35692fe0 106 if (!user_name)
4691c06d
RS
107#ifdef WINDOWSNT
108 user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
109#else /* WINDOWSNT */
2c9ae24e 110 user_name = (char *) getenv ("USER");
4691c06d 111#endif /* WINDOWSNT */
52b14ac0
JB
112 if (!user_name)
113 {
114 pw = (struct passwd *) getpwuid (geteuid ());
115 user_name = (char *) (pw ? pw->pw_name : "unknown");
116 }
35b34f72 117 Vuser_login_name = build_string (user_name);
35692fe0 118
52b14ac0
JB
119 /* If the user name claimed in the environment vars differs from
120 the real uid, use the claimed name to find the full name. */
35b34f72 121 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
3415b0e9
RS
122 Vuser_full_name = Fuser_full_name (NILP (tem)? make_number (geteuid())
123 : Vuser_login_name);
35692fe0 124
8f1e2d16 125 p = (unsigned char *) getenv ("NAME");
9d36d071
RS
126 if (p)
127 Vuser_full_name = build_string (p);
3347526c
RS
128 else if (NILP (Vuser_full_name))
129 Vuser_full_name = build_string ("unknown");
35692fe0
JB
130}
131\f
132DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
5f75e666 133 "Convert arg CHAR to a string containing that character.")
2591ec64
EN
134 (character)
135 Lisp_Object character;
35692fe0 136{
fb8106e8 137 int len;
d11ba98c 138 unsigned char workbuf[4], *str;
fb8106e8 139
2591ec64 140 CHECK_NUMBER (character, 0);
35692fe0 141
fb8106e8 142 len = CHAR_STRING (XFASTINT (character), workbuf, str);
5f75e666 143 return make_string_from_bytes (str, 1, len);
35692fe0
JB
144}
145
146DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
fb8106e8
KH
147 "Convert arg STRING to a character, the first character of that string.\n\
148A multibyte character is handled correctly.")
2591ec64
EN
149 (string)
150 register Lisp_Object string;
35692fe0
JB
151{
152 register Lisp_Object val;
153 register struct Lisp_String *p;
2591ec64 154 CHECK_STRING (string, 0);
2591ec64 155 p = XSTRING (string);
35692fe0 156 if (p->size)
dc3620af 157 XSETFASTINT (val, STRING_CHAR (p->data, STRING_BYTES (p)));
35692fe0 158 else
55561c63 159 XSETFASTINT (val, 0);
35692fe0
JB
160 return val;
161}
162\f
163static Lisp_Object
ec1c14f6
RS
164buildmark (charpos, bytepos)
165 int charpos, bytepos;
35692fe0
JB
166{
167 register Lisp_Object mark;
168 mark = Fmake_marker ();
ec1c14f6 169 set_marker_both (mark, Qnil, charpos, bytepos);
35692fe0
JB
170 return mark;
171}
172
173DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
174 "Return value of point, as an integer.\n\
175Beginning of buffer is position (point-min)")
176 ()
177{
178 Lisp_Object temp;
6ec8bbd2 179 XSETFASTINT (temp, PT);
35692fe0
JB
180 return temp;
181}
182
183DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
184 "Return value of point, as a marker object.")
185 ()
186{
ec1c14f6 187 return buildmark (PT, PT_BYTE);
35692fe0
JB
188}
189
190int
191clip_to_bounds (lower, num, upper)
192 int lower, num, upper;
193{
194 if (num < lower)
195 return lower;
196 else if (num > upper)
197 return upper;
198 else
199 return num;
200}
201
202DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
203 "Set point to POSITION, a number or marker.\n\
fb8106e8
KH
204Beginning of buffer is position (point-min), end is (point-max).\n\
205If the position is in the middle of a multibyte form,\n\
206the actual point is set at the head of the multibyte form\n\
207except in the case that `enable-multibyte-characters' is nil.")
2591ec64
EN
208 (position)
209 register Lisp_Object position;
35692fe0 210{
fb8106e8
KH
211 int pos;
212 unsigned char *p;
213
72ef82ec
RS
214 if (MARKERP (position)
215 && current_buffer == XMARKER (position)->buffer)
ec1c14f6
RS
216 {
217 pos = marker_position (position);
218 if (pos < BEGV)
219 SET_PT_BOTH (BEGV, BEGV_BYTE);
220 else if (pos > ZV)
221 SET_PT_BOTH (ZV, ZV_BYTE);
222 else
223 SET_PT_BOTH (pos, marker_byte_position (position));
224
225 return position;
226 }
227
2591ec64 228 CHECK_NUMBER_COERCE_MARKER (position, 0);
35692fe0 229
fb8106e8 230 pos = clip_to_bounds (BEGV, XINT (position), ZV);
fb8106e8 231 SET_PT (pos);
2591ec64 232 return position;
35692fe0
JB
233}
234
235static Lisp_Object
236region_limit (beginningp)
237 int beginningp;
238{
646d9d18 239 extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */
35692fe0 240 register Lisp_Object m;
c9dd14e1
RM
241 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
242 && NILP (current_buffer->mark_active))
243 Fsignal (Qmark_inactive, Qnil);
35692fe0 244 m = Fmarker_position (current_buffer->mark);
56a98455 245 if (NILP (m)) error ("There is no region now");
6ec8bbd2
KH
246 if ((PT < XFASTINT (m)) == beginningp)
247 return (make_number (PT));
35692fe0
JB
248 else
249 return (m);
250}
251
252DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
253 "Return position of beginning of region, as an integer.")
254 ()
255{
256 return (region_limit (1));
257}
258
259DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
260 "Return position of end of region, as an integer.")
261 ()
262{
263 return (region_limit (0));
264}
265
35692fe0
JB
266DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
267 "Return this buffer's mark, as a marker object.\n\
268Watch out! Moving this marker changes the mark position.\n\
269If you set the marker not to point anywhere, the buffer will have no mark.")
270 ()
271{
272 return current_buffer->mark;
273}
c9ed721d
RS
274\f
275DEFUN ("line-beginning-position", Fline_beginning_position, Sline_beginning_position,
276 0, 1, 0,
277 "Return the character position of the first character on the current line.\n\
278With argument N not nil or 1, move forward N - 1 lines first.\n\
279If scan reaches end of buffer, return that position.\n\
280This function does not move point.")
281 (n)
282 Lisp_Object n;
283{
ec1c14f6 284 register int orig, orig_byte, end;
c9ed721d
RS
285
286 if (NILP (n))
287 XSETFASTINT (n, 1);
288 else
289 CHECK_NUMBER (n, 0);
290
291 orig = PT;
ec1c14f6 292 orig_byte = PT_BYTE;
c9ed721d
RS
293 Fforward_line (make_number (XINT (n) - 1));
294 end = PT;
ec1c14f6 295 SET_PT_BOTH (orig, orig_byte);
35692fe0 296
c9ed721d
RS
297 return make_number (end);
298}
299
300DEFUN ("line-end-position", Fline_end_position, Sline_end_position,
301 0, 1, 0,
302 "Return the character position of the last character on the current line.\n\
303With argument N not nil or 1, move forward N - 1 lines first.\n\
304If scan reaches end of buffer, return that position.\n\
305This function does not move point.")
306 (n)
307 Lisp_Object n;
308{
309 if (NILP (n))
310 XSETFASTINT (n, 1);
311 else
312 CHECK_NUMBER (n, 0);
313
314 return make_number (find_before_next_newline
315 (PT, 0, XINT (n) - (XINT (n) <= 0)));
316}
317\f
35692fe0
JB
318Lisp_Object
319save_excursion_save ()
320{
0e2c9c70
JB
321 register int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
322 == current_buffer);
35692fe0
JB
323
324 return Fcons (Fpoint_marker (),
aea4a109 325 Fcons (Fcopy_marker (current_buffer->mark, Qnil),
9772455e
RS
326 Fcons (visible ? Qt : Qnil,
327 current_buffer->mark_active)));
35692fe0
JB
328}
329
330Lisp_Object
331save_excursion_restore (info)
4ad8681a 332 Lisp_Object info;
35692fe0 333{
4ad8681a
RS
334 Lisp_Object tem, tem1, omark, nmark;
335 struct gcpro gcpro1, gcpro2, gcpro3;
35692fe0
JB
336
337 tem = Fmarker_buffer (Fcar (info));
338 /* If buffer being returned to is now deleted, avoid error */
339 /* Otherwise could get error here while unwinding to top level
340 and crash */
341 /* In that case, Fmarker_buffer returns nil now. */
56a98455 342 if (NILP (tem))
35692fe0 343 return Qnil;
4ad8681a
RS
344
345 omark = nmark = Qnil;
346 GCPRO3 (info, omark, nmark);
347
35692fe0
JB
348 Fset_buffer (tem);
349 tem = Fcar (info);
350 Fgoto_char (tem);
351 unchain_marker (tem);
352 tem = Fcar (Fcdr (info));
03d18690 353 omark = Fmarker_position (current_buffer->mark);
35692fe0 354 Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
03d18690 355 nmark = Fmarker_position (tem);
35692fe0
JB
356 unchain_marker (tem);
357 tem = Fcdr (Fcdr (info));
ef580991
RS
358#if 0 /* We used to make the current buffer visible in the selected window
359 if that was true previously. That avoids some anomalies.
360 But it creates others, and it wasn't documented, and it is simpler
361 and cleaner never to alter the window/buffer connections. */
9772455e
RS
362 tem1 = Fcar (tem);
363 if (!NILP (tem1)
0e2c9c70 364 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
35692fe0 365 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
ef580991 366#endif /* 0 */
9772455e
RS
367
368 tem1 = current_buffer->mark_active;
369 current_buffer->mark_active = Fcdr (tem);
9fed2b18
RS
370 if (!NILP (Vrun_hooks))
371 {
03d18690
RS
372 /* If mark is active now, and either was not active
373 or was at a different place, run the activate hook. */
9fed2b18 374 if (! NILP (current_buffer->mark_active))
03d18690
RS
375 {
376 if (! EQ (omark, nmark))
377 call1 (Vrun_hooks, intern ("activate-mark-hook"));
378 }
379 /* If mark has ceased to be active, run deactivate hook. */
9fed2b18
RS
380 else if (! NILP (tem1))
381 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
382 }
4ad8681a 383 UNGCPRO;
35692fe0
JB
384 return Qnil;
385}
386
387DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
388 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
389Executes BODY just like `progn'.\n\
390The values of point, mark and the current buffer are restored\n\
9772455e 391even in case of abnormal exit (throw or error).\n\
85cac557
RS
392The state of activation of the mark is also restored.\n\
393\n\
394This construct does not save `deactivate-mark', and therefore\n\
395functions that change the buffer will still cause deactivation\n\
396of the mark at the end of the command. To prevent that, bind\n\
397`deactivate-mark' with `let'.")
35692fe0
JB
398 (args)
399 Lisp_Object args;
400{
401 register Lisp_Object val;
402 int count = specpdl_ptr - specpdl;
403
404 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4bc8c7d2
RS
405
406 val = Fprogn (args);
407 return unbind_to (count, val);
408}
409
410DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0,
411 "Save the current buffer; execute BODY; restore the current buffer.\n\
412Executes BODY just like `progn'.")
413 (args)
414 Lisp_Object args;
415{
416 register Lisp_Object val;
417 int count = specpdl_ptr - specpdl;
418
cb5e5f74 419 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
4bc8c7d2 420
35692fe0
JB
421 val = Fprogn (args);
422 return unbind_to (count, val);
423}
424\f
425DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 0, 0,
426 "Return the number of characters in the current buffer.")
427 ()
428{
429 Lisp_Object temp;
55561c63 430 XSETFASTINT (temp, Z - BEG);
35692fe0
JB
431 return temp;
432}
433
434DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
435 "Return the minimum permissible value of point in the current buffer.\n\
4c390850 436This is 1, unless narrowing (a buffer restriction) is in effect.")
35692fe0
JB
437 ()
438{
439 Lisp_Object temp;
55561c63 440 XSETFASTINT (temp, BEGV);
35692fe0
JB
441 return temp;
442}
443
444DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
445 "Return a marker to the minimum permissible value of point in this buffer.\n\
4c390850 446This is the beginning, unless narrowing (a buffer restriction) is in effect.")
35692fe0
JB
447 ()
448{
ec1c14f6 449 return buildmark (BEGV, BEGV_BYTE);
35692fe0
JB
450}
451
452DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
453 "Return the maximum permissible value of point in the current buffer.\n\
4c390850
RS
454This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
455is in effect, in which case it is less.")
35692fe0
JB
456 ()
457{
458 Lisp_Object temp;
55561c63 459 XSETFASTINT (temp, ZV);
35692fe0
JB
460 return temp;
461}
462
463DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
464 "Return a marker to the maximum permissible value of point in this buffer.\n\
4c390850
RS
465This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
466is in effect, in which case it is less.")
35692fe0
JB
467 ()
468{
ec1c14f6 469 return buildmark (ZV, ZV_BYTE);
35692fe0
JB
470}
471
7df74da6
RS
472DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
473 "Return the byte position for character position POSITION.")
474 (position)
80e01f8d 475 Lisp_Object position;
7df74da6 476{
fa8a5a15
KH
477 CHECK_NUMBER_COERCE_MARKER (position, 1);
478 return make_number (CHAR_TO_BYTE (XINT (position)));
7df74da6
RS
479}
480\f
850a8179
JB
481DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
482 "Return the character following point, as a number.\n\
fb8106e8
KH
483At the end of the buffer or accessible region, return 0.\n\
484If `enable-multibyte-characters' is nil or point is not\n\
485 at character boundary, multibyte form is ignored,\n\
486 and only one byte following point is returned as a character.")
35692fe0
JB
487 ()
488{
489 Lisp_Object temp;
6ec8bbd2 490 if (PT >= ZV)
55561c63 491 XSETFASTINT (temp, 0);
850a8179 492 else
ec1c14f6 493 XSETFASTINT (temp, FETCH_CHAR (PT_BYTE));
35692fe0
JB
494 return temp;
495}
496
850a8179
JB
497DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
498 "Return the character preceding point, as a number.\n\
fb8106e8
KH
499At the beginning of the buffer or accessible region, return 0.\n\
500If `enable-multibyte-characters' is nil or point is not\n\
501 at character boundary, multi-byte form is ignored,\n\
502 and only one byte preceding point is returned as a character.")
35692fe0
JB
503 ()
504{
505 Lisp_Object temp;
6ec8bbd2 506 if (PT <= BEGV)
55561c63 507 XSETFASTINT (temp, 0);
fb8106e8
KH
508 else if (!NILP (current_buffer->enable_multibyte_characters))
509 {
ec1c14f6 510 int pos = PT_BYTE;
fb8106e8
KH
511 DEC_POS (pos);
512 XSETFASTINT (temp, FETCH_CHAR (pos));
513 }
35692fe0 514 else
ec1c14f6 515 XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1));
35692fe0
JB
516 return temp;
517}
518
519DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
ec1c14f6 520 "Return t if point is at the beginning of the buffer.\n\
35692fe0
JB
521If the buffer is narrowed, this means the beginning of the narrowed part.")
522 ()
523{
6ec8bbd2 524 if (PT == BEGV)
35692fe0
JB
525 return Qt;
526 return Qnil;
527}
528
529DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
ec1c14f6 530 "Return t if point is at the end of the buffer.\n\
35692fe0
JB
531If the buffer is narrowed, this means the end of the narrowed part.")
532 ()
533{
6ec8bbd2 534 if (PT == ZV)
35692fe0
JB
535 return Qt;
536 return Qnil;
537}
538
539DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
ec1c14f6 540 "Return t if point is at the beginning of a line.")
35692fe0
JB
541 ()
542{
ec1c14f6 543 if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
35692fe0
JB
544 return Qt;
545 return Qnil;
546}
547
548DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
ec1c14f6 549 "Return t if point is at the end of a line.\n\
35692fe0
JB
550`End of a line' includes point being at the end of the buffer.")
551 ()
552{
ec1c14f6 553 if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
35692fe0
JB
554 return Qt;
555 return Qnil;
556}
557
fa1d3816 558DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
35692fe0
JB
559 "Return character in current buffer at position POS.\n\
560POS is an integer or a buffer pointer.\n\
fb8106e8
KH
561If POS is out of range, the value is nil.\n\
562If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
563 multi-byte form is ignored, and only one byte at POS\n\
564 is returned as a character.")
35692fe0
JB
565 (pos)
566 Lisp_Object pos;
567{
ec1c14f6 568 register int pos_byte;
35692fe0 569 register Lisp_Object val;
35692fe0 570
fa1d3816 571 if (NILP (pos))
85cac557
RS
572 pos_byte = PT_BYTE;
573 else if (MARKERP (pos))
574 {
575 pos_byte = marker_byte_position (pos);
576 if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE)
577 return Qnil;
578 }
fa1d3816
RS
579 else
580 {
581 CHECK_NUMBER_COERCE_MARKER (pos, 0);
85cac557
RS
582 if (pos < BEGV || pos >= ZV)
583 return Qnil;
ec1c14f6
RS
584
585 pos_byte = CHAR_TO_BYTE (XINT (pos));
fa1d3816 586 }
35692fe0 587
ec1c14f6 588 return make_number (FETCH_CHAR (pos_byte));
35692fe0 589}
fb8106e8 590
fa1d3816 591DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0,
fb8106e8
KH
592 "Return character in current buffer preceding position POS.\n\
593POS is an integer or a buffer pointer.\n\
594If POS is out of range, the value is nil.\n\
595If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\
596multi-byte form is ignored, and only one byte preceding POS\n\
597is returned as a character.")
598 (pos)
599 Lisp_Object pos;
600{
601 register Lisp_Object val;
ec1c14f6 602 register int pos_byte;
fb8106e8 603
fa1d3816 604 if (NILP (pos))
ec1c14f6
RS
605 pos_byte = PT_BYTE;
606 else if (MARKERP (pos))
85cac557
RS
607 {
608 pos_byte = marker_byte_position (pos);
609
610 if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE)
611 return Qnil;
612 }
fa1d3816
RS
613 else
614 {
615 CHECK_NUMBER_COERCE_MARKER (pos, 0);
fb8106e8 616
85cac557
RS
617 if (pos <= BEGV || pos > ZV)
618 return Qnil;
619
ec1c14f6 620 pos_byte = CHAR_TO_BYTE (XINT (pos));
fa1d3816 621 }
fb8106e8
KH
622
623 if (!NILP (current_buffer->enable_multibyte_characters))
624 {
ec1c14f6
RS
625 DEC_POS (pos_byte);
626 XSETFASTINT (val, FETCH_CHAR (pos_byte));
fb8106e8
KH
627 }
628 else
629 {
ec1c14f6
RS
630 pos_byte--;
631 XSETFASTINT (val, FETCH_BYTE (pos_byte));
fb8106e8
KH
632 }
633 return val;
634}
35692fe0 635\f
87485d6f 636DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
35692fe0
JB
637 "Return the name under which the user logged in, as a string.\n\
638This is based on the effective uid, not the real uid.\n\
2c9ae24e 639Also, if the environment variable LOGNAME or USER is set,\n\
87485d6f
MW
640that determines the value of this function.\n\n\
641If optional argument UID is an integer, return the login name of the user\n\
642with that uid, or nil if there is no such user.")
643 (uid)
644 Lisp_Object uid;
35692fe0 645{
87485d6f
MW
646 struct passwd *pw;
647
f8a0e364
RS
648 /* Set up the user name info if we didn't do it before.
649 (That can happen if Emacs is dumpable
650 but you decide to run `temacs -l loadup' and not dump. */
35b34f72 651 if (INTEGERP (Vuser_login_name))
f8a0e364 652 init_editfns ();
87485d6f
MW
653
654 if (NILP (uid))
35b34f72 655 return Vuser_login_name;
87485d6f
MW
656
657 CHECK_NUMBER (uid, 0);
658 pw = (struct passwd *) getpwuid (XINT (uid));
659 return (pw ? build_string (pw->pw_name) : Qnil);
35692fe0
JB
660}
661
662DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
663 0, 0, 0,
664 "Return the name of the user's real uid, as a string.\n\
9658bdd0 665This ignores the environment variables LOGNAME and USER, so it differs from\n\
b1da234a 666`user-login-name' when running under `su'.")
35692fe0
JB
667 ()
668{
f8a0e364
RS
669 /* Set up the user name info if we didn't do it before.
670 (That can happen if Emacs is dumpable
671 but you decide to run `temacs -l loadup' and not dump. */
35b34f72 672 if (INTEGERP (Vuser_login_name))
f8a0e364 673 init_editfns ();
35b34f72 674 return Vuser_real_login_name;
35692fe0
JB
675}
676
677DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
678 "Return the effective uid of Emacs, as an integer.")
679 ()
680{
681 return make_number (geteuid ());
682}
683
684DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
685 "Return the real uid of Emacs, as an integer.")
686 ()
687{
688 return make_number (getuid ());
689}
690
c9ed721d
RS
691DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0,
692 "Return the full name of the user logged in, as a string.\n\
693If optional argument UID is an integer, return the full name of the user\n\
ca38bbb2 694with that uid, or \"unknown\" if there is no such user.\n\
3415b0e9
RS
695If UID is a string, return the full name of the user with that login\n\
696name, or \"unknown\" if no such user could be found.")
c9ed721d
RS
697 (uid)
698 Lisp_Object uid;
35692fe0 699{
c9ed721d 700 struct passwd *pw;
b0e92acd 701 register unsigned char *p, *q;
3415b0e9
RS
702 extern char *index ();
703 Lisp_Object full;
c9ed721d
RS
704
705 if (NILP (uid))
3415b0e9
RS
706 return Vuser_full_name;
707 else if (NUMBERP (uid))
708 pw = (struct passwd *) getpwuid (XINT (uid));
709 else if (STRINGP (uid))
710 pw = (struct passwd *) getpwnam (XSTRING (uid)->data);
711 else
712 error ("Invalid UID specification");
c9ed721d 713
3415b0e9 714 if (!pw)
3347526c 715 return Qnil;
3415b0e9
RS
716
717 p = (unsigned char *) USER_FULL_NAME;
718 /* Chop off everything after the first comma. */
719 q = (unsigned char *) index (p, ',');
720 full = make_string (p, q ? q - p : strlen (p));
721
722#ifdef AMPERSAND_FULL_NAME
723 p = XSTRING (full)->data;
724 q = (unsigned char *) index (p, '&');
725 /* Substitute the login name for the &, upcasing the first character. */
726 if (q)
727 {
b0e92acd 728 register unsigned char *r;
3415b0e9
RS
729 Lisp_Object login;
730
731 login = Fuser_login_name (make_number (pw->pw_uid));
732 r = (unsigned char *) alloca (strlen (p) + XSTRING (login)->size + 1);
733 bcopy (p, r, q - p);
734 r[q - p] = 0;
735 strcat (r, XSTRING (login)->data);
736 r[q - p] = UPCASE (r[q - p]);
737 strcat (r, q + 1);
738 full = build_string (r);
739 }
740#endif /* AMPERSAND_FULL_NAME */
741
742 return full;
35692fe0
JB
743}
744
745DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
746 "Return the name of the machine you are running on, as a string.")
747 ()
748{
749 return Vsystem_name;
750}
751
ac988277
KH
752/* For the benefit of callers who don't want to include lisp.h */
753char *
754get_system_name ()
755{
3d976a9a
RS
756 if (STRINGP (Vsystem_name))
757 return (char *) XSTRING (Vsystem_name)->data;
758 else
759 return "";
ac988277
KH
760}
761
7fd233b3
RS
762DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
763 "Return the process ID of Emacs, as an integer.")
764 ()
765{
766 return make_number (getpid ());
767}
768
d940e0e4 769DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
e983fdb2 770 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
956ace37
JB
771The time is returned as a list of three integers. The first has the\n\
772most significant 16 bits of the seconds, while the second has the\n\
773least significant 16 bits. The third integer gives the microsecond\n\
774count.\n\
775\n\
776The microsecond count is zero on systems that do not provide\n\
777resolution finer than a second.")
d940e0e4
JB
778 ()
779{
956ace37
JB
780 EMACS_TIME t;
781 Lisp_Object result[3];
782
783 EMACS_GET_TIME (t);
d2fd0445
KH
784 XSETINT (result[0], (EMACS_SECS (t) >> 16) & 0xffff);
785 XSETINT (result[1], (EMACS_SECS (t) >> 0) & 0xffff);
786 XSETINT (result[2], EMACS_USECS (t));
956ace37
JB
787
788 return Flist (3, result);
d940e0e4
JB
789}
790\f
791
e3120ab5
JB
792static int
793lisp_time_argument (specified_time, result)
794 Lisp_Object specified_time;
795 time_t *result;
796{
797 if (NILP (specified_time))
798 return time (result) != -1;
799 else
800 {
801 Lisp_Object high, low;
802 high = Fcar (specified_time);
803 CHECK_NUMBER (high, 0);
804 low = Fcdr (specified_time);
ae683129 805 if (CONSP (low))
e3120ab5
JB
806 low = Fcar (low);
807 CHECK_NUMBER (low, 0);
808 *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
809 return *result >> 16 == XINT (high);
810 }
811}
812
3efcc98a 813/*
b48382a0
RS
814DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
815 "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
816TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
817`current-time' or `file-attributes'.\n\
818The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
819as Universal Time; nil means describe TIME in the local time zone.\n\
820The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
821by text that describes the specified date and time in TIME:\n\
a82d387c 822\n\
b48382a0
RS
823%Y is the year, %y within the century, %C the century.\n\
824%G is the year corresponding to the ISO week, %g within the century.\n\
b771d0da
PE
825%m is the numeric month.\n\
826%b and %h are the locale's abbreviated month name, %B the full name.\n\
b48382a0
RS
827%d is the day of the month, zero-padded, %e is blank-padded.\n\
828%u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\
b771d0da 829%a is the locale's abbreviated name of the day of week, %A the full name.\n\
b48382a0
RS
830%U is the week number starting on Sunday, %W starting on Monday,\n\
831 %V according to ISO 8601.\n\
832%j is the day of the year.\n\
833\n\
834%H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\
835 only blank-padded, %l is like %I blank-padded.\n\
b771d0da 836%p is the locale's equivalent of either AM or PM.\n\
b48382a0
RS
837%M is the minute.\n\
838%S is the second.\n\
839%Z is the time zone name, %z is the numeric form.\n\
840%s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\
841\n\
842%c is the locale's date and time format.\n\
843%x is the locale's \"preferred\" date format.\n\
844%D is like \"%m/%d/%y\".\n\
845\n\
846%R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\
847%X is the locale's \"preferred\" time format.\n\
848\n\
b771d0da 849Finally, %n is a newline, %t is a tab, %% is a literal %.\n\
b48382a0 850\n\
3efcc98a 851Certain flags and modifiers are available with some format controls.\n\
b48382a0
RS
852The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
853but padded with blanks; %-X is like %X, but without padding.\n\
854%NX (where N stands for an integer) is like %X,\n\
855but takes up at least N (a number) positions.\n\
856The modifiers are `E' and `O'. For certain characters X,\n\
857%EX is a locale's alternative version of %X;\n\
858%OX is like %X, but uses the locale's number symbols.\n\
859\n\
860For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
726ed0da 861 (format_string, time, universal)
3efcc98a
RS
862*/
863
864DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
865 0 /* See immediately above */)
b48382a0
RS
866 (format_string, time, universal)
867 Lisp_Object format_string, time, universal;
a82d387c
RS
868{
869 time_t value;
870 int size;
871
872 CHECK_STRING (format_string, 1);
873
874 if (! lisp_time_argument (time, &value))
875 error ("Invalid time specification");
876
877 /* This is probably enough. */
dc3620af 878 size = STRING_BYTES (XSTRING (format_string)) * 6 + 50;
a82d387c
RS
879
880 while (1)
881 {
b48382a0
RS
882 char *buf = (char *) alloca (size + 1);
883 int result;
884
bfbcc5ee 885 buf[0] = '\1';
b48382a0
RS
886 result = emacs_strftime (buf, size, XSTRING (format_string)->data,
887 (NILP (universal) ? localtime (&value)
888 : gmtime (&value)));
bfbcc5ee 889 if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
a82d387c 890 return build_string (buf);
b48382a0
RS
891
892 /* If buffer was too small, make it bigger and try again. */
bfbcc5ee 893 result = emacs_strftime (NULL, 0x7fffffff, XSTRING (format_string)->data,
b48382a0
RS
894 (NILP (universal) ? localtime (&value)
895 : gmtime (&value)));
896 size = result + 1;
a82d387c
RS
897 }
898}
899
4691c06d
RS
900DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
901 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
902The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
903or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
904to use the current time. The list has the following nine members:\n\
145b0681
RS
905SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
906only some operating systems support. MINUTE is an integer between 0 and 59.\n\
4691c06d
RS
907HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
908MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
909four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
9100 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
911ZONE is an integer indicating the number of seconds east of Greenwich.\n\
2c6c7c72 912\(Note that Common Lisp has different meanings for DOW and ZONE.)")
4691c06d
RS
913 (specified_time)
914 Lisp_Object specified_time;
915{
916 time_t time_spec;
3c887943 917 struct tm save_tm;
4691c06d
RS
918 struct tm *decoded_time;
919 Lisp_Object list_args[9];
920
921 if (! lisp_time_argument (specified_time, &time_spec))
922 error ("Invalid time specification");
923
924 decoded_time = localtime (&time_spec);
3c887943
KH
925 XSETFASTINT (list_args[0], decoded_time->tm_sec);
926 XSETFASTINT (list_args[1], decoded_time->tm_min);
927 XSETFASTINT (list_args[2], decoded_time->tm_hour);
928 XSETFASTINT (list_args[3], decoded_time->tm_mday);
929 XSETFASTINT (list_args[4], decoded_time->tm_mon + 1);
236ebf35 930 XSETINT (list_args[5], decoded_time->tm_year + 1900);
3c887943 931 XSETFASTINT (list_args[6], decoded_time->tm_wday);
4691c06d 932 list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
3c887943
KH
933
934 /* Make a copy, in case gmtime modifies the struct. */
935 save_tm = *decoded_time;
936 decoded_time = gmtime (&time_spec);
937 if (decoded_time == 0)
938 list_args[8] = Qnil;
939 else
94751666 940 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
4691c06d
RS
941 return Flist (9, list_args);
942}
943
6ee9061c 944DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
2591ec64 945 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
6ee9061c
RS
946This is the reverse operation of `decode-time', which see.\n\
947ZONE defaults to the current time zone rule. This can\n\
085e9fcb 948be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
7459d65b 949\(as from `current-time-zone') or an integer (as from `decode-time')\n\
c59b5089 950applied without consideration for daylight savings time.\n\
6ee9061c
RS
951\n\
952You can pass more than 7 arguments; then the first six arguments\n\
953are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
954The intervening arguments are ignored.\n\
955This feature lets (apply 'encode-time (decode-time ...)) work.\n\
956\n\
c59b5089
PE
957Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
958for example, a DAY of 0 means the day preceding the given month.\n\
01ba8cce 959Year numbers less than 100 are treated just like other year numbers.\n\
c59b5089 960If you want them to stand for years in this century, you must do that yourself.")
6ee9061c
RS
961 (nargs, args)
962 int nargs;
963 register Lisp_Object *args;
cce7b8a0 964{
1b8fa736 965 time_t time;
c59b5089 966 struct tm tm;
60653898 967 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
6ee9061c
RS
968
969 CHECK_NUMBER (args[0], 0); /* second */
970 CHECK_NUMBER (args[1], 1); /* minute */
971 CHECK_NUMBER (args[2], 2); /* hour */
972 CHECK_NUMBER (args[3], 3); /* day */
973 CHECK_NUMBER (args[4], 4); /* month */
974 CHECK_NUMBER (args[5], 5); /* year */
975
976 tm.tm_sec = XINT (args[0]);
977 tm.tm_min = XINT (args[1]);
978 tm.tm_hour = XINT (args[2]);
979 tm.tm_mday = XINT (args[3]);
980 tm.tm_mon = XINT (args[4]) - 1;
981 tm.tm_year = XINT (args[5]) - 1900;
c59b5089
PE
982 tm.tm_isdst = -1;
983
984 if (CONSP (zone))
985 zone = Fcar (zone);
1b8fa736 986 if (NILP (zone))
c59b5089
PE
987 time = mktime (&tm);
988 else
1b8fa736 989 {
c59b5089
PE
990 char tzbuf[100];
991 char *tzstring;
992 char **oldenv = environ, **newenv;
993
2e34157c 994 if (EQ (zone, Qt))
085e9fcb
EN
995 tzstring = "UTC0";
996 else if (STRINGP (zone))
4d4c1514 997 tzstring = (char *) XSTRING (zone)->data;
c59b5089 998 else if (INTEGERP (zone))
1b8fa736 999 {
c59b5089
PE
1000 int abszone = abs (XINT (zone));
1001 sprintf (tzbuf, "XXX%s%d:%02d:%02d", "-" + (XINT (zone) < 0),
1002 abszone / (60*60), (abszone/60) % 60, abszone % 60);
1003 tzstring = tzbuf;
1b8fa736 1004 }
c59b5089
PE
1005 else
1006 error ("Invalid time zone specification");
1007
1008 /* Set TZ before calling mktime; merely adjusting mktime's returned
1009 value doesn't suffice, since that would mishandle leap seconds. */
1010 set_time_zone_rule (tzstring);
1011
1012 time = mktime (&tm);
1013
1014 /* Restore TZ to previous value. */
1015 newenv = environ;
1016 environ = oldenv;
c0efcacf 1017 xfree (newenv);
c59b5089
PE
1018#ifdef LOCALTIME_CACHE
1019 tzset ();
1020#endif
1b8fa736 1021 }
1b8fa736 1022
c59b5089
PE
1023 if (time == (time_t) -1)
1024 error ("Specified time is not representable");
1025
1026 return make_time (time);
cce7b8a0
RS
1027}
1028
2148f2b4 1029DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
35692fe0 1030 "Return the current time, as a human-readable string.\n\
2148f2b4
RS
1031Programs can use this function to decode a time,\n\
1032since the number of columns in each field is fixed.\n\
1033The format is `Sun Sep 16 01:03:52 1973'.\n\
7a8630da
RS
1034However, see also the functions `decode-time' and `format-time-string'\n\
1035which provide a much more powerful and general facility.\n\
1036\n\
2148f2b4
RS
1037If an argument is given, it specifies a time to format\n\
1038instead of the current time. The argument should have the form:\n\
1039 (HIGH . LOW)\n\
1040or the form:\n\
1041 (HIGH LOW . IGNORED).\n\
1042Thus, you can use times obtained from `current-time'\n\
1043and from `file-attributes'.")
1044 (specified_time)
1045 Lisp_Object specified_time;
1046{
e3120ab5 1047 time_t value;
35692fe0 1048 char buf[30];
2148f2b4
RS
1049 register char *tem;
1050
e3120ab5
JB
1051 if (! lisp_time_argument (specified_time, &value))
1052 value = -1;
2148f2b4 1053 tem = (char *) ctime (&value);
35692fe0
JB
1054
1055 strncpy (buf, tem, 24);
1056 buf[24] = 0;
1057
1058 return build_string (buf);
1059}
c2662aea 1060
94751666 1061#define TM_YEAR_BASE 1900
e3120ab5 1062
94751666
PE
1063/* Yield A - B, measured in seconds.
1064 This function is copied from the GNU C Library. */
1065static int
1066tm_diff (a, b)
e3120ab5
JB
1067 struct tm *a, *b;
1068{
94751666
PE
1069 /* Compute intervening leap days correctly even if year is negative.
1070 Take care to avoid int overflow in leap day calculations,
1071 but it's OK to assume that A and B are close to each other. */
1072 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
1073 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
1074 int a100 = a4 / 25 - (a4 % 25 < 0);
1075 int b100 = b4 / 25 - (b4 % 25 < 0);
1076 int a400 = a100 >> 2;
1077 int b400 = b100 >> 2;
1078 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
1079 int years = a->tm_year - b->tm_year;
1080 int days = (365 * years + intervening_leap_days
1081 + (a->tm_yday - b->tm_yday));
1082 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
1083 + (a->tm_min - b->tm_min))
8e718b4e 1084 + (a->tm_sec - b->tm_sec));
e3120ab5
JB
1085}
1086
1087DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
1088 "Return the offset and name for the local time zone.\n\
1089This returns a list of the form (OFFSET NAME).\n\
1090OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1091 A negative value means west of Greenwich.\n\
1092NAME is a string giving the name of the time zone.\n\
1093If an argument is given, it specifies when the time zone offset is determined\n\
1094instead of using the current time. The argument should have the form:\n\
1095 (HIGH . LOW)\n\
1096or the form:\n\
1097 (HIGH LOW . IGNORED).\n\
1098Thus, you can use times obtained from `current-time'\n\
1099and from `file-attributes'.\n\
773c1fd3
JB
1100\n\
1101Some operating systems cannot provide all this information to Emacs;\n\
2d88f747 1102in this case, `current-time-zone' returns a list containing nil for\n\
773c1fd3 1103the data it can't find.")
e3120ab5
JB
1104 (specified_time)
1105 Lisp_Object specified_time;
c2662aea 1106{
e3120ab5
JB
1107 time_t value;
1108 struct tm *t;
c2662aea 1109
e3120ab5 1110 if (lisp_time_argument (specified_time, &value)
2d88f747 1111 && (t = gmtime (&value)) != 0)
e3120ab5 1112 {
2d88f747 1113 struct tm gmt;
94751666 1114 int offset;
e3120ab5 1115 char *s, buf[6];
2d88f747
RS
1116
1117 gmt = *t; /* Make a copy, in case localtime modifies *t. */
1118 t = localtime (&value);
94751666 1119 offset = tm_diff (t, &gmt);
e3120ab5
JB
1120 s = 0;
1121#ifdef HAVE_TM_ZONE
1122 if (t->tm_zone)
5fd4de15 1123 s = (char *)t->tm_zone;
a7971c39
RS
1124#else /* not HAVE_TM_ZONE */
1125#ifdef HAVE_TZNAME
1126 if (t->tm_isdst == 0 || t->tm_isdst == 1)
1127 s = tzname[t->tm_isdst];
c2662aea 1128#endif
a7971c39 1129#endif /* not HAVE_TM_ZONE */
e3120ab5
JB
1130 if (!s)
1131 {
1132 /* No local time zone name is available; use "+-NNNN" instead. */
00fc94d0 1133 int am = (offset < 0 ? -offset : offset) / 60;
e3120ab5
JB
1134 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
1135 s = buf;
1136 }
1137 return Fcons (make_number (offset), Fcons (build_string (s), Qnil));
1138 }
1139 else
09dbcf71 1140 return Fmake_list (make_number (2), Qnil);
c2662aea
JB
1141}
1142
260e2e2a
KH
1143/* This holds the value of `environ' produced by the previous
1144 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1145 has never been called. */
1146static char **environbuf;
1147
143cb9a9
RS
1148DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
1149 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
085e9fcb
EN
1150If TZ is nil, use implementation-defined default time zone information.\n\
1151If TZ is t, use Universal Time.")
143cb9a9
RS
1152 (tz)
1153 Lisp_Object tz;
1154{
143cb9a9
RS
1155 char *tzstring;
1156
1157 if (NILP (tz))
1158 tzstring = 0;
2e34157c 1159 else if (EQ (tz, Qt))
085e9fcb 1160 tzstring = "UTC0";
143cb9a9
RS
1161 else
1162 {
1163 CHECK_STRING (tz, 0);
4d4c1514 1164 tzstring = (char *) XSTRING (tz)->data;
143cb9a9
RS
1165 }
1166
c59b5089
PE
1167 set_time_zone_rule (tzstring);
1168 if (environbuf)
1169 free (environbuf);
1170 environbuf = environ;
1171
1172 return Qnil;
1173}
1174
e0bf9faf
PE
1175#ifdef LOCALTIME_CACHE
1176
1177/* These two values are known to load tz files in buggy implementations,
1178 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1155c453
RS
1179 Their values shouldn't matter in non-buggy implementations.
1180 We don't use string literals for these strings,
1181 since if a string in the environment is in readonly
1182 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1183 See Sun bugs 1113095 and 1114114, ``Timezone routines
1184 improperly modify environment''. */
1185
e0bf9faf
PE
1186static char set_time_zone_rule_tz1[] = "TZ=GMT+0";
1187static char set_time_zone_rule_tz2[] = "TZ=GMT+1";
1188
1189#endif
1155c453 1190
c59b5089
PE
1191/* Set the local time zone rule to TZSTRING.
1192 This allocates memory into `environ', which it is the caller's
1193 responsibility to free. */
a92ae0ce 1194void
c59b5089
PE
1195set_time_zone_rule (tzstring)
1196 char *tzstring;
1197{
1198 int envptrs;
1199 char **from, **to, **newenv;
1200
aafe5147 1201 /* Make the ENVIRON vector longer with room for TZSTRING. */
143cb9a9
RS
1202 for (from = environ; *from; from++)
1203 continue;
1204 envptrs = from - environ + 2;
1205 newenv = to = (char **) xmalloc (envptrs * sizeof (char *)
1206 + (tzstring ? strlen (tzstring) + 4 : 0));
aafe5147
RS
1207
1208 /* Add TZSTRING to the end of environ, as a value for TZ. */
143cb9a9
RS
1209 if (tzstring)
1210 {
1211 char *t = (char *) (to + envptrs);
1212 strcpy (t, "TZ=");
1213 strcat (t, tzstring);
1214 *to++ = t;
1215 }
1216
aafe5147
RS
1217 /* Copy the old environ vector elements into NEWENV,
1218 but don't copy the TZ variable.
1219 So we have only one definition of TZ, which came from TZSTRING. */
143cb9a9
RS
1220 for (from = environ; *from; from++)
1221 if (strncmp (*from, "TZ=", 3) != 0)
1222 *to++ = *from;
1223 *to = 0;
1224
1225 environ = newenv;
143cb9a9 1226
aafe5147
RS
1227 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1228 the TZ variable is stored. If we do not have a TZSTRING,
1229 TO points to the vector slot which has the terminating null. */
1230
143cb9a9 1231#ifdef LOCALTIME_CACHE
aafe5147
RS
1232 {
1233 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1234 "US/Pacific" that loads a tz file, then changes to a value like
1235 "XXX0" that does not load a tz file, and then changes back to
1236 its original value, the last change is (incorrectly) ignored.
1237 Also, if TZ changes twice in succession to values that do
1238 not load a tz file, tzset can dump core (see Sun bug#1225179).
1239 The following code works around these bugs. */
1240
aafe5147
RS
1241 if (tzstring)
1242 {
1243 /* Temporarily set TZ to a value that loads a tz file
1244 and that differs from tzstring. */
1245 char *tz = *newenv;
1155c453
RS
1246 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
1247 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
aafe5147
RS
1248 tzset ();
1249 *newenv = tz;
1250 }
1251 else
1252 {
1253 /* The implied tzstring is unknown, so temporarily set TZ to
1254 two different values that each load a tz file. */
1155c453 1255 *to = set_time_zone_rule_tz1;
aafe5147
RS
1256 to[1] = 0;
1257 tzset ();
1155c453 1258 *to = set_time_zone_rule_tz2;
aafe5147
RS
1259 tzset ();
1260 *to = 0;
1261 }
1262
1263 /* Now TZ has the desired value, and tzset can be invoked safely. */
1264 }
1265
143cb9a9
RS
1266 tzset ();
1267#endif
143cb9a9 1268}
35692fe0 1269\f
fb8106e8
KH
1270/* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1271 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1272 type of object is Lisp_String). INHERIT is passed to
1273 INSERT_FROM_STRING_FUNC as the last argument. */
1274
d11ba98c 1275void
fb8106e8
KH
1276general_insert_function (insert_func, insert_from_string_func,
1277 inherit, nargs, args)
d11ba98c 1278 void (*insert_func) P_ ((unsigned char *, int));
1f24f4fd 1279 void (*insert_from_string_func) P_ ((Lisp_Object, int, int, int, int, int));
fb8106e8
KH
1280 int inherit, nargs;
1281 register Lisp_Object *args;
1282{
1283 register int argnum;
1284 register Lisp_Object val;
1285
1286 for (argnum = 0; argnum < nargs; argnum++)
1287 {
1288 val = args[argnum];
1289 retry:
1290 if (INTEGERP (val))
1291 {
d11ba98c 1292 unsigned char workbuf[4], *str;
fb8106e8
KH
1293 int len;
1294
1295 if (!NILP (current_buffer->enable_multibyte_characters))
1296 len = CHAR_STRING (XFASTINT (val), workbuf, str);
1297 else
1298 workbuf[0] = XINT (val), str = workbuf, len = 1;
1299 (*insert_func) (str, len);
1300 }
1301 else if (STRINGP (val))
1302 {
1f24f4fd
RS
1303 (*insert_from_string_func) (val, 0, 0,
1304 XSTRING (val)->size,
dc3620af 1305 STRING_BYTES (XSTRING (val)),
1f24f4fd 1306 inherit);
fb8106e8
KH
1307 }
1308 else
1309 {
1310 val = wrong_type_argument (Qchar_or_string_p, val);
1311 goto retry;
1312 }
1313 }
1314}
1315
35692fe0
JB
1316void
1317insert1 (arg)
1318 Lisp_Object arg;
1319{
1320 Finsert (1, &arg);
1321}
1322
52b14ac0
JB
1323
1324/* Callers passing one argument to Finsert need not gcpro the
1325 argument "array", since the only element of the array will
1326 not be used after calling insert or insert_from_string, so
1327 we don't care if it gets trashed. */
1328
35692fe0
JB
1329DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
1330 "Insert the arguments, either strings or characters, at point.\n\
fb8106e8
KH
1331Point and before-insertion-markers move forward so that it ends up\n\
1332 after the inserted text.\n\
35692fe0
JB
1333Any other markers at the point of insertion remain before the text.")
1334 (nargs, args)
1335 int nargs;
1336 register Lisp_Object *args;
1337{
fb8106e8 1338 general_insert_function (insert, insert_from_string, 0, nargs, args);
be91036a
RS
1339 return Qnil;
1340}
1341
1342DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
1343 0, MANY, 0,
1344 "Insert the arguments at point, inheriting properties from adjoining text.\n\
fb8106e8
KH
1345Point and before-insertion-markers move forward so that it ends up\n\
1346 after the inserted text.\n\
be91036a
RS
1347Any other markers at the point of insertion remain before the text.")
1348 (nargs, args)
1349 int nargs;
1350 register Lisp_Object *args;
1351{
fb8106e8
KH
1352 general_insert_function (insert_and_inherit, insert_from_string, 1,
1353 nargs, args);
35692fe0
JB
1354 return Qnil;
1355}
1356
1357DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
1358 "Insert strings or characters at point, relocating markers after the text.\n\
fb8106e8
KH
1359Point and before-insertion-markers move forward so that it ends up\n\
1360 after the inserted text.\n\
35692fe0
JB
1361Any other markers at the point of insertion also end up after the text.")
1362 (nargs, args)
1363 int nargs;
1364 register Lisp_Object *args;
1365{
fb8106e8
KH
1366 general_insert_function (insert_before_markers,
1367 insert_from_string_before_markers, 0,
1368 nargs, args);
be91036a
RS
1369 return Qnil;
1370}
1371
a0d76c27
EN
1372DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
1373 Sinsert_and_inherit_before_markers, 0, MANY, 0,
be91036a
RS
1374 "Insert text at point, relocating markers and inheriting properties.\n\
1375Point moves forward so that it ends up after the inserted text.\n\
1376Any other markers at the point of insertion also end up after the text.")
1377 (nargs, args)
1378 int nargs;
1379 register Lisp_Object *args;
1380{
fb8106e8
KH
1381 general_insert_function (insert_before_markers_and_inherit,
1382 insert_from_string_before_markers, 1,
1383 nargs, args);
35692fe0
JB
1384 return Qnil;
1385}
1386\f
e2eeabbb 1387DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
2591ec64 1388 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
fb8106e8 1389Point and before-insertion-markers are affected as in the function `insert'.\n\
e2eeabbb
RS
1390Both arguments are required.\n\
1391The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1392from adjoining text, if those properties are sticky.")
2591ec64
EN
1393 (character, count, inherit)
1394 Lisp_Object character, count, inherit;
35692fe0
JB
1395{
1396 register unsigned char *string;
1397 register int strlen;
1398 register int i, n;
fb8106e8
KH
1399 int len;
1400 unsigned char workbuf[4], *str;
35692fe0 1401
2591ec64 1402 CHECK_NUMBER (character, 0);
35692fe0
JB
1403 CHECK_NUMBER (count, 1);
1404
fb8106e8
KH
1405 if (!NILP (current_buffer->enable_multibyte_characters))
1406 len = CHAR_STRING (XFASTINT (character), workbuf, str);
1407 else
1408 workbuf[0] = XFASTINT (character), str = workbuf, len = 1;
1409 n = XINT (count) * len;
35692fe0
JB
1410 if (n <= 0)
1411 return Qnil;
fb8106e8 1412 strlen = min (n, 256 * len);
35692fe0
JB
1413 string = (unsigned char *) alloca (strlen);
1414 for (i = 0; i < strlen; i++)
fb8106e8 1415 string[i] = str[i % len];
35692fe0
JB
1416 while (n >= strlen)
1417 {
54e42e2d 1418 QUIT;
e2eeabbb
RS
1419 if (!NILP (inherit))
1420 insert_and_inherit (string, strlen);
1421 else
1422 insert (string, strlen);
35692fe0
JB
1423 n -= strlen;
1424 }
1425 if (n > 0)
83951f1e
KH
1426 {
1427 if (!NILP (inherit))
1428 insert_and_inherit (string, n);
1429 else
1430 insert (string, n);
1431 }
35692fe0
JB
1432 return Qnil;
1433}
1434
1435\f
ffd56f97
JB
1436/* Making strings from buffer contents. */
1437
1438/* Return a Lisp_String containing the text of the current buffer from
74d6d8c5 1439 START to END. If text properties are in use and the current buffer
eb8c3be9 1440 has properties in the range specified, the resulting string will also
260e2e2a 1441 have them, if PROPS is nonzero.
ffd56f97
JB
1442
1443 We don't want to use plain old make_string here, because it calls
1444 make_uninit_string, which can cause the buffer arena to be
1445 compacted. make_string has no way of knowing that the data has
1446 been moved, and thus copies the wrong data into the string. This
1447 doesn't effect most of the other users of make_string, so it should
1448 be left as is. But we should use this function when conjuring
1449 buffer substrings. */
74d6d8c5 1450
ffd56f97 1451Lisp_Object
260e2e2a 1452make_buffer_string (start, end, props)
ffd56f97 1453 int start, end;
260e2e2a 1454 int props;
ffd56f97 1455{
ec1c14f6
RS
1456 int start_byte = CHAR_TO_BYTE (start);
1457 int end_byte = CHAR_TO_BYTE (end);
ffd56f97 1458
88441c8e
RS
1459 return make_buffer_string_both (start, start_byte, end, end_byte, props);
1460}
1461
1462/* Return a Lisp_String containing the text of the current buffer from
1463 START / START_BYTE to END / END_BYTE.
1464
1465 If text properties are in use and the current buffer
1466 has properties in the range specified, the resulting string will also
1467 have them, if PROPS is nonzero.
1468
1469 We don't want to use plain old make_string here, because it calls
1470 make_uninit_string, which can cause the buffer arena to be
1471 compacted. make_string has no way of knowing that the data has
1472 been moved, and thus copies the wrong data into the string. This
1473 doesn't effect most of the other users of make_string, so it should
1474 be left as is. But we should use this function when conjuring
1475 buffer substrings. */
1476
1477Lisp_Object
1478make_buffer_string_both (start, start_byte, end, end_byte, props)
1479 int start, start_byte, end, end_byte;
1480 int props;
1481{
1482 Lisp_Object result, tem, tem1;
1483
ffd56f97
JB
1484 if (start < GPT && GPT < end)
1485 move_gap (start);
1486
5f75e666
RS
1487 if (! NILP (current_buffer->enable_multibyte_characters))
1488 result = make_uninit_multibyte_string (end - start, end_byte - start_byte);
1489 else
1490 result = make_uninit_string (end - start);
ec1c14f6
RS
1491 bcopy (BYTE_POS_ADDR (start_byte), XSTRING (result)->data,
1492 end_byte - start_byte);
ffd56f97 1493
260e2e2a 1494 /* If desired, update and copy the text properties. */
60b96ee7 1495#ifdef USE_TEXT_PROPERTIES
260e2e2a
KH
1496 if (props)
1497 {
1498 update_buffer_properties (start, end);
1499
1500 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
1501 tem1 = Ftext_properties_at (make_number (start), Qnil);
1502
1503 if (XINT (tem) != end || !NILP (tem1))
ec1c14f6
RS
1504 copy_intervals_to_string (result, current_buffer, start,
1505 end - start);
260e2e2a 1506 }
60b96ee7 1507#endif
74d6d8c5 1508
ffd56f97
JB
1509 return result;
1510}
35692fe0 1511
260e2e2a
KH
1512/* Call Vbuffer_access_fontify_functions for the range START ... END
1513 in the current buffer, if necessary. */
1514
1515static void
1516update_buffer_properties (start, end)
1517 int start, end;
1518{
1519#ifdef USE_TEXT_PROPERTIES
1520 /* If this buffer has some access functions,
1521 call them, specifying the range of the buffer being accessed. */
1522 if (!NILP (Vbuffer_access_fontify_functions))
1523 {
1524 Lisp_Object args[3];
1525 Lisp_Object tem;
1526
1527 args[0] = Qbuffer_access_fontify_functions;
1528 XSETINT (args[1], start);
1529 XSETINT (args[2], end);
1530
1531 /* But don't call them if we can tell that the work
1532 has already been done. */
1533 if (!NILP (Vbuffer_access_fontified_property))
1534 {
1535 tem = Ftext_property_any (args[1], args[2],
1536 Vbuffer_access_fontified_property,
1537 Qnil, Qnil);
1538 if (! NILP (tem))
ced1d19a 1539 Frun_hook_with_args (3, args);
260e2e2a
KH
1540 }
1541 else
ced1d19a 1542 Frun_hook_with_args (3, args);
260e2e2a
KH
1543 }
1544#endif
1545}
1546
35692fe0
JB
1547DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
1548 "Return the contents of part of the current buffer as a string.\n\
1549The two arguments START and END are character positions;\n\
1550they can be in either order.")
2591ec64
EN
1551 (start, end)
1552 Lisp_Object start, end;
35692fe0 1553{
2591ec64 1554 register int b, e;
35692fe0 1555
2591ec64
EN
1556 validate_region (&start, &end);
1557 b = XINT (start);
1558 e = XINT (end);
35692fe0 1559
2591ec64 1560 return make_buffer_string (b, e, 1);
260e2e2a
KH
1561}
1562
1563DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
1564 Sbuffer_substring_no_properties, 2, 2, 0,
1565 "Return the characters of part of the buffer, without the text properties.\n\
1566The two arguments START and END are character positions;\n\
1567they can be in either order.")
2591ec64
EN
1568 (start, end)
1569 Lisp_Object start, end;
260e2e2a 1570{
2591ec64 1571 register int b, e;
260e2e2a 1572
2591ec64
EN
1573 validate_region (&start, &end);
1574 b = XINT (start);
1575 e = XINT (end);
260e2e2a 1576
2591ec64 1577 return make_buffer_string (b, e, 0);
35692fe0
JB
1578}
1579
1580DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
af7bd86c
KH
1581 "Return the contents of the current buffer as a string.\n\
1582If narrowing is in effect, this function returns only the visible part\n\
1583of the buffer.")
35692fe0
JB
1584 ()
1585{
260e2e2a 1586 return make_buffer_string (BEGV, ZV, 1);
35692fe0
JB
1587}
1588
1589DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
1590 1, 3, 0,
83ea6fc2 1591 "Insert before point a substring of the contents of buffer BUFFER.\n\
35692fe0
JB
1592BUFFER may be a buffer or a buffer name.\n\
1593Arguments START and END are character numbers specifying the substring.\n\
1594They default to the beginning and the end of BUFFER.")
2591ec64
EN
1595 (buf, start, end)
1596 Lisp_Object buf, start, end;
35692fe0 1597{
2591ec64 1598 register int b, e, temp;
260e2e2a 1599 register struct buffer *bp, *obuf;
3fff2dfa 1600 Lisp_Object buffer;
35692fe0 1601
3fff2dfa
RS
1602 buffer = Fget_buffer (buf);
1603 if (NILP (buffer))
1604 nsberror (buf);
1605 bp = XBUFFER (buffer);
93b62e82
KH
1606 if (NILP (bp->name))
1607 error ("Selecting deleted buffer");
35692fe0 1608
2591ec64
EN
1609 if (NILP (start))
1610 b = BUF_BEGV (bp);
35692fe0
JB
1611 else
1612 {
2591ec64
EN
1613 CHECK_NUMBER_COERCE_MARKER (start, 0);
1614 b = XINT (start);
35692fe0 1615 }
2591ec64
EN
1616 if (NILP (end))
1617 e = BUF_ZV (bp);
35692fe0
JB
1618 else
1619 {
2591ec64
EN
1620 CHECK_NUMBER_COERCE_MARKER (end, 1);
1621 e = XINT (end);
35692fe0
JB
1622 }
1623
2591ec64
EN
1624 if (b > e)
1625 temp = b, b = e, e = temp;
35692fe0 1626
2591ec64
EN
1627 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
1628 args_out_of_range (start, end);
35692fe0 1629
260e2e2a
KH
1630 obuf = current_buffer;
1631 set_buffer_internal_1 (bp);
2591ec64 1632 update_buffer_properties (b, e);
260e2e2a
KH
1633 set_buffer_internal_1 (obuf);
1634
2591ec64 1635 insert_from_buffer (bp, b, e - b, 0);
35692fe0
JB
1636 return Qnil;
1637}
e9cf2084
RS
1638
1639DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
1640 6, 6, 0,
1641 "Compare two substrings of two buffers; return result as number.\n\
1642the value is -N if first string is less after N-1 chars,\n\
1643+N if first string is greater after N-1 chars, or 0 if strings match.\n\
1644Each substring is represented as three arguments: BUFFER, START and END.\n\
1645That makes six args in all, three for each substring.\n\n\
1646The value of `case-fold-search' in the current buffer\n\
1647determines whether case is significant or ignored.")
1648 (buffer1, start1, end1, buffer2, start2, end2)
1649 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
1650{
1651 register int begp1, endp1, begp2, endp2, temp, len1, len2, length, i;
1652 register struct buffer *bp1, *bp2;
2a8b0ff0 1653 register Lisp_Object *trt
e9cf2084 1654 = (!NILP (current_buffer->case_fold_search)
2a8b0ff0 1655 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0);
ec1c14f6
RS
1656 int chars = 0;
1657 int beg1_byte, beg2_byte;
e9cf2084
RS
1658
1659 /* Find the first buffer and its substring. */
1660
1661 if (NILP (buffer1))
1662 bp1 = current_buffer;
1663 else
1664 {
3fff2dfa
RS
1665 Lisp_Object buf1;
1666 buf1 = Fget_buffer (buffer1);
1667 if (NILP (buf1))
1668 nsberror (buffer1);
1669 bp1 = XBUFFER (buf1);
93b62e82
KH
1670 if (NILP (bp1->name))
1671 error ("Selecting deleted buffer");
e9cf2084
RS
1672 }
1673
1674 if (NILP (start1))
1675 begp1 = BUF_BEGV (bp1);
1676 else
1677 {
1678 CHECK_NUMBER_COERCE_MARKER (start1, 1);
1679 begp1 = XINT (start1);
1680 }
1681 if (NILP (end1))
1682 endp1 = BUF_ZV (bp1);
1683 else
1684 {
1685 CHECK_NUMBER_COERCE_MARKER (end1, 2);
1686 endp1 = XINT (end1);
1687 }
1688
1689 if (begp1 > endp1)
1690 temp = begp1, begp1 = endp1, endp1 = temp;
1691
1692 if (!(BUF_BEGV (bp1) <= begp1
1693 && begp1 <= endp1
1694 && endp1 <= BUF_ZV (bp1)))
1695 args_out_of_range (start1, end1);
1696
1697 /* Likewise for second substring. */
1698
1699 if (NILP (buffer2))
1700 bp2 = current_buffer;
1701 else
1702 {
3fff2dfa
RS
1703 Lisp_Object buf2;
1704 buf2 = Fget_buffer (buffer2);
1705 if (NILP (buf2))
1706 nsberror (buffer2);
3b1fdd85 1707 bp2 = XBUFFER (buf2);
93b62e82
KH
1708 if (NILP (bp2->name))
1709 error ("Selecting deleted buffer");
e9cf2084
RS
1710 }
1711
1712 if (NILP (start2))
1713 begp2 = BUF_BEGV (bp2);
1714 else
1715 {
1716 CHECK_NUMBER_COERCE_MARKER (start2, 4);
1717 begp2 = XINT (start2);
1718 }
1719 if (NILP (end2))
1720 endp2 = BUF_ZV (bp2);
1721 else
1722 {
1723 CHECK_NUMBER_COERCE_MARKER (end2, 5);
1724 endp2 = XINT (end2);
1725 }
1726
1727 if (begp2 > endp2)
1728 temp = begp2, begp2 = endp2, endp2 = temp;
1729
1730 if (!(BUF_BEGV (bp2) <= begp2
1731 && begp2 <= endp2
1732 && endp2 <= BUF_ZV (bp2)))
1733 args_out_of_range (start2, end2);
1734
ec1c14f6
RS
1735 beg1_byte = buf_charpos_to_bytepos (bp1, begp1);
1736 beg2_byte = buf_charpos_to_bytepos (bp2, begp2);
1737 len1 = buf_charpos_to_bytepos (bp1, endp1) - begp1;
1738 len2 = buf_charpos_to_bytepos (bp2, endp2) - begp2;
e9cf2084
RS
1739 length = len1;
1740 if (len2 < length)
1741 length = len2;
1742
1743 for (i = 0; i < length; i++)
1744 {
ec1c14f6
RS
1745 unsigned char *p1 = BUF_BYTE_ADDRESS (bp1, beg1_byte + i);
1746 int c1 = *p1;
1747 int c2 = *BUF_BYTE_ADDRESS (bp2, beg2_byte + i);
1748
1749 /* If a character begins here,
1750 count the previous character now. */
1751 if (i > 0
1752 && (NILP (current_buffer->enable_multibyte_characters)
1753 || CHAR_HEAD_P (*p1)))
1754 chars++;
1755
e9cf2084
RS
1756 if (trt)
1757 {
1b10fb77
RS
1758 c1 = XINT (trt[c1]);
1759 c2 = XINT (trt[c2]);
e9cf2084
RS
1760 }
1761 if (c1 < c2)
ec1c14f6 1762 return make_number (- 1 - chars);
e9cf2084 1763 if (c1 > c2)
ec1c14f6 1764 return make_number (chars + 1);
e9cf2084
RS
1765 }
1766
1767 /* The strings match as far as they go.
1768 If one is shorter, that one is less. */
1769 if (length < len1)
ec1c14f6 1770 return make_number (chars + 1);
e9cf2084 1771 else if (length < len2)
ec1c14f6 1772 return make_number (- chars - 1);
e9cf2084
RS
1773
1774 /* Same length too => they are equal. */
1775 return make_number (0);
1776}
35692fe0 1777\f
d5a539cd
RS
1778static Lisp_Object
1779subst_char_in_region_unwind (arg)
1780 Lisp_Object arg;
1781{
1782 return current_buffer->undo_list = arg;
1783}
1784
c8e76b47
RS
1785static Lisp_Object
1786subst_char_in_region_unwind_1 (arg)
1787 Lisp_Object arg;
1788{
1789 return current_buffer->filename = arg;
1790}
1791
35692fe0
JB
1792DEFUN ("subst-char-in-region", Fsubst_char_in_region,
1793 Ssubst_char_in_region, 4, 5, 0,
1794 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1795If optional arg NOUNDO is non-nil, don't record this change for undo\n\
fb8106e8
KH
1796and don't mark the buffer as really changed.\n\
1797Both characters must have the same length of multi-byte form.")
35692fe0
JB
1798 (start, end, fromchar, tochar, noundo)
1799 Lisp_Object start, end, fromchar, tochar, noundo;
1800{
84246b95 1801 register int pos, pos_byte, stop, i, len, end_byte;
60b96ee7 1802 int changed = 0;
fb8106e8 1803 unsigned char fromwork[4], *fromstr, towork[4], *tostr, *p;
d5a539cd 1804 int count = specpdl_ptr - specpdl;
35692fe0
JB
1805
1806 validate_region (&start, &end);
1807 CHECK_NUMBER (fromchar, 2);
1808 CHECK_NUMBER (tochar, 3);
1809
fb8106e8
KH
1810 if (! NILP (current_buffer->enable_multibyte_characters))
1811 {
1812 len = CHAR_STRING (XFASTINT (fromchar), fromwork, fromstr);
1813 if (CHAR_STRING (XFASTINT (tochar), towork, tostr) != len)
1814 error ("Characters in subst-char-in-region have different byte-lengths");
1815 }
1816 else
1817 {
1818 len = 1;
1819 fromwork[0] = XFASTINT (fromchar), fromstr = fromwork;
1820 towork[0] = XFASTINT (tochar), tostr = towork;
1821 }
1822
84246b95
KH
1823 pos = XINT (start);
1824 pos_byte = CHAR_TO_BYTE (pos);
ec1c14f6
RS
1825 stop = CHAR_TO_BYTE (XINT (end));
1826 end_byte = stop;
35692fe0 1827
d5a539cd
RS
1828 /* If we don't want undo, turn off putting stuff on the list.
1829 That's faster than getting rid of things,
c8e76b47
RS
1830 and it prevents even the entry for a first change.
1831 Also inhibit locking the file. */
d5a539cd
RS
1832 if (!NILP (noundo))
1833 {
1834 record_unwind_protect (subst_char_in_region_unwind,
1835 current_buffer->undo_list);
1836 current_buffer->undo_list = Qt;
c8e76b47
RS
1837 /* Don't do file-locking. */
1838 record_unwind_protect (subst_char_in_region_unwind_1,
1839 current_buffer->filename);
1840 current_buffer->filename = Qnil;
d5a539cd
RS
1841 }
1842
84246b95 1843 if (pos_byte < GPT_BYTE)
ec1c14f6 1844 stop = min (stop, GPT_BYTE);
fb8106e8 1845 while (1)
35692fe0 1846 {
84246b95 1847 if (pos_byte >= stop)
fb8106e8 1848 {
84246b95 1849 if (pos_byte >= end_byte) break;
ec1c14f6 1850 stop = end_byte;
fb8106e8 1851 }
84246b95 1852 p = BYTE_POS_ADDR (pos_byte);
fb8106e8
KH
1853 if (p[0] == fromstr[0]
1854 && (len == 1
1855 || (p[1] == fromstr[1]
1856 && (len == 2 || (p[2] == fromstr[2]
1857 && (len == 3 || p[3] == fromstr[3]))))))
35692fe0 1858 {
60b96ee7
RS
1859 if (! changed)
1860 {
fb8106e8 1861 modify_region (current_buffer, XINT (start), XINT (end));
7653d030
RS
1862
1863 if (! NILP (noundo))
1864 {
1e158d25
RS
1865 if (MODIFF - 1 == SAVE_MODIFF)
1866 SAVE_MODIFF++;
7653d030
RS
1867 if (MODIFF - 1 == current_buffer->auto_save_modified)
1868 current_buffer->auto_save_modified++;
1869 }
1870
fb8106e8 1871 changed = 1;
60b96ee7
RS
1872 }
1873
56a98455 1874 if (NILP (noundo))
84246b95 1875 record_change (pos, 1);
fb8106e8 1876 for (i = 0; i < len; i++) *p++ = tostr[i];
35692fe0 1877 }
84246b95 1878 INC_BOTH (pos, pos_byte);
35692fe0
JB
1879 }
1880
60b96ee7
RS
1881 if (changed)
1882 signal_after_change (XINT (start),
84246b95 1883 XINT (end) - XINT (start), XINT (end) - XINT (start));
60b96ee7 1884
d5a539cd 1885 unbind_to (count, Qnil);
35692fe0
JB
1886 return Qnil;
1887}
1888
1889DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
1890 "From START to END, translate characters according to TABLE.\n\
1891TABLE is a string; the Nth character in it is the mapping\n\
1f24f4fd
RS
1892for the character with code N.\n\
1893This function does not alter multibyte characters.\n\
1894It returns the number of characters changed.")
35692fe0
JB
1895 (start, end, table)
1896 Lisp_Object start;
1897 Lisp_Object end;
1898 register Lisp_Object table;
1899{
ec1c14f6 1900 register int pos_byte, stop; /* Limits of the region. */
35692fe0 1901 register unsigned char *tt; /* Trans table. */
35692fe0
JB
1902 register int nc; /* New character. */
1903 int cnt; /* Number of changes made. */
35692fe0 1904 int size; /* Size of translate table. */
1f24f4fd 1905 int pos;
35692fe0
JB
1906
1907 validate_region (&start, &end);
1908 CHECK_STRING (table, 2);
1909
dc3620af 1910 size = STRING_BYTES (XSTRING (table));
35692fe0
JB
1911 tt = XSTRING (table)->data;
1912
ec1c14f6
RS
1913 pos_byte = CHAR_TO_BYTE (XINT (start));
1914 stop = CHAR_TO_BYTE (XINT (end));
1915 modify_region (current_buffer, XINT (start), XINT (end));
1f24f4fd 1916 pos = XINT (start);
35692fe0
JB
1917
1918 cnt = 0;
1f24f4fd 1919 for (; pos_byte < stop; )
35692fe0 1920 {
ec1c14f6 1921 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
1f24f4fd
RS
1922 int len;
1923 int oc;
ec1c14f6 1924
1f24f4fd
RS
1925 oc = STRING_CHAR_AND_LENGTH (p, stop - pos_byte, len);
1926 if (oc < size && len == 1)
35692fe0
JB
1927 {
1928 nc = tt[oc];
1929 if (nc != oc)
1930 {
1f24f4fd 1931 record_change (pos, 1);
ec1c14f6 1932 *p = nc;
1f24f4fd 1933 signal_after_change (pos, 1, 1);
35692fe0
JB
1934 ++cnt;
1935 }
1936 }
1f24f4fd
RS
1937 pos_byte += len;
1938 pos++;
35692fe0
JB
1939 }
1940
ec1c14f6 1941 return make_number (cnt);
35692fe0
JB
1942}
1943
1944DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
1945 "Delete the text between point and mark.\n\
1946When called from a program, expects two arguments,\n\
1947positions (integers or markers) specifying the stretch to be deleted.")
2591ec64
EN
1948 (start, end)
1949 Lisp_Object start, end;
35692fe0 1950{
2591ec64
EN
1951 validate_region (&start, &end);
1952 del_range (XINT (start), XINT (end));
35692fe0
JB
1953 return Qnil;
1954}
1955\f
1956DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
1957 "Remove restrictions (narrowing) from current buffer.\n\
1958This allows the buffer's full text to be seen and edited.")
1959 ()
1960{
2cad2e34
RS
1961 if (BEG != BEGV || Z != ZV)
1962 current_buffer->clip_changed = 1;
35692fe0 1963 BEGV = BEG;
ec1c14f6
RS
1964 BEGV_BYTE = BEG_BYTE;
1965 SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE);
52b14ac0
JB
1966 /* Changing the buffer bounds invalidates any recorded current column. */
1967 invalidate_current_column ();
35692fe0
JB
1968 return Qnil;
1969}
1970
1971DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
1972 "Restrict editing in this buffer to the current region.\n\
1973The rest of the text becomes temporarily invisible and untouchable\n\
1974but is not deleted; if you save the buffer in a file, the invisible\n\
1975text is included in the file. \\[widen] makes all visible again.\n\
1976See also `save-restriction'.\n\
1977\n\
1978When calling from a program, pass two arguments; positions (integers\n\
1979or markers) bounding the text that should remain visible.")
2591ec64
EN
1980 (start, end)
1981 register Lisp_Object start, end;
35692fe0 1982{
2591ec64
EN
1983 CHECK_NUMBER_COERCE_MARKER (start, 0);
1984 CHECK_NUMBER_COERCE_MARKER (end, 1);
35692fe0 1985
2591ec64 1986 if (XINT (start) > XINT (end))
35692fe0 1987 {
b5a6948e 1988 Lisp_Object tem;
2591ec64 1989 tem = start; start = end; end = tem;
35692fe0
JB
1990 }
1991
2591ec64
EN
1992 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
1993 args_out_of_range (start, end);
35692fe0 1994
2cad2e34
RS
1995 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
1996 current_buffer->clip_changed = 1;
1997
ec1c14f6 1998 SET_BUF_BEGV (current_buffer, XFASTINT (start));
2591ec64 1999 SET_BUF_ZV (current_buffer, XFASTINT (end));
6ec8bbd2 2000 if (PT < XFASTINT (start))
2591ec64 2001 SET_PT (XFASTINT (start));
6ec8bbd2 2002 if (PT > XFASTINT (end))
2591ec64 2003 SET_PT (XFASTINT (end));
52b14ac0
JB
2004 /* Changing the buffer bounds invalidates any recorded current column. */
2005 invalidate_current_column ();
35692fe0
JB
2006 return Qnil;
2007}
2008
2009Lisp_Object
2010save_restriction_save ()
2011{
2012 register Lisp_Object bottom, top;
2013 /* Note: I tried using markers here, but it does not win
2014 because insertion at the end of the saved region
2015 does not advance mh and is considered "outside" the saved region. */
55561c63
KH
2016 XSETFASTINT (bottom, BEGV - BEG);
2017 XSETFASTINT (top, Z - ZV);
35692fe0
JB
2018
2019 return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
2020}
2021
2022Lisp_Object
2023save_restriction_restore (data)
2024 Lisp_Object data;
2025{
2026 register struct buffer *buf;
2027 register int newhead, newtail;
2028 register Lisp_Object tem;
2cad2e34 2029 int obegv, ozv;
35692fe0
JB
2030
2031 buf = XBUFFER (XCONS (data)->car);
2032
2033 data = XCONS (data)->cdr;
2034
2035 tem = XCONS (data)->car;
2036 newhead = XINT (tem);
2037 tem = XCONS (data)->cdr;
2038 newtail = XINT (tem);
2039 if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
2040 {
2041 newhead = 0;
2042 newtail = 0;
2043 }
2cad2e34
RS
2044
2045 obegv = BUF_BEGV (buf);
2046 ozv = BUF_ZV (buf);
2047
ec1c14f6 2048 SET_BUF_BEGV (buf, BUF_BEG (buf) + newhead);
35692fe0 2049 SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
2cad2e34
RS
2050
2051 if (obegv != BUF_BEGV (buf) || ozv != BUF_ZV (buf))
2052 current_buffer->clip_changed = 1;
35692fe0
JB
2053
2054 /* If point is outside the new visible range, move it inside. */
ec1c14f6
RS
2055 SET_BUF_PT_BOTH (buf,
2056 clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)),
2057 clip_to_bounds (BUF_BEGV_BYTE (buf), BUF_PT_BYTE (buf),
2058 BUF_ZV_BYTE (buf)));
35692fe0
JB
2059
2060 return Qnil;
2061}
2062
2063DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
2064 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2065The buffer's restrictions make parts of the beginning and end invisible.\n\
2066\(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2067This special form, `save-restriction', saves the current buffer's restrictions\n\
2068when it is entered, and restores them when it is exited.\n\
2069So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2070The old restrictions settings are restored\n\
2071even in case of abnormal exit (throw or error).\n\
2072\n\
2073The value returned is the value of the last form in BODY.\n\
2074\n\
2075`save-restriction' can get confused if, within the BODY, you widen\n\
2076and then make changes outside the area within the saved restrictions.\n\
2077\n\
2078Note: if you are using both `save-excursion' and `save-restriction',\n\
2079use `save-excursion' outermost:\n\
2080 (save-excursion (save-restriction ...))")
2081 (body)
2082 Lisp_Object body;
2083{
2084 register Lisp_Object val;
2085 int count = specpdl_ptr - specpdl;
2086
2087 record_unwind_protect (save_restriction_restore, save_restriction_save ());
2088 val = Fprogn (body);
2089 return unbind_to (count, val);
2090}
2091\f
671fbc4d
KH
2092/* Buffer for the most recent text displayed by Fmessage. */
2093static char *message_text;
2094
2095/* Allocated length of that buffer. */
2096static int message_length;
2097
35692fe0
JB
2098DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
2099 "Print a one-line message at the bottom of the screen.\n\
98fc5c3c
RS
2100The first argument is a format control string, and the rest are data\n\
2101to be formatted under control of the string. See `format' for details.\n\
2102\n\
ccdac5be
JB
2103If the first argument is nil, clear any existing message; let the\n\
2104minibuffer contents show.")
35692fe0
JB
2105 (nargs, args)
2106 int nargs;
2107 Lisp_Object *args;
2108{
ccdac5be 2109 if (NILP (args[0]))
f0250249
JB
2110 {
2111 message (0);
2112 return Qnil;
2113 }
ccdac5be
JB
2114 else
2115 {
2116 register Lisp_Object val;
2117 val = Fformat (nargs, args);
671fbc4d
KH
2118 /* Copy the data so that it won't move when we GC. */
2119 if (! message_text)
2120 {
2121 message_text = (char *)xmalloc (80);
2122 message_length = 80;
2123 }
dc3620af 2124 if (STRING_BYTES (XSTRING (val)) > message_length)
671fbc4d 2125 {
dc3620af 2126 message_length = STRING_BYTES (XSTRING (val));
671fbc4d
KH
2127 message_text = (char *)xrealloc (message_text, message_length);
2128 }
dc3620af
RS
2129 bcopy (XSTRING (val)->data, message_text, STRING_BYTES (XSTRING (val)));
2130 message2 (message_text, STRING_BYTES (XSTRING (val)),
1f24f4fd 2131 STRING_MULTIBYTE (val));
ccdac5be
JB
2132 return val;
2133 }
35692fe0
JB
2134}
2135
cacc3e2c
RS
2136DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
2137 "Display a message, in a dialog box if possible.\n\
2138If a dialog box is not available, use the echo area.\n\
f8250f01
RS
2139The first argument is a format control string, and the rest are data\n\
2140to be formatted under control of the string. See `format' for details.\n\
2141\n\
cacc3e2c
RS
2142If the first argument is nil, clear any existing message; let the\n\
2143minibuffer contents show.")
2144 (nargs, args)
2145 int nargs;
2146 Lisp_Object *args;
2147{
2148 if (NILP (args[0]))
2149 {
2150 message (0);
2151 return Qnil;
2152 }
2153 else
2154 {
2155 register Lisp_Object val;
2156 val = Fformat (nargs, args);
f8250f01 2157#ifdef HAVE_MENUS
cacc3e2c
RS
2158 {
2159 Lisp_Object pane, menu, obj;
2160 struct gcpro gcpro1;
2161 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
2162 GCPRO1 (pane);
2163 menu = Fcons (val, pane);
2164 obj = Fx_popup_dialog (Qt, menu);
2165 UNGCPRO;
2166 return val;
2167 }
f8250f01 2168#else /* not HAVE_MENUS */
cacc3e2c
RS
2169 /* Copy the data so that it won't move when we GC. */
2170 if (! message_text)
2171 {
2172 message_text = (char *)xmalloc (80);
2173 message_length = 80;
2174 }
dc3620af 2175 if (STRING_BYTES (XSTRING (val)) > message_length)
cacc3e2c 2176 {
dc3620af 2177 message_length = STRING_BYTES (XSTRING (val));
cacc3e2c
RS
2178 message_text = (char *)xrealloc (message_text, message_length);
2179 }
dc3620af 2180 bcopy (XSTRING (val)->data, message_text, STRING_BYTES (XSTRING (val)));
d13a8480
RS
2181 message2 (message_text, STRING_BYTES (XSTRING (val)),
2182 STRING_MULTIBYTE (val));
cacc3e2c 2183 return val;
f8250f01 2184#endif /* not HAVE_MENUS */
cacc3e2c
RS
2185 }
2186}
f8250f01 2187#ifdef HAVE_MENUS
cacc3e2c
RS
2188extern Lisp_Object last_nonmenu_event;
2189#endif
f8250f01 2190
cacc3e2c
RS
2191DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
2192 "Display a message in a dialog box or in the echo area.\n\
2193If this command was invoked with the mouse, use a dialog box.\n\
2194Otherwise, use the echo area.\n\
f8250f01
RS
2195The first argument is a format control string, and the rest are data\n\
2196to be formatted under control of the string. See `format' for details.\n\
cacc3e2c 2197\n\
cacc3e2c
RS
2198If the first argument is nil, clear any existing message; let the\n\
2199minibuffer contents show.")
2200 (nargs, args)
2201 int nargs;
2202 Lisp_Object *args;
2203{
f8250f01 2204#ifdef HAVE_MENUS
cacc3e2c 2205 if (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
0a56ee6b 2206 return Fmessage_box (nargs, args);
cacc3e2c
RS
2207#endif
2208 return Fmessage (nargs, args);
2209}
2210
b14dda8a
RS
2211DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0,
2212 "Return the string currently displayed in the echo area, or nil if none.")
2213 ()
2214{
2215 return (echo_area_glyphs
2216 ? make_string (echo_area_glyphs, echo_area_glyphs_length)
2217 : Qnil);
2218}
2219
1f24f4fd
RS
2220/* Number of bytes that STRING will occupy when put into the result.
2221 MULTIBYTE is nonzero if the result should be multibyte. */
2222
2223#define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \
2224 (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \
8d6179dc 2225 ? count_size_as_multibyte (XSTRING (STRING)->data, \
dc3620af
RS
2226 STRING_BYTES (XSTRING (STRING))) \
2227 : STRING_BYTES (XSTRING (STRING)))
1f24f4fd 2228
35692fe0
JB
2229DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
2230 "Format a string out of a control-string and arguments.\n\
2231The first argument is a control string.\n\
2232The other arguments are substituted into it to make the result, a string.\n\
2233It may contain %-sequences meaning to substitute the next argument.\n\
2234%s means print a string argument. Actually, prints any object, with `princ'.\n\
2235%d means print as number in decimal (%o octal, %x hex).\n\
9db1775a
RS
2236%e means print a number in exponential notation.\n\
2237%f means print a number in decimal-point notation.\n\
2238%g means print a number in exponential notation\n\
2239 or decimal-point notation, whichever uses fewer characters.\n\
35692fe0
JB
2240%c means print a number as a single character.\n\
2241%S means print any object as an s-expression (using prin1).\n\
9db1775a 2242 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
52b14ac0 2243Use %% to put a single % into the output.")
35692fe0
JB
2244 (nargs, args)
2245 int nargs;
2246 register Lisp_Object *args;
2247{
2248 register int n; /* The number of the next arg to substitute */
e781c49e 2249 register int total; /* An estimate of the final length */
1f24f4fd 2250 char *buf, *p;
35692fe0 2251 register unsigned char *format, *end;
1f24f4fd
RS
2252 int length, nchars;
2253 /* Nonzero if the output should be a multibyte string,
2254 which is true if any of the inputs is one. */
2255 int multibyte = 0;
2256 unsigned char *this_format;
e781c49e 2257 int longest_format;
8d6179dc 2258 Lisp_Object val;
1f24f4fd 2259
35692fe0 2260 extern char *index ();
1f24f4fd 2261
35692fe0
JB
2262 /* It should not be necessary to GCPRO ARGS, because
2263 the caller in the interpreter should take care of that. */
2264
e781c49e
RS
2265 /* Try to determine whether the result should be multibyte.
2266 This is not always right; sometimes the result needs to be multibyte
2267 because of an object that we will pass through prin1,
2268 and in that case, we won't know it here. */
1f24f4fd
RS
2269 for (n = 0; n < nargs; n++)
2270 if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
2271 multibyte = 1;
2272
35692fe0 2273 CHECK_STRING (args[0], 0);
e781c49e
RS
2274
2275 /* If we start out planning a unibyte result,
2276 and later find it has to be multibyte, we jump back to retry. */
2277 retry:
2278
35692fe0 2279 format = XSTRING (args[0])->data;
dc3620af 2280 end = format + STRING_BYTES (XSTRING (args[0]));
e781c49e 2281 longest_format = 0;
1f24f4fd
RS
2282
2283 /* Make room in result for all the non-%-codes in the control string. */
e781c49e 2284 total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]);
1f24f4fd
RS
2285
2286 /* Add to TOTAL enough space to hold the converted arguments. */
35692fe0
JB
2287
2288 n = 0;
2289 while (format != end)
2290 if (*format++ == '%')
2291 {
1f24f4fd
RS
2292 int minlen, thissize = 0;
2293 unsigned char *this_format_start = format - 1;
35692fe0
JB
2294
2295 /* Process a numeric arg and skip it. */
2296 minlen = atoi (format);
537dfb13
RS
2297 if (minlen < 0)
2298 minlen = - minlen;
2299
35692fe0
JB
2300 while ((*format >= '0' && *format <= '9')
2301 || *format == '-' || *format == ' ' || *format == '.')
2302 format++;
2303
1f24f4fd
RS
2304 if (format - this_format_start + 1 > longest_format)
2305 longest_format = format - this_format_start + 1;
2306
35692fe0
JB
2307 if (*format == '%')
2308 format++;
2309 else if (++n >= nargs)
537dfb13 2310 error ("Not enough arguments for format string");
35692fe0
JB
2311 else if (*format == 'S')
2312 {
2313 /* For `S', prin1 the argument and then treat like a string. */
2314 register Lisp_Object tem;
2315 tem = Fprin1_to_string (args[n], Qnil);
e781c49e
RS
2316 if (STRING_MULTIBYTE (tem) && ! multibyte)
2317 {
2318 multibyte = 1;
2319 goto retry;
2320 }
35692fe0
JB
2321 args[n] = tem;
2322 goto string;
2323 }
ae683129 2324 else if (SYMBOLP (args[n]))
35692fe0 2325 {
d2fd0445 2326 XSETSTRING (args[n], XSYMBOL (args[n])->name);
7df74da6
RS
2327 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
2328 {
2329 multibyte = 1;
2330 goto retry;
2331 }
35692fe0
JB
2332 goto string;
2333 }
ae683129 2334 else if (STRINGP (args[n]))
35692fe0
JB
2335 {
2336 string:
b22e7ecc
KH
2337 if (*format != 's' && *format != 'S')
2338 error ("format specifier doesn't match argument type");
1f24f4fd 2339 thissize = CONVERTED_BYTE_SIZE (multibyte, args[n]);
35692fe0
JB
2340 }
2341 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
ae683129 2342 else if (INTEGERP (args[n]) && *format != 's')
35692fe0 2343 {
4746118a 2344#ifdef LISP_FLOAT_TYPE
eb8c3be9 2345 /* The following loop assumes the Lisp type indicates
35692fe0
JB
2346 the proper way to pass the argument.
2347 So make sure we have a flonum if the argument should
2348 be a double. */
2349 if (*format == 'e' || *format == 'f' || *format == 'g')
2350 args[n] = Ffloat (args[n]);
4746118a 2351#endif
f49a2d74 2352 thissize = 30;
25c9e7fb
RS
2353 if (*format == 'c'
2354 && (! SINGLE_BYTE_CHAR_P (XINT (args[n]))
2355 || XINT (args[n]) == 0))
f49a2d74
KH
2356 {
2357 if (! multibyte)
2358 {
2359 multibyte = 1;
2360 goto retry;
2361 }
2362 args[n] = Fchar_to_string (args[n]);
dc3620af 2363 thissize = STRING_BYTES (XSTRING (args[n]));
f49a2d74 2364 }
35692fe0 2365 }
4746118a 2366#ifdef LISP_FLOAT_TYPE
ae683129 2367 else if (FLOATP (args[n]) && *format != 's')
35692fe0
JB
2368 {
2369 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
247422ce 2370 args[n] = Ftruncate (args[n], Qnil);
1f24f4fd 2371 thissize = 60;
35692fe0 2372 }
4746118a 2373#endif
35692fe0
JB
2374 else
2375 {
2376 /* Anything but a string, convert to a string using princ. */
2377 register Lisp_Object tem;
2378 tem = Fprin1_to_string (args[n], Qt);
a4e91443 2379 if (STRING_MULTIBYTE (tem) & ! multibyte)
e781c49e
RS
2380 {
2381 multibyte = 1;
2382 goto retry;
2383 }
35692fe0
JB
2384 args[n] = tem;
2385 goto string;
2386 }
1f24f4fd
RS
2387
2388 if (thissize < minlen)
2389 thissize = minlen;
2390
2391 total += thissize + 4;
35692fe0
JB
2392 }
2393
e781c49e
RS
2394 /* Now we can no longer jump to retry.
2395 TOTAL and LONGEST_FORMAT are known for certain. */
2396
1f24f4fd 2397 this_format = (unsigned char *) alloca (longest_format + 1);
50aa2f90 2398
1f24f4fd
RS
2399 /* Allocate the space for the result.
2400 Note that TOTAL is an overestimate. */
2401 if (total < 1000)
2402 buf = (unsigned char *) alloca (total + 1);
2403 else
2404 buf = (unsigned char *) xmalloc (total + 1);
35692fe0 2405
1f24f4fd
RS
2406 p = buf;
2407 nchars = 0;
2408 n = 0;
35692fe0 2409
1f24f4fd
RS
2410 /* Scan the format and store result in BUF. */
2411 format = XSTRING (args[0])->data;
2412 while (format != end)
2413 {
2414 if (*format == '%')
2415 {
2416 int minlen;
25c9e7fb 2417 int negative = 0;
1f24f4fd 2418 unsigned char *this_format_start = format;
35692fe0 2419
1f24f4fd 2420 format++;
fb893977 2421
1f24f4fd
RS
2422 /* Process a numeric arg and skip it. */
2423 minlen = atoi (format);
2424 if (minlen < 0)
25c9e7fb 2425 minlen = - minlen, negative = 1;
35692fe0 2426
1f24f4fd
RS
2427 while ((*format >= '0' && *format <= '9')
2428 || *format == '-' || *format == ' ' || *format == '.')
2429 format++;
35692fe0 2430
1f24f4fd
RS
2431 if (*format++ == '%')
2432 {
2433 *p++ = '%';
2434 nchars++;
2435 continue;
2436 }
2437
2438 ++n;
2439
2440 if (STRINGP (args[n]))
2441 {
25c9e7fb
RS
2442 int padding, nbytes;
2443 int width = strwidth (XSTRING (args[n])->data,
dc3620af 2444 STRING_BYTES (XSTRING (args[n])));
25c9e7fb
RS
2445
2446 /* If spec requires it, pad on right with spaces. */
2447 padding = minlen - width;
2448 if (! negative)
2449 while (padding-- > 0)
2450 {
2451 *p++ = ' ';
2452 nchars++;
2453 }
1f24f4fd
RS
2454
2455 nbytes = copy_text (XSTRING (args[n])->data, p,
dc3620af 2456 STRING_BYTES (XSTRING (args[n])),
1f24f4fd
RS
2457 STRING_MULTIBYTE (args[n]), multibyte);
2458 p += nbytes;
2459 nchars += XSTRING (args[n])->size;
2460
25c9e7fb
RS
2461 if (negative)
2462 while (padding-- > 0)
2463 {
2464 *p++ = ' ';
2465 nchars++;
2466 }
1f24f4fd
RS
2467 }
2468 else if (INTEGERP (args[n]) || FLOATP (args[n]))
2469 {
2470 int this_nchars;
2471
2472 bcopy (this_format_start, this_format,
2473 format - this_format_start);
2474 this_format[format - this_format_start] = 0;
2475
d0183d38
RS
2476 if (INTEGERP (args[n]))
2477 sprintf (p, this_format, XINT (args[n]));
2478 else
2479 sprintf (p, this_format, XFLOAT (args[n])->data);
1f24f4fd
RS
2480
2481 this_nchars = strlen (p);
2482 p += this_nchars;
2483 nchars += this_nchars;
2484 }
2485 }
7df74da6
RS
2486 else if (STRING_MULTIBYTE (args[0]))
2487 {
2488 /* Copy a whole multibyte character. */
2489 *p++ = *format++;
2490 while (! CHAR_HEAD_P (*format)) *p++ = *format++;
2491 nchars++;
2492 }
2493 else if (multibyte)
1f24f4fd
RS
2494 {
2495 /* Convert a single-byte character to multibyte. */
2496 int len = copy_text (format, p, 1, 0, 1);
2497
2498 p += len;
2499 format++;
2500 nchars++;
2501 }
2502 else
2503 *p++ = *format++, nchars++;
2504 }
2505
5f75e666 2506 val = make_specified_string (buf, nchars, p - buf, multibyte);
8d6179dc 2507
1f24f4fd
RS
2508 /* If we allocated BUF with malloc, free it too. */
2509 if (total >= 1000)
2510 xfree (buf);
35692fe0 2511
8d6179dc 2512 return val;
35692fe0
JB
2513}
2514
2515/* VARARGS 1 */
2516Lisp_Object
2517#ifdef NO_ARG_ARRAY
2518format1 (string1, arg0, arg1, arg2, arg3, arg4)
679e18b1 2519 EMACS_INT arg0, arg1, arg2, arg3, arg4;
35692fe0
JB
2520#else
2521format1 (string1)
2522#endif
2523 char *string1;
2524{
2525 char buf[100];
2526#ifdef NO_ARG_ARRAY
679e18b1 2527 EMACS_INT args[5];
35692fe0
JB
2528 args[0] = arg0;
2529 args[1] = arg1;
2530 args[2] = arg2;
2531 args[3] = arg3;
2532 args[4] = arg4;
e3670faa 2533 doprnt (buf, sizeof buf, string1, (char *)0, 5, (char **) args);
35692fe0 2534#else
ea4d2909 2535 doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1);
35692fe0
JB
2536#endif
2537 return build_string (buf);
2538}
2539\f
2540DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
2541 "Return t if two characters match, optionally ignoring case.\n\
2542Both arguments must be characters (i.e. integers).\n\
2543Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2544 (c1, c2)
2545 register Lisp_Object c1, c2;
2546{
1b5d98bb 2547 int i1, i2;
35692fe0
JB
2548 CHECK_NUMBER (c1, 0);
2549 CHECK_NUMBER (c2, 1);
2550
1b5d98bb 2551 if (XINT (c1) == XINT (c2))
35692fe0 2552 return Qt;
1b5d98bb
RS
2553 if (NILP (current_buffer->case_fold_search))
2554 return Qnil;
2555
2556 /* Do these in separate statements,
2557 then compare the variables.
2558 because of the way DOWNCASE uses temp variables. */
2559 i1 = DOWNCASE (XFASTINT (c1));
2560 i2 = DOWNCASE (XFASTINT (c2));
2561 return (i1 == i2 ? Qt : Qnil);
35692fe0 2562}
b229b8d1
RS
2563\f
2564/* Transpose the markers in two regions of the current buffer, and
2565 adjust the ones between them if necessary (i.e.: if the regions
2566 differ in size).
2567
ec1c14f6
RS
2568 START1, END1 are the character positions of the first region.
2569 START1_BYTE, END1_BYTE are the byte positions.
2570 START2, END2 are the character positions of the second region.
2571 START2_BYTE, END2_BYTE are the byte positions.
2572
b229b8d1
RS
2573 Traverses the entire marker list of the buffer to do so, adding an
2574 appropriate amount to some, subtracting from some, and leaving the
2575 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2576
ec1c14f6 2577 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
b229b8d1
RS
2578
2579void
ec1c14f6
RS
2580transpose_markers (start1, end1, start2, end2,
2581 start1_byte, end1_byte, start2_byte, end2_byte)
b229b8d1 2582 register int start1, end1, start2, end2;
ec1c14f6 2583 register int start1_byte, end1_byte, start2_byte, end2_byte;
b229b8d1 2584{
ec1c14f6 2585 register int amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
b229b8d1 2586 register Lisp_Object marker;
b229b8d1 2587
03240d11 2588 /* Update point as if it were a marker. */
8de1d5f0
KH
2589 if (PT < start1)
2590 ;
2591 else if (PT < end1)
ec1c14f6
RS
2592 TEMP_SET_PT_BOTH (PT + (end2 - end1),
2593 PT_BYTE + (end2_byte - end1_byte));
8de1d5f0 2594 else if (PT < start2)
ec1c14f6
RS
2595 TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1),
2596 (PT_BYTE + (end2_byte - start2_byte)
2597 - (end1_byte - start1_byte)));
8de1d5f0 2598 else if (PT < end2)
ec1c14f6
RS
2599 TEMP_SET_PT_BOTH (PT - (start2 - start1),
2600 PT_BYTE - (start2_byte - start1_byte));
8de1d5f0 2601
03240d11
KH
2602 /* We used to adjust the endpoints here to account for the gap, but that
2603 isn't good enough. Even if we assume the caller has tried to move the
2604 gap out of our way, it might still be at start1 exactly, for example;
2605 and that places it `inside' the interval, for our purposes. The amount
2606 of adjustment is nontrivial if there's a `denormalized' marker whose
2607 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2608 the dirty work to Fmarker_position, below. */
b229b8d1
RS
2609
2610 /* The difference between the region's lengths */
2611 diff = (end2 - start2) - (end1 - start1);
ec1c14f6 2612 diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte);
b229b8d1
RS
2613
2614 /* For shifting each marker in a region by the length of the other
ec1c14f6 2615 region plus the distance between the regions. */
b229b8d1
RS
2616 amt1 = (end2 - start2) + (start2 - end1);
2617 amt2 = (end1 - start1) + (start2 - end1);
ec1c14f6
RS
2618 amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte);
2619 amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte);
b229b8d1 2620
1e158d25 2621 for (marker = BUF_MARKERS (current_buffer); !NILP (marker);
03240d11 2622 marker = XMARKER (marker)->chain)
b229b8d1 2623 {
ec1c14f6
RS
2624 mpos = marker_byte_position (marker);
2625 if (mpos >= start1_byte && mpos < end2_byte)
2626 {
2627 if (mpos < end1_byte)
2628 mpos += amt1_byte;
2629 else if (mpos < start2_byte)
2630 mpos += diff_byte;
2631 else
2632 mpos -= amt2_byte;
f3e1f752 2633 XMARKER (marker)->bytepos = mpos;
ec1c14f6
RS
2634 }
2635 mpos = XMARKER (marker)->charpos;
03240d11
KH
2636 if (mpos >= start1 && mpos < end2)
2637 {
2638 if (mpos < end1)
2639 mpos += amt1;
2640 else if (mpos < start2)
2641 mpos += diff;
2642 else
2643 mpos -= amt2;
03240d11 2644 }
ec1c14f6 2645 XMARKER (marker)->charpos = mpos;
b229b8d1
RS
2646 }
2647}
2648
2649DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
2650 "Transpose region START1 to END1 with START2 to END2.\n\
2651The regions may not be overlapping, because the size of the buffer is\n\
2652never changed in a transposition.\n\
2653\n\
ec1c14f6 2654Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update\n\
b229b8d1
RS
2655any markers that happen to be located in the regions.\n\
2656\n\
2657Transposing beyond buffer boundaries is an error.")
2658 (startr1, endr1, startr2, endr2, leave_markers)
2659 Lisp_Object startr1, endr1, startr2, endr2, leave_markers;
2660{
ec1c14f6
RS
2661 register int start1, end1, start2, end2;
2662 int start1_byte, start2_byte, len1_byte, len2_byte;
2663 int gap, len1, len_mid, len2;
3c6bc7d0 2664 unsigned char *start1_addr, *start2_addr, *temp;
dc3620af
RS
2665 int combined_before_bytes_1, combined_after_bytes_1;
2666 int combined_before_bytes_2, combined_after_bytes_2;
2667 struct gcpro gcpro1, gcpro2;
b229b8d1
RS
2668
2669#ifdef USE_TEXT_PROPERTIES
2670 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2;
1e158d25 2671 cur_intv = BUF_INTERVALS (current_buffer);
b229b8d1
RS
2672#endif /* USE_TEXT_PROPERTIES */
2673
2674 validate_region (&startr1, &endr1);
2675 validate_region (&startr2, &endr2);
2676
2677 start1 = XFASTINT (startr1);
2678 end1 = XFASTINT (endr1);
2679 start2 = XFASTINT (startr2);
2680 end2 = XFASTINT (endr2);
2681 gap = GPT;
2682
2683 /* Swap the regions if they're reversed. */
2684 if (start2 < end1)
2685 {
2686 register int glumph = start1;
2687 start1 = start2;
2688 start2 = glumph;
2689 glumph = end1;
2690 end1 = end2;
2691 end2 = glumph;
2692 }
2693
b229b8d1
RS
2694 len1 = end1 - start1;
2695 len2 = end2 - start2;
2696
2697 if (start2 < end1)
dc3620af 2698 error ("Transposed regions overlap");
b229b8d1 2699 else if (start1 == end1 || start2 == end2)
dc3620af 2700 error ("Transposed region has length 0");
b229b8d1
RS
2701
2702 /* The possibilities are:
2703 1. Adjacent (contiguous) regions, or separate but equal regions
2704 (no, really equal, in this case!), or
2705 2. Separate regions of unequal size.
2706
2707 The worst case is usually No. 2. It means that (aside from
2708 potential need for getting the gap out of the way), there also
2709 needs to be a shifting of the text between the two regions. So
2710 if they are spread far apart, we are that much slower... sigh. */
2711
2712 /* It must be pointed out that the really studly thing to do would
2713 be not to move the gap at all, but to leave it in place and work
2714 around it if necessary. This would be extremely efficient,
2715 especially considering that people are likely to do
2716 transpositions near where they are working interactively, which
2717 is exactly where the gap would be found. However, such code
2718 would be much harder to write and to read. So, if you are
2719 reading this comment and are feeling squirrely, by all means have
2720 a go! I just didn't feel like doing it, so I will simply move
2721 the gap the minimum distance to get it out of the way, and then
2722 deal with an unbroken array. */
3c6bc7d0
RS
2723
2724 /* Make sure the gap won't interfere, by moving it out of the text
2725 we will operate on. */
2726 if (start1 < gap && gap < end2)
2727 {
2728 if (gap - start1 < end2 - gap)
2729 move_gap (start1);
2730 else
2731 move_gap (end2);
2732 }
ec1c14f6
RS
2733
2734 start1_byte = CHAR_TO_BYTE (start1);
2735 start2_byte = CHAR_TO_BYTE (start2);
2736 len1_byte = CHAR_TO_BYTE (end1) - start1_byte;
2737 len2_byte = CHAR_TO_BYTE (end2) - start2_byte;
dc3620af
RS
2738
2739 if (end1 == start2)
2740 {
2741 combined_before_bytes_2
2742 = count_combining_before (BYTE_POS_ADDR (start2_byte),
2743 len2_byte, start1, start1_byte);
2744 combined_before_bytes_1
2745 = count_combining_before (BYTE_POS_ADDR (start1_byte),
2746 len1_byte, end2, start2_byte + len2_byte);
2747 combined_after_bytes_1
2748 = count_combining_after (BYTE_POS_ADDR (start1_byte),
2749 len1_byte, end2, start2_byte + len2_byte);
2750 combined_after_bytes_2 = 0;
2751 }
2752 else
2753 {
2754 combined_before_bytes_2
2755 = count_combining_before (BYTE_POS_ADDR (start2_byte),
2756 len2_byte, start1, start1_byte);
2757 combined_before_bytes_1
2758 = count_combining_before (BYTE_POS_ADDR (start1_byte),
2759 len1_byte, start2, start2_byte);
2760 combined_after_bytes_2
2761 = count_combining_after (BYTE_POS_ADDR (start2_byte),
2762 len2_byte, end1, start1_byte + len1_byte);
2763 combined_after_bytes_1
2764 = count_combining_after (BYTE_POS_ADDR (start1_byte),
2765 len1_byte, end2, start2_byte + len2_byte);
2766 }
2767
2768 /* If any combining is going to happen, do this the stupid way,
2769 because replace handles combining properly. */
2770 if (combined_before_bytes_1 || combined_before_bytes_2
2771 || combined_after_bytes_1 || combined_after_bytes_2)
2772 {
2773 Lisp_Object text1, text2;
2774
2775 text1 = text2 = Qnil;
2776 GCPRO2 (text1, text2);
2777
2778 text1 = make_buffer_string_both (start1, start1_byte,
2779 end1, start1_byte + len1_byte, 1);
2780 text2 = make_buffer_string_both (start2, start2_byte,
2781 end2, start2_byte + len2_byte, 1);
2782
2783 transpose_markers (start1, end1, start2, end2,
2784 start1_byte, start1_byte + len1_byte,
2785 start2_byte, start2_byte + len2_byte);
2786
8daf7106
AS
2787 replace_range (start2, end2, text1, 1, 0, 1);
2788 replace_range (start1, end1, text2, 1, 0, 1);
dc3620af
RS
2789
2790 UNGCPRO;
2791 return Qnil;
2792 }
b229b8d1
RS
2793
2794 /* Hmmm... how about checking to see if the gap is large
2795 enough to use as the temporary storage? That would avoid an
2796 allocation... interesting. Later, don't fool with it now. */
2797
2798 /* Working without memmove, for portability (sigh), so must be
2799 careful of overlapping subsections of the array... */
2800
2801 if (end1 == start2) /* adjacent regions */
2802 {
b229b8d1
RS
2803 modify_region (current_buffer, start1, end2);
2804 record_change (start1, len1 + len2);
2805
2806#ifdef USE_TEXT_PROPERTIES
2807 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2808 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
09dbcf71
RS
2809 Fset_text_properties (make_number (start1), make_number (end2),
2810 Qnil, Qnil);
b229b8d1
RS
2811#endif /* USE_TEXT_PROPERTIES */
2812
2813 /* First region smaller than second. */
ec1c14f6 2814 if (len1_byte < len2_byte)
b229b8d1 2815 {
3c6bc7d0
RS
2816 /* We use alloca only if it is small,
2817 because we want to avoid stack overflow. */
ec1c14f6
RS
2818 if (len2_byte > 20000)
2819 temp = (unsigned char *) xmalloc (len2_byte);
3c6bc7d0 2820 else
ec1c14f6 2821 temp = (unsigned char *) alloca (len2_byte);
03240d11
KH
2822
2823 /* Don't precompute these addresses. We have to compute them
2824 at the last minute, because the relocating allocator might
2825 have moved the buffer around during the xmalloc. */
ec1c14f6
RS
2826 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
2827 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
03240d11 2828
ec1c14f6
RS
2829 bcopy (start2_addr, temp, len2_byte);
2830 bcopy (start1_addr, start1_addr + len2_byte, len1_byte);
2831 bcopy (temp, start1_addr, len2_byte);
2832 if (len2_byte > 20000)
3c6bc7d0 2833 free (temp);
b229b8d1
RS
2834 }
2835 else
2836 /* First region not smaller than second. */
2837 {
ec1c14f6
RS
2838 if (len1_byte > 20000)
2839 temp = (unsigned char *) xmalloc (len1_byte);
3c6bc7d0 2840 else
ec1c14f6
RS
2841 temp = (unsigned char *) alloca (len1_byte);
2842 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
2843 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
2844 bcopy (start1_addr, temp, len1_byte);
2845 bcopy (start2_addr, start1_addr, len2_byte);
2846 bcopy (temp, start1_addr + len2_byte, len1_byte);
2847 if (len1_byte > 20000)
3c6bc7d0 2848 free (temp);
b229b8d1
RS
2849 }
2850#ifdef USE_TEXT_PROPERTIES
2851 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
2852 len1, current_buffer, 0);
2853 graft_intervals_into_buffer (tmp_interval2, start1,
2854 len2, current_buffer, 0);
2855#endif /* USE_TEXT_PROPERTIES */
2856 }
2857 /* Non-adjacent regions, because end1 != start2, bleagh... */
2858 else
2859 {
ec1c14f6
RS
2860 len_mid = start2_byte - (start1_byte + len1_byte);
2861
2862 if (len1_byte == len2_byte)
b229b8d1
RS
2863 /* Regions are same size, though, how nice. */
2864 {
2865 modify_region (current_buffer, start1, end1);
2866 modify_region (current_buffer, start2, end2);
2867 record_change (start1, len1);
2868 record_change (start2, len2);
2869#ifdef USE_TEXT_PROPERTIES
2870 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2871 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
09dbcf71
RS
2872 Fset_text_properties (make_number (start1), make_number (end1),
2873 Qnil, Qnil);
2874 Fset_text_properties (make_number (start2), make_number (end2),
2875 Qnil, Qnil);
b229b8d1
RS
2876#endif /* USE_TEXT_PROPERTIES */
2877
ec1c14f6
RS
2878 if (len1_byte > 20000)
2879 temp = (unsigned char *) xmalloc (len1_byte);
3c6bc7d0 2880 else
ec1c14f6
RS
2881 temp = (unsigned char *) alloca (len1_byte);
2882 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
2883 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
2884 bcopy (start1_addr, temp, len1_byte);
2885 bcopy (start2_addr, start1_addr, len2_byte);
2886 bcopy (temp, start2_addr, len1_byte);
2887 if (len1_byte > 20000)
3c6bc7d0 2888 free (temp);
b229b8d1
RS
2889#ifdef USE_TEXT_PROPERTIES
2890 graft_intervals_into_buffer (tmp_interval1, start2,
2891 len1, current_buffer, 0);
2892 graft_intervals_into_buffer (tmp_interval2, start1,
2893 len2, current_buffer, 0);
2894#endif /* USE_TEXT_PROPERTIES */
2895 }
2896
ec1c14f6 2897 else if (len1_byte < len2_byte) /* Second region larger than first */
b229b8d1
RS
2898 /* Non-adjacent & unequal size, area between must also be shifted. */
2899 {
b229b8d1
RS
2900 modify_region (current_buffer, start1, end2);
2901 record_change (start1, (end2 - start1));
2902#ifdef USE_TEXT_PROPERTIES
2903 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2904 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
2905 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
09dbcf71
RS
2906 Fset_text_properties (make_number (start1), make_number (end2),
2907 Qnil, Qnil);
b229b8d1
RS
2908#endif /* USE_TEXT_PROPERTIES */
2909
3c6bc7d0 2910 /* holds region 2 */
ec1c14f6
RS
2911 if (len2_byte > 20000)
2912 temp = (unsigned char *) xmalloc (len2_byte);
3c6bc7d0 2913 else
ec1c14f6
RS
2914 temp = (unsigned char *) alloca (len2_byte);
2915 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
2916 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
2917 bcopy (start2_addr, temp, len2_byte);
2918 bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte);
2919 safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
2920 bcopy (temp, start1_addr, len2_byte);
2921 if (len2_byte > 20000)
3c6bc7d0 2922 free (temp);
b229b8d1
RS
2923#ifdef USE_TEXT_PROPERTIES
2924 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
2925 len1, current_buffer, 0);
2926 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
2927 len_mid, current_buffer, 0);
2928 graft_intervals_into_buffer (tmp_interval2, start1,
2929 len2, current_buffer, 0);
2930#endif /* USE_TEXT_PROPERTIES */
2931 }
2932 else
2933 /* Second region smaller than first. */
2934 {
b229b8d1
RS
2935 record_change (start1, (end2 - start1));
2936 modify_region (current_buffer, start1, end2);
2937
2938#ifdef USE_TEXT_PROPERTIES
2939 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2940 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
2941 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
09dbcf71
RS
2942 Fset_text_properties (make_number (start1), make_number (end2),
2943 Qnil, Qnil);
b229b8d1
RS
2944#endif /* USE_TEXT_PROPERTIES */
2945
3c6bc7d0 2946 /* holds region 1 */
ec1c14f6
RS
2947 if (len1_byte > 20000)
2948 temp = (unsigned char *) xmalloc (len1_byte);
3c6bc7d0 2949 else
ec1c14f6
RS
2950 temp = (unsigned char *) alloca (len1_byte);
2951 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte);
2952 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte);
2953 bcopy (start1_addr, temp, len1_byte);
2954 bcopy (start2_addr, start1_addr, len2_byte);
2955 bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
2956 bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte);
2957 if (len1_byte > 20000)
3c6bc7d0 2958 free (temp);
b229b8d1
RS
2959#ifdef USE_TEXT_PROPERTIES
2960 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
2961 len1, current_buffer, 0);
2962 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
2963 len_mid, current_buffer, 0);
2964 graft_intervals_into_buffer (tmp_interval2, start1,
2965 len2, current_buffer, 0);
2966#endif /* USE_TEXT_PROPERTIES */
2967 }
2968 }
2969
ec1c14f6
RS
2970 /* When doing multiple transpositions, it might be nice
2971 to optimize this. Perhaps the markers in any one buffer
2972 should be organized in some sorted data tree. */
b229b8d1 2973 if (NILP (leave_markers))
8de1d5f0 2974 {
ec1c14f6
RS
2975 transpose_markers (start1, end1, start2, end2,
2976 start1_byte, start1_byte + len1_byte,
2977 start2_byte, start2_byte + len2_byte);
8de1d5f0
KH
2978 fix_overlays_in_range (start1, end2);
2979 }
b229b8d1
RS
2980
2981 return Qnil;
2982}
35692fe0 2983
35692fe0
JB
2984\f
2985void
2986syms_of_editfns ()
2987{
260e2e2a
KH
2988 environbuf = 0;
2989
2990 Qbuffer_access_fontify_functions
2991 = intern ("buffer-access-fontify-functions");
2992 staticpro (&Qbuffer_access_fontify_functions);
2993
2994 DEFVAR_LISP ("buffer-access-fontify-functions",
2995 &Vbuffer_access_fontify_functions,
2996 "List of functions called by `buffer-substring' to fontify if necessary.\n\
2997Each function is called with two arguments which specify the range\n\
2998of the buffer being accessed.");
2999 Vbuffer_access_fontify_functions = Qnil;
3000
af209db8
RS
3001 {
3002 Lisp_Object obuf;
3003 extern Lisp_Object Vprin1_to_string_buffer;
3004 obuf = Fcurrent_buffer ();
3005 /* Do this here, because init_buffer_once is too early--it won't work. */
3006 Fset_buffer (Vprin1_to_string_buffer);
3007 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
3008 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
3009 Qnil);
3010 Fset_buffer (obuf);
3011 }
3012
0b6fd023 3013 DEFVAR_LISP ("buffer-access-fontified-property",
260e2e2a
KH
3014 &Vbuffer_access_fontified_property,
3015 "Property which (if non-nil) indicates text has been fontified.\n\
3016`buffer-substring' need not call the `buffer-access-fontify-functions'\n\
3017functions if all the text being accessed has this property.");
3018 Vbuffer_access_fontified_property = Qnil;
3019
f43754f6
KH
3020 DEFVAR_LISP ("system-name", &Vsystem_name,
3021 "The name of the machine Emacs is running on.");
3022
3023 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
3024 "The full name of the user logged in.");
3025
35b34f72 3026 DEFVAR_LISP ("user-login-name", &Vuser_login_name,
f43754f6
KH
3027 "The user's name, taken from environment variables if possible.");
3028
35b34f72 3029 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name,
f43754f6 3030 "The user's name, based upon the real uid only.");
35692fe0
JB
3031
3032 defsubr (&Schar_equal);
3033 defsubr (&Sgoto_char);
3034 defsubr (&Sstring_to_char);
3035 defsubr (&Schar_to_string);
3036 defsubr (&Sbuffer_substring);
260e2e2a 3037 defsubr (&Sbuffer_substring_no_properties);
35692fe0
JB
3038 defsubr (&Sbuffer_string);
3039
3040 defsubr (&Spoint_marker);
3041 defsubr (&Smark_marker);
3042 defsubr (&Spoint);
3043 defsubr (&Sregion_beginning);
3044 defsubr (&Sregion_end);
7df74da6
RS
3045
3046 defsubr (&Sline_beginning_position);
3047 defsubr (&Sline_end_position);
3048
35692fe0
JB
3049/* defsubr (&Smark); */
3050/* defsubr (&Sset_mark); */
3051 defsubr (&Ssave_excursion);
4bc8c7d2 3052 defsubr (&Ssave_current_buffer);
35692fe0
JB
3053
3054 defsubr (&Sbufsize);
3055 defsubr (&Spoint_max);
3056 defsubr (&Spoint_min);
3057 defsubr (&Spoint_min_marker);
3058 defsubr (&Spoint_max_marker);
7df74da6 3059 defsubr (&Sposition_bytes);
c9ed721d 3060
35692fe0
JB
3061 defsubr (&Sbobp);
3062 defsubr (&Seobp);
3063 defsubr (&Sbolp);
3064 defsubr (&Seolp);
850a8179
JB
3065 defsubr (&Sfollowing_char);
3066 defsubr (&Sprevious_char);
35692fe0 3067 defsubr (&Schar_after);
fb8106e8 3068 defsubr (&Schar_before);
35692fe0
JB
3069 defsubr (&Sinsert);
3070 defsubr (&Sinsert_before_markers);
be91036a
RS
3071 defsubr (&Sinsert_and_inherit);
3072 defsubr (&Sinsert_and_inherit_before_markers);
35692fe0
JB
3073 defsubr (&Sinsert_char);
3074
3075 defsubr (&Suser_login_name);
3076 defsubr (&Suser_real_login_name);
3077 defsubr (&Suser_uid);
3078 defsubr (&Suser_real_uid);
3079 defsubr (&Suser_full_name);
7fd233b3 3080 defsubr (&Semacs_pid);
d940e0e4 3081 defsubr (&Scurrent_time);
a82d387c 3082 defsubr (&Sformat_time_string);
4691c06d 3083 defsubr (&Sdecode_time);
cce7b8a0 3084 defsubr (&Sencode_time);
35692fe0 3085 defsubr (&Scurrent_time_string);
c2662aea 3086 defsubr (&Scurrent_time_zone);
143cb9a9 3087 defsubr (&Sset_time_zone_rule);
35692fe0 3088 defsubr (&Ssystem_name);
35692fe0 3089 defsubr (&Smessage);
cacc3e2c
RS
3090 defsubr (&Smessage_box);
3091 defsubr (&Smessage_or_box);
b14dda8a 3092 defsubr (&Scurrent_message);
35692fe0 3093 defsubr (&Sformat);
35692fe0
JB
3094
3095 defsubr (&Sinsert_buffer_substring);
e9cf2084 3096 defsubr (&Scompare_buffer_substrings);
35692fe0
JB
3097 defsubr (&Ssubst_char_in_region);
3098 defsubr (&Stranslate_region);
3099 defsubr (&Sdelete_region);
3100 defsubr (&Swiden);
3101 defsubr (&Snarrow_to_region);
3102 defsubr (&Ssave_restriction);
b229b8d1 3103 defsubr (&Stranspose_regions);
35692fe0 3104}