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