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