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