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