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