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