Repeat the argument list of format-time-string in the
[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 (format_string, time, universal)
858 */
859
860 DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
861 0 /* See immediately above */)
862 (format_string, time, universal)
863 Lisp_Object format_string, time, universal;
864 {
865 time_t value;
866 int size;
867
868 CHECK_STRING (format_string, 1);
869
870 if (! lisp_time_argument (time, &value))
871 error ("Invalid time specification");
872
873 /* This is probably enough. */
874 size = XSTRING (format_string)->size * 6 + 50;
875
876 while (1)
877 {
878 char *buf = (char *) alloca (size + 1);
879 int result;
880
881 buf[0] = '\1';
882 result = emacs_strftime (buf, size, XSTRING (format_string)->data,
883 (NILP (universal) ? localtime (&value)
884 : gmtime (&value)));
885 if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
886 return build_string (buf);
887
888 /* If buffer was too small, make it bigger and try again. */
889 result = emacs_strftime (NULL, 0x7fffffff, XSTRING (format_string)->data,
890 (NILP (universal) ? localtime (&value)
891 : gmtime (&value)));
892 size = result + 1;
893 }
894 }
895
896 DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
897 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
898 The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
899 or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
900 to use the current time. The list has the following nine members:\n\
901 SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
902 only some operating systems support. MINUTE is an integer between 0 and 59.\n\
903 HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
904 MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
905 four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
906 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
907 ZONE is an integer indicating the number of seconds east of Greenwich.\n\
908 \(Note that Common Lisp has different meanings for DOW and ZONE.)")
909 (specified_time)
910 Lisp_Object specified_time;
911 {
912 time_t time_spec;
913 struct tm save_tm;
914 struct tm *decoded_time;
915 Lisp_Object list_args[9];
916
917 if (! lisp_time_argument (specified_time, &time_spec))
918 error ("Invalid time specification");
919
920 decoded_time = localtime (&time_spec);
921 XSETFASTINT (list_args[0], decoded_time->tm_sec);
922 XSETFASTINT (list_args[1], decoded_time->tm_min);
923 XSETFASTINT (list_args[2], decoded_time->tm_hour);
924 XSETFASTINT (list_args[3], decoded_time->tm_mday);
925 XSETFASTINT (list_args[4], decoded_time->tm_mon + 1);
926 XSETINT (list_args[5], decoded_time->tm_year + 1900);
927 XSETFASTINT (list_args[6], decoded_time->tm_wday);
928 list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
929
930 /* Make a copy, in case gmtime modifies the struct. */
931 save_tm = *decoded_time;
932 decoded_time = gmtime (&time_spec);
933 if (decoded_time == 0)
934 list_args[8] = Qnil;
935 else
936 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
937 return Flist (9, list_args);
938 }
939
940 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
941 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
942 This is the reverse operation of `decode-time', which see.\n\
943 ZONE defaults to the current time zone rule. This can\n\
944 be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
945 \(as from `current-time-zone') or an integer (as from `decode-time')\n\
946 applied without consideration for daylight savings time.\n\
947 \n\
948 You can pass more than 7 arguments; then the first six arguments\n\
949 are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
950 The intervening arguments are ignored.\n\
951 This feature lets (apply 'encode-time (decode-time ...)) work.\n\
952 \n\
953 Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
954 for example, a DAY of 0 means the day preceding the given month.\n\
955 Year numbers less than 100 are treated just like other year numbers.\n\
956 If you want them to stand for years in this century, you must do that yourself.")
957 (nargs, args)
958 int nargs;
959 register Lisp_Object *args;
960 {
961 time_t time;
962 struct tm tm;
963 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
964
965 CHECK_NUMBER (args[0], 0); /* second */
966 CHECK_NUMBER (args[1], 1); /* minute */
967 CHECK_NUMBER (args[2], 2); /* hour */
968 CHECK_NUMBER (args[3], 3); /* day */
969 CHECK_NUMBER (args[4], 4); /* month */
970 CHECK_NUMBER (args[5], 5); /* year */
971
972 tm.tm_sec = XINT (args[0]);
973 tm.tm_min = XINT (args[1]);
974 tm.tm_hour = XINT (args[2]);
975 tm.tm_mday = XINT (args[3]);
976 tm.tm_mon = XINT (args[4]) - 1;
977 tm.tm_year = XINT (args[5]) - 1900;
978 tm.tm_isdst = -1;
979
980 if (CONSP (zone))
981 zone = Fcar (zone);
982 if (NILP (zone))
983 time = mktime (&tm);
984 else
985 {
986 char tzbuf[100];
987 char *tzstring;
988 char **oldenv = environ, **newenv;
989
990 if (EQ (zone, Qt))
991 tzstring = "UTC0";
992 else if (STRINGP (zone))
993 tzstring = (char *) XSTRING (zone)->data;
994 else if (INTEGERP (zone))
995 {
996 int abszone = abs (XINT (zone));
997 sprintf (tzbuf, "XXX%s%d:%02d:%02d", "-" + (XINT (zone) < 0),
998 abszone / (60*60), (abszone/60) % 60, abszone % 60);
999 tzstring = tzbuf;
1000 }
1001 else
1002 error ("Invalid time zone specification");
1003
1004 /* Set TZ before calling mktime; merely adjusting mktime's returned
1005 value doesn't suffice, since that would mishandle leap seconds. */
1006 set_time_zone_rule (tzstring);
1007
1008 time = mktime (&tm);
1009
1010 /* Restore TZ to previous value. */
1011 newenv = environ;
1012 environ = oldenv;
1013 xfree (newenv);
1014 #ifdef LOCALTIME_CACHE
1015 tzset ();
1016 #endif
1017 }
1018
1019 if (time == (time_t) -1)
1020 error ("Specified time is not representable");
1021
1022 return make_time (time);
1023 }
1024
1025 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
1026 "Return the current time, as a human-readable string.\n\
1027 Programs can use this function to decode a time,\n\
1028 since the number of columns in each field is fixed.\n\
1029 The format is `Sun Sep 16 01:03:52 1973'.\n\
1030 However, see also the functions `decode-time' and `format-time-string'\n\
1031 which provide a much more powerful and general facility.\n\
1032 \n\
1033 If an argument is given, it specifies a time to format\n\
1034 instead of the current time. The argument should have the form:\n\
1035 (HIGH . LOW)\n\
1036 or the form:\n\
1037 (HIGH LOW . IGNORED).\n\
1038 Thus, you can use times obtained from `current-time'\n\
1039 and from `file-attributes'.")
1040 (specified_time)
1041 Lisp_Object specified_time;
1042 {
1043 time_t value;
1044 char buf[30];
1045 register char *tem;
1046
1047 if (! lisp_time_argument (specified_time, &value))
1048 value = -1;
1049 tem = (char *) ctime (&value);
1050
1051 strncpy (buf, tem, 24);
1052 buf[24] = 0;
1053
1054 return build_string (buf);
1055 }
1056
1057 #define TM_YEAR_BASE 1900
1058
1059 /* Yield A - B, measured in seconds.
1060 This function is copied from the GNU C Library. */
1061 static int
1062 tm_diff (a, b)
1063 struct tm *a, *b;
1064 {
1065 /* Compute intervening leap days correctly even if year is negative.
1066 Take care to avoid int overflow in leap day calculations,
1067 but it's OK to assume that A and B are close to each other. */
1068 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
1069 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
1070 int a100 = a4 / 25 - (a4 % 25 < 0);
1071 int b100 = b4 / 25 - (b4 % 25 < 0);
1072 int a400 = a100 >> 2;
1073 int b400 = b100 >> 2;
1074 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
1075 int years = a->tm_year - b->tm_year;
1076 int days = (365 * years + intervening_leap_days
1077 + (a->tm_yday - b->tm_yday));
1078 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
1079 + (a->tm_min - b->tm_min))
1080 + (a->tm_sec - b->tm_sec));
1081 }
1082
1083 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
1084 "Return the offset and name for the local time zone.\n\
1085 This returns a list of the form (OFFSET NAME).\n\
1086 OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1087 A negative value means west of Greenwich.\n\
1088 NAME is a string giving the name of the time zone.\n\
1089 If an argument is given, it specifies when the time zone offset is determined\n\
1090 instead of using the current time. The argument should have the form:\n\
1091 (HIGH . LOW)\n\
1092 or the form:\n\
1093 (HIGH LOW . IGNORED).\n\
1094 Thus, you can use times obtained from `current-time'\n\
1095 and from `file-attributes'.\n\
1096 \n\
1097 Some operating systems cannot provide all this information to Emacs;\n\
1098 in this case, `current-time-zone' returns a list containing nil for\n\
1099 the data it can't find.")
1100 (specified_time)
1101 Lisp_Object specified_time;
1102 {
1103 time_t value;
1104 struct tm *t;
1105
1106 if (lisp_time_argument (specified_time, &value)
1107 && (t = gmtime (&value)) != 0)
1108 {
1109 struct tm gmt;
1110 int offset;
1111 char *s, buf[6];
1112
1113 gmt = *t; /* Make a copy, in case localtime modifies *t. */
1114 t = localtime (&value);
1115 offset = tm_diff (t, &gmt);
1116 s = 0;
1117 #ifdef HAVE_TM_ZONE
1118 if (t->tm_zone)
1119 s = (char *)t->tm_zone;
1120 #else /* not HAVE_TM_ZONE */
1121 #ifdef HAVE_TZNAME
1122 if (t->tm_isdst == 0 || t->tm_isdst == 1)
1123 s = tzname[t->tm_isdst];
1124 #endif
1125 #endif /* not HAVE_TM_ZONE */
1126 if (!s)
1127 {
1128 /* No local time zone name is available; use "+-NNNN" instead. */
1129 int am = (offset < 0 ? -offset : offset) / 60;
1130 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
1131 s = buf;
1132 }
1133 return Fcons (make_number (offset), Fcons (build_string (s), Qnil));
1134 }
1135 else
1136 return Fmake_list (make_number (2), Qnil);
1137 }
1138
1139 /* This holds the value of `environ' produced by the previous
1140 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1141 has never been called. */
1142 static char **environbuf;
1143
1144 DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
1145 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
1146 If TZ is nil, use implementation-defined default time zone information.\n\
1147 If TZ is t, use Universal Time.")
1148 (tz)
1149 Lisp_Object tz;
1150 {
1151 char *tzstring;
1152
1153 if (NILP (tz))
1154 tzstring = 0;
1155 else if (EQ (tz, Qt))
1156 tzstring = "UTC0";
1157 else
1158 {
1159 CHECK_STRING (tz, 0);
1160 tzstring = (char *) XSTRING (tz)->data;
1161 }
1162
1163 set_time_zone_rule (tzstring);
1164 if (environbuf)
1165 free (environbuf);
1166 environbuf = environ;
1167
1168 return Qnil;
1169 }
1170
1171 #ifdef LOCALTIME_CACHE
1172
1173 /* These two values are known to load tz files in buggy implementations,
1174 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1175 Their values shouldn't matter in non-buggy implementations.
1176 We don't use string literals for these strings,
1177 since if a string in the environment is in readonly
1178 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1179 See Sun bugs 1113095 and 1114114, ``Timezone routines
1180 improperly modify environment''. */
1181
1182 static char set_time_zone_rule_tz1[] = "TZ=GMT+0";
1183 static char set_time_zone_rule_tz2[] = "TZ=GMT+1";
1184
1185 #endif
1186
1187 /* Set the local time zone rule to TZSTRING.
1188 This allocates memory into `environ', which it is the caller's
1189 responsibility to free. */
1190 void
1191 set_time_zone_rule (tzstring)
1192 char *tzstring;
1193 {
1194 int envptrs;
1195 char **from, **to, **newenv;
1196
1197 /* Make the ENVIRON vector longer with room for TZSTRING. */
1198 for (from = environ; *from; from++)
1199 continue;
1200 envptrs = from - environ + 2;
1201 newenv = to = (char **) xmalloc (envptrs * sizeof (char *)
1202 + (tzstring ? strlen (tzstring) + 4 : 0));
1203
1204 /* Add TZSTRING to the end of environ, as a value for TZ. */
1205 if (tzstring)
1206 {
1207 char *t = (char *) (to + envptrs);
1208 strcpy (t, "TZ=");
1209 strcat (t, tzstring);
1210 *to++ = t;
1211 }
1212
1213 /* Copy the old environ vector elements into NEWENV,
1214 but don't copy the TZ variable.
1215 So we have only one definition of TZ, which came from TZSTRING. */
1216 for (from = environ; *from; from++)
1217 if (strncmp (*from, "TZ=", 3) != 0)
1218 *to++ = *from;
1219 *to = 0;
1220
1221 environ = newenv;
1222
1223 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1224 the TZ variable is stored. If we do not have a TZSTRING,
1225 TO points to the vector slot which has the terminating null. */
1226
1227 #ifdef LOCALTIME_CACHE
1228 {
1229 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1230 "US/Pacific" that loads a tz file, then changes to a value like
1231 "XXX0" that does not load a tz file, and then changes back to
1232 its original value, the last change is (incorrectly) ignored.
1233 Also, if TZ changes twice in succession to values that do
1234 not load a tz file, tzset can dump core (see Sun bug#1225179).
1235 The following code works around these bugs. */
1236
1237 if (tzstring)
1238 {
1239 /* Temporarily set TZ to a value that loads a tz file
1240 and that differs from tzstring. */
1241 char *tz = *newenv;
1242 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
1243 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
1244 tzset ();
1245 *newenv = tz;
1246 }
1247 else
1248 {
1249 /* The implied tzstring is unknown, so temporarily set TZ to
1250 two different values that each load a tz file. */
1251 *to = set_time_zone_rule_tz1;
1252 to[1] = 0;
1253 tzset ();
1254 *to = set_time_zone_rule_tz2;
1255 tzset ();
1256 *to = 0;
1257 }
1258
1259 /* Now TZ has the desired value, and tzset can be invoked safely. */
1260 }
1261
1262 tzset ();
1263 #endif
1264 }
1265 \f
1266 /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1267 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1268 type of object is Lisp_String). INHERIT is passed to
1269 INSERT_FROM_STRING_FUNC as the last argument. */
1270
1271 general_insert_function (insert_func, insert_from_string_func,
1272 inherit, nargs, args)
1273 int (*insert_func)(), (*insert_from_string_func)();
1274 int inherit, nargs;
1275 register Lisp_Object *args;
1276 {
1277 register int argnum;
1278 register Lisp_Object val;
1279
1280 for (argnum = 0; argnum < nargs; argnum++)
1281 {
1282 val = args[argnum];
1283 retry:
1284 if (INTEGERP (val))
1285 {
1286 char workbuf[4], *str;
1287 int len;
1288
1289 if (!NILP (current_buffer->enable_multibyte_characters))
1290 len = CHAR_STRING (XFASTINT (val), workbuf, str);
1291 else
1292 workbuf[0] = XINT (val), str = workbuf, len = 1;
1293 (*insert_func) (str, len);
1294 }
1295 else if (STRINGP (val))
1296 {
1297 (*insert_from_string_func) (val, 0, XSTRING (val)->size, inherit);
1298 }
1299 else
1300 {
1301 val = wrong_type_argument (Qchar_or_string_p, val);
1302 goto retry;
1303 }
1304 }
1305 }
1306
1307 void
1308 insert1 (arg)
1309 Lisp_Object arg;
1310 {
1311 Finsert (1, &arg);
1312 }
1313
1314
1315 /* Callers passing one argument to Finsert need not gcpro the
1316 argument "array", since the only element of the array will
1317 not be used after calling insert or insert_from_string, so
1318 we don't care if it gets trashed. */
1319
1320 DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
1321 "Insert the arguments, either strings or characters, at point.\n\
1322 Point and before-insertion-markers move forward so that it ends up\n\
1323 after the inserted text.\n\
1324 Any other markers at the point of insertion remain before the text.")
1325 (nargs, args)
1326 int nargs;
1327 register Lisp_Object *args;
1328 {
1329 general_insert_function (insert, insert_from_string, 0, nargs, args);
1330 return Qnil;
1331 }
1332
1333 DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
1334 0, MANY, 0,
1335 "Insert the arguments at point, inheriting properties from adjoining text.\n\
1336 Point and before-insertion-markers move forward so that it ends up\n\
1337 after the inserted text.\n\
1338 Any other markers at the point of insertion remain before the text.")
1339 (nargs, args)
1340 int nargs;
1341 register Lisp_Object *args;
1342 {
1343 general_insert_function (insert_and_inherit, insert_from_string, 1,
1344 nargs, args);
1345 return Qnil;
1346 }
1347
1348 DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
1349 "Insert strings or characters at point, relocating markers after the text.\n\
1350 Point and before-insertion-markers move forward so that it ends up\n\
1351 after the inserted text.\n\
1352 Any other markers at the point of insertion also end up after the text.")
1353 (nargs, args)
1354 int nargs;
1355 register Lisp_Object *args;
1356 {
1357 general_insert_function (insert_before_markers,
1358 insert_from_string_before_markers, 0,
1359 nargs, args);
1360 return Qnil;
1361 }
1362
1363 DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
1364 Sinsert_and_inherit_before_markers, 0, MANY, 0,
1365 "Insert text at point, relocating markers and inheriting properties.\n\
1366 Point moves forward so that it ends up after the inserted text.\n\
1367 Any other markers at the point of insertion also end up after the text.")
1368 (nargs, args)
1369 int nargs;
1370 register Lisp_Object *args;
1371 {
1372 general_insert_function (insert_before_markers_and_inherit,
1373 insert_from_string_before_markers, 1,
1374 nargs, args);
1375 return Qnil;
1376 }
1377 \f
1378 DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
1379 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
1380 Point and before-insertion-markers are affected as in the function `insert'.\n\
1381 Both arguments are required.\n\
1382 The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1383 from adjoining text, if those properties are sticky.")
1384 (character, count, inherit)
1385 Lisp_Object character, count, inherit;
1386 {
1387 register unsigned char *string;
1388 register int strlen;
1389 register int i, n;
1390 int len;
1391 unsigned char workbuf[4], *str;
1392
1393 CHECK_NUMBER (character, 0);
1394 CHECK_NUMBER (count, 1);
1395
1396 if (!NILP (current_buffer->enable_multibyte_characters))
1397 len = CHAR_STRING (XFASTINT (character), workbuf, str);
1398 else
1399 workbuf[0] = XFASTINT (character), str = workbuf, len = 1;
1400 n = XINT (count) * len;
1401 if (n <= 0)
1402 return Qnil;
1403 strlen = min (n, 256 * len);
1404 string = (unsigned char *) alloca (strlen);
1405 for (i = 0; i < strlen; i++)
1406 string[i] = str[i % len];
1407 while (n >= strlen)
1408 {
1409 QUIT;
1410 if (!NILP (inherit))
1411 insert_and_inherit (string, strlen);
1412 else
1413 insert (string, strlen);
1414 n -= strlen;
1415 }
1416 if (n > 0)
1417 {
1418 if (!NILP (inherit))
1419 insert_and_inherit (string, n);
1420 else
1421 insert (string, n);
1422 }
1423 return Qnil;
1424 }
1425
1426 \f
1427 /* Making strings from buffer contents. */
1428
1429 /* Return a Lisp_String containing the text of the current buffer from
1430 START to END. If text properties are in use and the current buffer
1431 has properties in the range specified, the resulting string will also
1432 have them, if PROPS is nonzero.
1433
1434 We don't want to use plain old make_string here, because it calls
1435 make_uninit_string, which can cause the buffer arena to be
1436 compacted. make_string has no way of knowing that the data has
1437 been moved, and thus copies the wrong data into the string. This
1438 doesn't effect most of the other users of make_string, so it should
1439 be left as is. But we should use this function when conjuring
1440 buffer substrings. */
1441
1442 Lisp_Object
1443 make_buffer_string (start, end, props)
1444 int start, end;
1445 int props;
1446 {
1447 Lisp_Object result, tem, tem1;
1448
1449 if (start < GPT && GPT < end)
1450 move_gap (start);
1451
1452 result = make_uninit_string (end - start);
1453 bcopy (POS_ADDR (start), XSTRING (result)->data, end - start);
1454
1455 /* If desired, update and copy the text properties. */
1456 #ifdef USE_TEXT_PROPERTIES
1457 if (props)
1458 {
1459 update_buffer_properties (start, end);
1460
1461 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
1462 tem1 = Ftext_properties_at (make_number (start), Qnil);
1463
1464 if (XINT (tem) != end || !NILP (tem1))
1465 copy_intervals_to_string (result, current_buffer, start, end - start);
1466 }
1467 #endif
1468
1469 return result;
1470 }
1471
1472 /* Call Vbuffer_access_fontify_functions for the range START ... END
1473 in the current buffer, if necessary. */
1474
1475 static void
1476 update_buffer_properties (start, end)
1477 int start, end;
1478 {
1479 #ifdef USE_TEXT_PROPERTIES
1480 /* If this buffer has some access functions,
1481 call them, specifying the range of the buffer being accessed. */
1482 if (!NILP (Vbuffer_access_fontify_functions))
1483 {
1484 Lisp_Object args[3];
1485 Lisp_Object tem;
1486
1487 args[0] = Qbuffer_access_fontify_functions;
1488 XSETINT (args[1], start);
1489 XSETINT (args[2], end);
1490
1491 /* But don't call them if we can tell that the work
1492 has already been done. */
1493 if (!NILP (Vbuffer_access_fontified_property))
1494 {
1495 tem = Ftext_property_any (args[1], args[2],
1496 Vbuffer_access_fontified_property,
1497 Qnil, Qnil);
1498 if (! NILP (tem))
1499 Frun_hook_with_args (3, args);
1500 }
1501 else
1502 Frun_hook_with_args (3, args);
1503 }
1504 #endif
1505 }
1506
1507 DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
1508 "Return the contents of part of the current buffer as a string.\n\
1509 The two arguments START and END are character positions;\n\
1510 they can be in either order.")
1511 (start, end)
1512 Lisp_Object start, end;
1513 {
1514 register int b, e;
1515
1516 validate_region (&start, &end);
1517 b = XINT (start);
1518 e = XINT (end);
1519
1520 return make_buffer_string (b, e, 1);
1521 }
1522
1523 DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
1524 Sbuffer_substring_no_properties, 2, 2, 0,
1525 "Return the characters of part of the buffer, without the text properties.\n\
1526 The two arguments START and END are character positions;\n\
1527 they can be in either order.")
1528 (start, end)
1529 Lisp_Object start, end;
1530 {
1531 register int b, e;
1532
1533 validate_region (&start, &end);
1534 b = XINT (start);
1535 e = XINT (end);
1536
1537 return make_buffer_string (b, e, 0);
1538 }
1539
1540 DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
1541 "Return the contents of the current buffer as a string.\n\
1542 If narrowing is in effect, this function returns only the visible part\n\
1543 of the buffer.")
1544 ()
1545 {
1546 return make_buffer_string (BEGV, ZV, 1);
1547 }
1548
1549 DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
1550 1, 3, 0,
1551 "Insert before point a substring of the contents of buffer BUFFER.\n\
1552 BUFFER may be a buffer or a buffer name.\n\
1553 Arguments START and END are character numbers specifying the substring.\n\
1554 They default to the beginning and the end of BUFFER.")
1555 (buf, start, end)
1556 Lisp_Object buf, start, end;
1557 {
1558 register int b, e, temp;
1559 register struct buffer *bp, *obuf;
1560 Lisp_Object buffer;
1561
1562 buffer = Fget_buffer (buf);
1563 if (NILP (buffer))
1564 nsberror (buf);
1565 bp = XBUFFER (buffer);
1566 if (NILP (bp->name))
1567 error ("Selecting deleted buffer");
1568
1569 if (NILP (start))
1570 b = BUF_BEGV (bp);
1571 else
1572 {
1573 CHECK_NUMBER_COERCE_MARKER (start, 0);
1574 b = XINT (start);
1575 }
1576 if (NILP (end))
1577 e = BUF_ZV (bp);
1578 else
1579 {
1580 CHECK_NUMBER_COERCE_MARKER (end, 1);
1581 e = XINT (end);
1582 }
1583
1584 if (b > e)
1585 temp = b, b = e, e = temp;
1586
1587 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
1588 args_out_of_range (start, end);
1589
1590 obuf = current_buffer;
1591 set_buffer_internal_1 (bp);
1592 update_buffer_properties (b, e);
1593 set_buffer_internal_1 (obuf);
1594
1595 insert_from_buffer (bp, b, e - b, 0);
1596 return Qnil;
1597 }
1598
1599 DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
1600 6, 6, 0,
1601 "Compare two substrings of two buffers; return result as number.\n\
1602 the value is -N if first string is less after N-1 chars,\n\
1603 +N if first string is greater after N-1 chars, or 0 if strings match.\n\
1604 Each substring is represented as three arguments: BUFFER, START and END.\n\
1605 That makes six args in all, three for each substring.\n\n\
1606 The value of `case-fold-search' in the current buffer\n\
1607 determines whether case is significant or ignored.")
1608 (buffer1, start1, end1, buffer2, start2, end2)
1609 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
1610 {
1611 register int begp1, endp1, begp2, endp2, temp, len1, len2, length, i;
1612 register struct buffer *bp1, *bp2;
1613 register Lisp_Object *trt
1614 = (!NILP (current_buffer->case_fold_search)
1615 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0);
1616
1617 /* Find the first buffer and its substring. */
1618
1619 if (NILP (buffer1))
1620 bp1 = current_buffer;
1621 else
1622 {
1623 Lisp_Object buf1;
1624 buf1 = Fget_buffer (buffer1);
1625 if (NILP (buf1))
1626 nsberror (buffer1);
1627 bp1 = XBUFFER (buf1);
1628 if (NILP (bp1->name))
1629 error ("Selecting deleted buffer");
1630 }
1631
1632 if (NILP (start1))
1633 begp1 = BUF_BEGV (bp1);
1634 else
1635 {
1636 CHECK_NUMBER_COERCE_MARKER (start1, 1);
1637 begp1 = XINT (start1);
1638 }
1639 if (NILP (end1))
1640 endp1 = BUF_ZV (bp1);
1641 else
1642 {
1643 CHECK_NUMBER_COERCE_MARKER (end1, 2);
1644 endp1 = XINT (end1);
1645 }
1646
1647 if (begp1 > endp1)
1648 temp = begp1, begp1 = endp1, endp1 = temp;
1649
1650 if (!(BUF_BEGV (bp1) <= begp1
1651 && begp1 <= endp1
1652 && endp1 <= BUF_ZV (bp1)))
1653 args_out_of_range (start1, end1);
1654
1655 /* Likewise for second substring. */
1656
1657 if (NILP (buffer2))
1658 bp2 = current_buffer;
1659 else
1660 {
1661 Lisp_Object buf2;
1662 buf2 = Fget_buffer (buffer2);
1663 if (NILP (buf2))
1664 nsberror (buffer2);
1665 bp2 = XBUFFER (buf2);
1666 if (NILP (bp2->name))
1667 error ("Selecting deleted buffer");
1668 }
1669
1670 if (NILP (start2))
1671 begp2 = BUF_BEGV (bp2);
1672 else
1673 {
1674 CHECK_NUMBER_COERCE_MARKER (start2, 4);
1675 begp2 = XINT (start2);
1676 }
1677 if (NILP (end2))
1678 endp2 = BUF_ZV (bp2);
1679 else
1680 {
1681 CHECK_NUMBER_COERCE_MARKER (end2, 5);
1682 endp2 = XINT (end2);
1683 }
1684
1685 if (begp2 > endp2)
1686 temp = begp2, begp2 = endp2, endp2 = temp;
1687
1688 if (!(BUF_BEGV (bp2) <= begp2
1689 && begp2 <= endp2
1690 && endp2 <= BUF_ZV (bp2)))
1691 args_out_of_range (start2, end2);
1692
1693 len1 = endp1 - begp1;
1694 len2 = endp2 - begp2;
1695 length = len1;
1696 if (len2 < length)
1697 length = len2;
1698
1699 for (i = 0; i < length; i++)
1700 {
1701 int c1 = *BUF_CHAR_ADDRESS (bp1, begp1 + i);
1702 int c2 = *BUF_CHAR_ADDRESS (bp2, begp2 + i);
1703 if (trt)
1704 {
1705 c1 = XINT (trt[c1]);
1706 c2 = XINT (trt[c2]);
1707 }
1708 if (c1 < c2)
1709 return make_number (- 1 - i);
1710 if (c1 > c2)
1711 return make_number (i + 1);
1712 }
1713
1714 /* The strings match as far as they go.
1715 If one is shorter, that one is less. */
1716 if (length < len1)
1717 return make_number (length + 1);
1718 else if (length < len2)
1719 return make_number (- length - 1);
1720
1721 /* Same length too => they are equal. */
1722 return make_number (0);
1723 }
1724 \f
1725 static Lisp_Object
1726 subst_char_in_region_unwind (arg)
1727 Lisp_Object arg;
1728 {
1729 return current_buffer->undo_list = arg;
1730 }
1731
1732 static Lisp_Object
1733 subst_char_in_region_unwind_1 (arg)
1734 Lisp_Object arg;
1735 {
1736 return current_buffer->filename = arg;
1737 }
1738
1739 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
1740 Ssubst_char_in_region, 4, 5, 0,
1741 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1742 If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1743 and don't mark the buffer as really changed.\n\
1744 Both characters must have the same length of multi-byte form.")
1745 (start, end, fromchar, tochar, noundo)
1746 Lisp_Object start, end, fromchar, tochar, noundo;
1747 {
1748 register int pos, stop, i, len;
1749 int changed = 0;
1750 unsigned char fromwork[4], *fromstr, towork[4], *tostr, *p;
1751 int count = specpdl_ptr - specpdl;
1752
1753 validate_region (&start, &end);
1754 CHECK_NUMBER (fromchar, 2);
1755 CHECK_NUMBER (tochar, 3);
1756
1757 if (! NILP (current_buffer->enable_multibyte_characters))
1758 {
1759 len = CHAR_STRING (XFASTINT (fromchar), fromwork, fromstr);
1760 if (CHAR_STRING (XFASTINT (tochar), towork, tostr) != len)
1761 error ("Characters in subst-char-in-region have different byte-lengths");
1762 }
1763 else
1764 {
1765 len = 1;
1766 fromwork[0] = XFASTINT (fromchar), fromstr = fromwork;
1767 towork[0] = XFASTINT (tochar), tostr = towork;
1768 }
1769
1770 pos = XINT (start);
1771 stop = XINT (end);
1772
1773 /* If we don't want undo, turn off putting stuff on the list.
1774 That's faster than getting rid of things,
1775 and it prevents even the entry for a first change.
1776 Also inhibit locking the file. */
1777 if (!NILP (noundo))
1778 {
1779 record_unwind_protect (subst_char_in_region_unwind,
1780 current_buffer->undo_list);
1781 current_buffer->undo_list = Qt;
1782 /* Don't do file-locking. */
1783 record_unwind_protect (subst_char_in_region_unwind_1,
1784 current_buffer->filename);
1785 current_buffer->filename = Qnil;
1786 }
1787
1788 if (pos < GPT)
1789 stop = min(stop, GPT);
1790 p = POS_ADDR (pos);
1791 while (1)
1792 {
1793 if (pos >= stop)
1794 {
1795 if (pos >= XINT (end)) break;
1796 stop = XINT (end);
1797 p = POS_ADDR (pos);
1798 }
1799 if (p[0] == fromstr[0]
1800 && (len == 1
1801 || (p[1] == fromstr[1]
1802 && (len == 2 || (p[2] == fromstr[2]
1803 && (len == 3 || p[3] == fromstr[3]))))))
1804 {
1805 if (! changed)
1806 {
1807 modify_region (current_buffer, XINT (start), XINT (end));
1808
1809 if (! NILP (noundo))
1810 {
1811 if (MODIFF - 1 == SAVE_MODIFF)
1812 SAVE_MODIFF++;
1813 if (MODIFF - 1 == current_buffer->auto_save_modified)
1814 current_buffer->auto_save_modified++;
1815 }
1816
1817 changed = 1;
1818 }
1819
1820 if (NILP (noundo))
1821 record_change (pos, len);
1822 for (i = 0; i < len; i++) *p++ = tostr[i];
1823 pos += len;
1824 }
1825 else
1826 pos++, p++;
1827 }
1828
1829 if (changed)
1830 signal_after_change (XINT (start),
1831 stop - XINT (start), stop - XINT (start));
1832
1833 unbind_to (count, Qnil);
1834 return Qnil;
1835 }
1836
1837 DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
1838 "From START to END, translate characters according to TABLE.\n\
1839 TABLE is a string; the Nth character in it is the mapping\n\
1840 for the character with code N. Returns the number of characters changed.")
1841 (start, end, table)
1842 Lisp_Object start;
1843 Lisp_Object end;
1844 register Lisp_Object table;
1845 {
1846 register int pos, stop; /* Limits of the region. */
1847 register unsigned char *tt; /* Trans table. */
1848 register int oc; /* Old character. */
1849 register int nc; /* New character. */
1850 int cnt; /* Number of changes made. */
1851 Lisp_Object z; /* Return. */
1852 int size; /* Size of translate table. */
1853
1854 validate_region (&start, &end);
1855 CHECK_STRING (table, 2);
1856
1857 size = XSTRING (table)->size;
1858 tt = XSTRING (table)->data;
1859
1860 pos = XINT (start);
1861 stop = XINT (end);
1862 modify_region (current_buffer, pos, stop);
1863
1864 cnt = 0;
1865 for (; pos < stop; ++pos)
1866 {
1867 oc = FETCH_BYTE (pos);
1868 if (oc < size)
1869 {
1870 nc = tt[oc];
1871 if (nc != oc)
1872 {
1873 record_change (pos, 1);
1874 *(POS_ADDR (pos)) = nc;
1875 signal_after_change (pos, 1, 1);
1876 ++cnt;
1877 }
1878 }
1879 }
1880
1881 XSETFASTINT (z, cnt);
1882 return (z);
1883 }
1884
1885 DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
1886 "Delete the text between point and mark.\n\
1887 When called from a program, expects two arguments,\n\
1888 positions (integers or markers) specifying the stretch to be deleted.")
1889 (start, end)
1890 Lisp_Object start, end;
1891 {
1892 validate_region (&start, &end);
1893 del_range (XINT (start), XINT (end));
1894 return Qnil;
1895 }
1896 \f
1897 DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
1898 "Remove restrictions (narrowing) from current buffer.\n\
1899 This allows the buffer's full text to be seen and edited.")
1900 ()
1901 {
1902 if (BEG != BEGV || Z != ZV)
1903 current_buffer->clip_changed = 1;
1904 BEGV = BEG;
1905 SET_BUF_ZV (current_buffer, Z);
1906 /* Changing the buffer bounds invalidates any recorded current column. */
1907 invalidate_current_column ();
1908 return Qnil;
1909 }
1910
1911 DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
1912 "Restrict editing in this buffer to the current region.\n\
1913 The rest of the text becomes temporarily invisible and untouchable\n\
1914 but is not deleted; if you save the buffer in a file, the invisible\n\
1915 text is included in the file. \\[widen] makes all visible again.\n\
1916 See also `save-restriction'.\n\
1917 \n\
1918 When calling from a program, pass two arguments; positions (integers\n\
1919 or markers) bounding the text that should remain visible.")
1920 (start, end)
1921 register Lisp_Object start, end;
1922 {
1923 CHECK_NUMBER_COERCE_MARKER (start, 0);
1924 CHECK_NUMBER_COERCE_MARKER (end, 1);
1925
1926 if (XINT (start) > XINT (end))
1927 {
1928 Lisp_Object tem;
1929 tem = start; start = end; end = tem;
1930 }
1931
1932 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
1933 args_out_of_range (start, end);
1934
1935 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
1936 current_buffer->clip_changed = 1;
1937
1938 BEGV = XFASTINT (start);
1939 SET_BUF_ZV (current_buffer, XFASTINT (end));
1940 if (PT < XFASTINT (start))
1941 SET_PT (XFASTINT (start));
1942 if (PT > XFASTINT (end))
1943 SET_PT (XFASTINT (end));
1944 /* Changing the buffer bounds invalidates any recorded current column. */
1945 invalidate_current_column ();
1946 return Qnil;
1947 }
1948
1949 Lisp_Object
1950 save_restriction_save ()
1951 {
1952 register Lisp_Object bottom, top;
1953 /* Note: I tried using markers here, but it does not win
1954 because insertion at the end of the saved region
1955 does not advance mh and is considered "outside" the saved region. */
1956 XSETFASTINT (bottom, BEGV - BEG);
1957 XSETFASTINT (top, Z - ZV);
1958
1959 return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
1960 }
1961
1962 Lisp_Object
1963 save_restriction_restore (data)
1964 Lisp_Object data;
1965 {
1966 register struct buffer *buf;
1967 register int newhead, newtail;
1968 register Lisp_Object tem;
1969 int obegv, ozv;
1970
1971 buf = XBUFFER (XCONS (data)->car);
1972
1973 data = XCONS (data)->cdr;
1974
1975 tem = XCONS (data)->car;
1976 newhead = XINT (tem);
1977 tem = XCONS (data)->cdr;
1978 newtail = XINT (tem);
1979 if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
1980 {
1981 newhead = 0;
1982 newtail = 0;
1983 }
1984
1985 obegv = BUF_BEGV (buf);
1986 ozv = BUF_ZV (buf);
1987
1988 BUF_BEGV (buf) = BUF_BEG (buf) + newhead;
1989 SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
1990
1991 if (obegv != BUF_BEGV (buf) || ozv != BUF_ZV (buf))
1992 current_buffer->clip_changed = 1;
1993
1994 /* If point is outside the new visible range, move it inside. */
1995 SET_BUF_PT (buf,
1996 clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)));
1997
1998 return Qnil;
1999 }
2000
2001 DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
2002 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2003 The buffer's restrictions make parts of the beginning and end invisible.\n\
2004 \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2005 This special form, `save-restriction', saves the current buffer's restrictions\n\
2006 when it is entered, and restores them when it is exited.\n\
2007 So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2008 The old restrictions settings are restored\n\
2009 even in case of abnormal exit (throw or error).\n\
2010 \n\
2011 The value returned is the value of the last form in BODY.\n\
2012 \n\
2013 `save-restriction' can get confused if, within the BODY, you widen\n\
2014 and then make changes outside the area within the saved restrictions.\n\
2015 \n\
2016 Note: if you are using both `save-excursion' and `save-restriction',\n\
2017 use `save-excursion' outermost:\n\
2018 (save-excursion (save-restriction ...))")
2019 (body)
2020 Lisp_Object body;
2021 {
2022 register Lisp_Object val;
2023 int count = specpdl_ptr - specpdl;
2024
2025 record_unwind_protect (save_restriction_restore, save_restriction_save ());
2026 val = Fprogn (body);
2027 return unbind_to (count, val);
2028 }
2029 \f
2030 /* Buffer for the most recent text displayed by Fmessage. */
2031 static char *message_text;
2032
2033 /* Allocated length of that buffer. */
2034 static int message_length;
2035
2036 DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
2037 "Print a one-line message at the bottom of the screen.\n\
2038 The first argument is a format control string, and the rest are data\n\
2039 to be formatted under control of the string. See `format' for details.\n\
2040 \n\
2041 If the first argument is nil, clear any existing message; let the\n\
2042 minibuffer contents show.")
2043 (nargs, args)
2044 int nargs;
2045 Lisp_Object *args;
2046 {
2047 if (NILP (args[0]))
2048 {
2049 message (0);
2050 return Qnil;
2051 }
2052 else
2053 {
2054 register Lisp_Object val;
2055 val = Fformat (nargs, args);
2056 /* Copy the data so that it won't move when we GC. */
2057 if (! message_text)
2058 {
2059 message_text = (char *)xmalloc (80);
2060 message_length = 80;
2061 }
2062 if (XSTRING (val)->size > message_length)
2063 {
2064 message_length = XSTRING (val)->size;
2065 message_text = (char *)xrealloc (message_text, message_length);
2066 }
2067 bcopy (XSTRING (val)->data, message_text, XSTRING (val)->size);
2068 message2 (message_text, XSTRING (val)->size);
2069 return val;
2070 }
2071 }
2072
2073 DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
2074 "Display a message, in a dialog box if possible.\n\
2075 If a dialog box is not available, use the echo area.\n\
2076 The first argument is a format control string, and the rest are data\n\
2077 to be formatted under control of the string. See `format' for details.\n\
2078 \n\
2079 If the first argument is nil, clear any existing message; let the\n\
2080 minibuffer contents show.")
2081 (nargs, args)
2082 int nargs;
2083 Lisp_Object *args;
2084 {
2085 if (NILP (args[0]))
2086 {
2087 message (0);
2088 return Qnil;
2089 }
2090 else
2091 {
2092 register Lisp_Object val;
2093 val = Fformat (nargs, args);
2094 #ifdef HAVE_MENUS
2095 {
2096 Lisp_Object pane, menu, obj;
2097 struct gcpro gcpro1;
2098 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
2099 GCPRO1 (pane);
2100 menu = Fcons (val, pane);
2101 obj = Fx_popup_dialog (Qt, menu);
2102 UNGCPRO;
2103 return val;
2104 }
2105 #else /* not HAVE_MENUS */
2106 /* Copy the data so that it won't move when we GC. */
2107 if (! message_text)
2108 {
2109 message_text = (char *)xmalloc (80);
2110 message_length = 80;
2111 }
2112 if (XSTRING (val)->size > message_length)
2113 {
2114 message_length = XSTRING (val)->size;
2115 message_text = (char *)xrealloc (message_text, message_length);
2116 }
2117 bcopy (XSTRING (val)->data, message_text, XSTRING (val)->size);
2118 message2 (message_text, XSTRING (val)->size);
2119 return val;
2120 #endif /* not HAVE_MENUS */
2121 }
2122 }
2123 #ifdef HAVE_MENUS
2124 extern Lisp_Object last_nonmenu_event;
2125 #endif
2126
2127 DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
2128 "Display a message in a dialog box or in the echo area.\n\
2129 If this command was invoked with the mouse, use a dialog box.\n\
2130 Otherwise, use the echo area.\n\
2131 The first argument is a format control string, and the rest are data\n\
2132 to be formatted under control of the string. See `format' for details.\n\
2133 \n\
2134 If the first argument is nil, clear any existing message; let the\n\
2135 minibuffer contents show.")
2136 (nargs, args)
2137 int nargs;
2138 Lisp_Object *args;
2139 {
2140 #ifdef HAVE_MENUS
2141 if (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2142 return Fmessage_box (nargs, args);
2143 #endif
2144 return Fmessage (nargs, args);
2145 }
2146
2147 DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0,
2148 "Return the string currently displayed in the echo area, or nil if none.")
2149 ()
2150 {
2151 return (echo_area_glyphs
2152 ? make_string (echo_area_glyphs, echo_area_glyphs_length)
2153 : Qnil);
2154 }
2155
2156 DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
2157 "Format a string out of a control-string and arguments.\n\
2158 The first argument is a control string.\n\
2159 The other arguments are substituted into it to make the result, a string.\n\
2160 It may contain %-sequences meaning to substitute the next argument.\n\
2161 %s means print a string argument. Actually, prints any object, with `princ'.\n\
2162 %d means print as number in decimal (%o octal, %x hex).\n\
2163 %e means print a number in exponential notation.\n\
2164 %f means print a number in decimal-point notation.\n\
2165 %g means print a number in exponential notation\n\
2166 or decimal-point notation, whichever uses fewer characters.\n\
2167 %c means print a number as a single character.\n\
2168 %S means print any object as an s-expression (using prin1).\n\
2169 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
2170 Use %% to put a single % into the output.")
2171 (nargs, args)
2172 int nargs;
2173 register Lisp_Object *args;
2174 {
2175 register int n; /* The number of the next arg to substitute */
2176 register int total = 5; /* An estimate of the final length */
2177 char *buf;
2178 register unsigned char *format, *end;
2179 int length;
2180 extern char *index ();
2181 /* It should not be necessary to GCPRO ARGS, because
2182 the caller in the interpreter should take care of that. */
2183
2184 CHECK_STRING (args[0], 0);
2185 format = XSTRING (args[0])->data;
2186 end = format + XSTRING (args[0])->size;
2187
2188 n = 0;
2189 while (format != end)
2190 if (*format++ == '%')
2191 {
2192 int minlen;
2193
2194 /* Process a numeric arg and skip it. */
2195 minlen = atoi (format);
2196 if (minlen < 0)
2197 minlen = - minlen;
2198
2199 while ((*format >= '0' && *format <= '9')
2200 || *format == '-' || *format == ' ' || *format == '.')
2201 format++;
2202
2203 if (*format == '%')
2204 format++;
2205 else if (++n >= nargs)
2206 error ("Not enough arguments for format string");
2207 else if (*format == 'S')
2208 {
2209 /* For `S', prin1 the argument and then treat like a string. */
2210 register Lisp_Object tem;
2211 tem = Fprin1_to_string (args[n], Qnil);
2212 args[n] = tem;
2213 goto string;
2214 }
2215 else if (SYMBOLP (args[n]))
2216 {
2217 XSETSTRING (args[n], XSYMBOL (args[n])->name);
2218 goto string;
2219 }
2220 else if (STRINGP (args[n]))
2221 {
2222 string:
2223 if (*format != 's' && *format != 'S')
2224 error ("format specifier doesn't match argument type");
2225 total += XSTRING (args[n])->size;
2226 /* We have to put an arbitrary limit on minlen
2227 since otherwise it could make alloca fail. */
2228 if (minlen < XSTRING (args[n])->size + 1000)
2229 total += minlen;
2230 }
2231 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
2232 else if (INTEGERP (args[n]) && *format != 's')
2233 {
2234 #ifdef LISP_FLOAT_TYPE
2235 /* The following loop assumes the Lisp type indicates
2236 the proper way to pass the argument.
2237 So make sure we have a flonum if the argument should
2238 be a double. */
2239 if (*format == 'e' || *format == 'f' || *format == 'g')
2240 args[n] = Ffloat (args[n]);
2241 #endif
2242 total += 30;
2243 /* We have to put an arbitrary limit on minlen
2244 since otherwise it could make alloca fail. */
2245 if (minlen < 1000)
2246 total += minlen;
2247 }
2248 #ifdef LISP_FLOAT_TYPE
2249 else if (FLOATP (args[n]) && *format != 's')
2250 {
2251 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
2252 args[n] = Ftruncate (args[n], Qnil);
2253 total += 30;
2254 /* We have to put an arbitrary limit on minlen
2255 since otherwise it could make alloca fail. */
2256 if (minlen < 1000)
2257 total += minlen;
2258 }
2259 #endif
2260 else
2261 {
2262 /* Anything but a string, convert to a string using princ. */
2263 register Lisp_Object tem;
2264 tem = Fprin1_to_string (args[n], Qt);
2265 args[n] = tem;
2266 goto string;
2267 }
2268 }
2269
2270 {
2271 register int nstrings = n + 1;
2272
2273 /* Allocate twice as many strings as we have %-escapes; floats occupy
2274 two slots, and we're not sure how many of those we have. */
2275 register unsigned char **strings
2276 = (unsigned char **) alloca (2 * nstrings * sizeof (unsigned char *));
2277 int i;
2278
2279 i = 0;
2280 for (n = 0; n < nstrings; n++)
2281 {
2282 if (n >= nargs)
2283 strings[i++] = (unsigned char *) "";
2284 else if (INTEGERP (args[n]))
2285 /* We checked above that the corresponding format effector
2286 isn't %s, which would cause MPV. */
2287 strings[i++] = (unsigned char *) XINT (args[n]);
2288 #ifdef LISP_FLOAT_TYPE
2289 else if (FLOATP (args[n]))
2290 {
2291 union { double d; char *half[2]; } u;
2292
2293 u.d = XFLOAT (args[n])->data;
2294 strings[i++] = (unsigned char *) u.half[0];
2295 strings[i++] = (unsigned char *) u.half[1];
2296 }
2297 #endif
2298 else if (i == 0)
2299 /* The first string is treated differently
2300 because it is the format string. */
2301 strings[i++] = XSTRING (args[n])->data;
2302 else
2303 strings[i++] = (unsigned char *) XSTRING (args[n]);
2304 }
2305
2306 /* Make room in result for all the non-%-codes in the control string. */
2307 total += XSTRING (args[0])->size;
2308
2309 /* Format it in bigger and bigger buf's until it all fits. */
2310 while (1)
2311 {
2312 buf = (char *) alloca (total + 1);
2313 buf[total - 1] = 0;
2314
2315 length = doprnt_lisp (buf, total + 1, strings[0],
2316 end, i-1, strings + 1);
2317 if (buf[total - 1] == 0)
2318 break;
2319
2320 total *= 2;
2321 }
2322 }
2323
2324 /* UNGCPRO; */
2325 return make_string (buf, length);
2326 }
2327
2328 /* VARARGS 1 */
2329 Lisp_Object
2330 #ifdef NO_ARG_ARRAY
2331 format1 (string1, arg0, arg1, arg2, arg3, arg4)
2332 EMACS_INT arg0, arg1, arg2, arg3, arg4;
2333 #else
2334 format1 (string1)
2335 #endif
2336 char *string1;
2337 {
2338 char buf[100];
2339 #ifdef NO_ARG_ARRAY
2340 EMACS_INT args[5];
2341 args[0] = arg0;
2342 args[1] = arg1;
2343 args[2] = arg2;
2344 args[3] = arg3;
2345 args[4] = arg4;
2346 doprnt (buf, sizeof buf, string1, (char *)0, 5, args);
2347 #else
2348 doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1);
2349 #endif
2350 return build_string (buf);
2351 }
2352 \f
2353 DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
2354 "Return t if two characters match, optionally ignoring case.\n\
2355 Both arguments must be characters (i.e. integers).\n\
2356 Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2357 (c1, c2)
2358 register Lisp_Object c1, c2;
2359 {
2360 CHECK_NUMBER (c1, 0);
2361 CHECK_NUMBER (c2, 1);
2362
2363 if (XINT (c1) == XINT (c2)
2364 && (NILP (current_buffer->case_fold_search)
2365 || DOWNCASE (XFASTINT (c1)) == DOWNCASE (XFASTINT (c2))))
2366 return Qt;
2367 return Qnil;
2368 }
2369 \f
2370 /* Transpose the markers in two regions of the current buffer, and
2371 adjust the ones between them if necessary (i.e.: if the regions
2372 differ in size).
2373
2374 Traverses the entire marker list of the buffer to do so, adding an
2375 appropriate amount to some, subtracting from some, and leaving the
2376 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2377
2378 It's the caller's job to see that (start1 <= end1 <= start2 <= end2). */
2379
2380 void
2381 transpose_markers (start1, end1, start2, end2)
2382 register int start1, end1, start2, end2;
2383 {
2384 register int amt1, amt2, diff, mpos;
2385 register Lisp_Object marker;
2386
2387 /* Update point as if it were a marker. */
2388 if (PT < start1)
2389 ;
2390 else if (PT < end1)
2391 TEMP_SET_PT (PT + (end2 - end1));
2392 else if (PT < start2)
2393 TEMP_SET_PT (PT + (end2 - start2) - (end1 - start1));
2394 else if (PT < end2)
2395 TEMP_SET_PT (PT - (start2 - start1));
2396
2397 /* We used to adjust the endpoints here to account for the gap, but that
2398 isn't good enough. Even if we assume the caller has tried to move the
2399 gap out of our way, it might still be at start1 exactly, for example;
2400 and that places it `inside' the interval, for our purposes. The amount
2401 of adjustment is nontrivial if there's a `denormalized' marker whose
2402 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2403 the dirty work to Fmarker_position, below. */
2404
2405 /* The difference between the region's lengths */
2406 diff = (end2 - start2) - (end1 - start1);
2407
2408 /* For shifting each marker in a region by the length of the other
2409 * region plus the distance between the regions.
2410 */
2411 amt1 = (end2 - start2) + (start2 - end1);
2412 amt2 = (end1 - start1) + (start2 - end1);
2413
2414 for (marker = BUF_MARKERS (current_buffer); !NILP (marker);
2415 marker = XMARKER (marker)->chain)
2416 {
2417 mpos = marker_position (marker);
2418 if (mpos >= start1 && mpos < end2)
2419 {
2420 if (mpos < end1)
2421 mpos += amt1;
2422 else if (mpos < start2)
2423 mpos += diff;
2424 else
2425 mpos -= amt2;
2426 if (mpos > GPT) mpos += GAP_SIZE;
2427 XMARKER (marker)->bufpos = mpos;
2428 }
2429 }
2430 }
2431
2432 DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
2433 "Transpose region START1 to END1 with START2 to END2.\n\
2434 The regions may not be overlapping, because the size of the buffer is\n\
2435 never changed in a transposition.\n\
2436 \n\
2437 Optional fifth arg LEAVE_MARKERS, if non-nil, means don't transpose\n\
2438 any markers that happen to be located in the regions.\n\
2439 \n\
2440 Transposing beyond buffer boundaries is an error.")
2441 (startr1, endr1, startr2, endr2, leave_markers)
2442 Lisp_Object startr1, endr1, startr2, endr2, leave_markers;
2443 {
2444 register int start1, end1, start2, end2,
2445 gap, len1, len_mid, len2;
2446 unsigned char *start1_addr, *start2_addr, *temp;
2447
2448 #ifdef USE_TEXT_PROPERTIES
2449 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2;
2450 cur_intv = BUF_INTERVALS (current_buffer);
2451 #endif /* USE_TEXT_PROPERTIES */
2452
2453 validate_region (&startr1, &endr1);
2454 validate_region (&startr2, &endr2);
2455
2456 start1 = XFASTINT (startr1);
2457 end1 = XFASTINT (endr1);
2458 start2 = XFASTINT (startr2);
2459 end2 = XFASTINT (endr2);
2460 gap = GPT;
2461
2462 /* Swap the regions if they're reversed. */
2463 if (start2 < end1)
2464 {
2465 register int glumph = start1;
2466 start1 = start2;
2467 start2 = glumph;
2468 glumph = end1;
2469 end1 = end2;
2470 end2 = glumph;
2471 }
2472
2473 len1 = end1 - start1;
2474 len2 = end2 - start2;
2475
2476 if (start2 < end1)
2477 error ("transposed regions not properly ordered");
2478 else if (start1 == end1 || start2 == end2)
2479 error ("transposed region may not be of length 0");
2480
2481 /* The possibilities are:
2482 1. Adjacent (contiguous) regions, or separate but equal regions
2483 (no, really equal, in this case!), or
2484 2. Separate regions of unequal size.
2485
2486 The worst case is usually No. 2. It means that (aside from
2487 potential need for getting the gap out of the way), there also
2488 needs to be a shifting of the text between the two regions. So
2489 if they are spread far apart, we are that much slower... sigh. */
2490
2491 /* It must be pointed out that the really studly thing to do would
2492 be not to move the gap at all, but to leave it in place and work
2493 around it if necessary. This would be extremely efficient,
2494 especially considering that people are likely to do
2495 transpositions near where they are working interactively, which
2496 is exactly where the gap would be found. However, such code
2497 would be much harder to write and to read. So, if you are
2498 reading this comment and are feeling squirrely, by all means have
2499 a go! I just didn't feel like doing it, so I will simply move
2500 the gap the minimum distance to get it out of the way, and then
2501 deal with an unbroken array. */
2502
2503 /* Make sure the gap won't interfere, by moving it out of the text
2504 we will operate on. */
2505 if (start1 < gap && gap < end2)
2506 {
2507 if (gap - start1 < end2 - gap)
2508 move_gap (start1);
2509 else
2510 move_gap (end2);
2511 }
2512
2513 /* Hmmm... how about checking to see if the gap is large
2514 enough to use as the temporary storage? That would avoid an
2515 allocation... interesting. Later, don't fool with it now. */
2516
2517 /* Working without memmove, for portability (sigh), so must be
2518 careful of overlapping subsections of the array... */
2519
2520 if (end1 == start2) /* adjacent regions */
2521 {
2522 modify_region (current_buffer, start1, end2);
2523 record_change (start1, len1 + len2);
2524
2525 #ifdef USE_TEXT_PROPERTIES
2526 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2527 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2528 Fset_text_properties (make_number (start1), make_number (end2),
2529 Qnil, Qnil);
2530 #endif /* USE_TEXT_PROPERTIES */
2531
2532 /* First region smaller than second. */
2533 if (len1 < len2)
2534 {
2535 /* We use alloca only if it is small,
2536 because we want to avoid stack overflow. */
2537 if (len2 > 20000)
2538 temp = (unsigned char *) xmalloc (len2);
2539 else
2540 temp = (unsigned char *) alloca (len2);
2541
2542 /* Don't precompute these addresses. We have to compute them
2543 at the last minute, because the relocating allocator might
2544 have moved the buffer around during the xmalloc. */
2545 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2546 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2547
2548 bcopy (start2_addr, temp, len2);
2549 bcopy (start1_addr, start1_addr + len2, len1);
2550 bcopy (temp, start1_addr, len2);
2551 if (len2 > 20000)
2552 free (temp);
2553 }
2554 else
2555 /* First region not smaller than second. */
2556 {
2557 if (len1 > 20000)
2558 temp = (unsigned char *) xmalloc (len1);
2559 else
2560 temp = (unsigned char *) alloca (len1);
2561 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2562 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2563 bcopy (start1_addr, temp, len1);
2564 bcopy (start2_addr, start1_addr, len2);
2565 bcopy (temp, start1_addr + len2, len1);
2566 if (len1 > 20000)
2567 free (temp);
2568 }
2569 #ifdef USE_TEXT_PROPERTIES
2570 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
2571 len1, current_buffer, 0);
2572 graft_intervals_into_buffer (tmp_interval2, start1,
2573 len2, current_buffer, 0);
2574 #endif /* USE_TEXT_PROPERTIES */
2575 }
2576 /* Non-adjacent regions, because end1 != start2, bleagh... */
2577 else
2578 {
2579 if (len1 == len2)
2580 /* Regions are same size, though, how nice. */
2581 {
2582 modify_region (current_buffer, start1, end1);
2583 modify_region (current_buffer, start2, end2);
2584 record_change (start1, len1);
2585 record_change (start2, len2);
2586 #ifdef USE_TEXT_PROPERTIES
2587 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2588 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2589 Fset_text_properties (make_number (start1), make_number (end1),
2590 Qnil, Qnil);
2591 Fset_text_properties (make_number (start2), make_number (end2),
2592 Qnil, Qnil);
2593 #endif /* USE_TEXT_PROPERTIES */
2594
2595 if (len1 > 20000)
2596 temp = (unsigned char *) xmalloc (len1);
2597 else
2598 temp = (unsigned char *) alloca (len1);
2599 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2600 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2601 bcopy (start1_addr, temp, len1);
2602 bcopy (start2_addr, start1_addr, len2);
2603 bcopy (temp, start2_addr, len1);
2604 if (len1 > 20000)
2605 free (temp);
2606 #ifdef USE_TEXT_PROPERTIES
2607 graft_intervals_into_buffer (tmp_interval1, start2,
2608 len1, current_buffer, 0);
2609 graft_intervals_into_buffer (tmp_interval2, start1,
2610 len2, current_buffer, 0);
2611 #endif /* USE_TEXT_PROPERTIES */
2612 }
2613
2614 else if (len1 < len2) /* Second region larger than first */
2615 /* Non-adjacent & unequal size, area between must also be shifted. */
2616 {
2617 len_mid = start2 - end1;
2618 modify_region (current_buffer, start1, end2);
2619 record_change (start1, (end2 - start1));
2620 #ifdef USE_TEXT_PROPERTIES
2621 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2622 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
2623 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2624 Fset_text_properties (make_number (start1), make_number (end2),
2625 Qnil, Qnil);
2626 #endif /* USE_TEXT_PROPERTIES */
2627
2628 /* holds region 2 */
2629 if (len2 > 20000)
2630 temp = (unsigned char *) xmalloc (len2);
2631 else
2632 temp = (unsigned char *) alloca (len2);
2633 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2634 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2635 bcopy (start2_addr, temp, len2);
2636 bcopy (start1_addr, start1_addr + len_mid + len2, len1);
2637 safe_bcopy (start1_addr + len1, start1_addr + len2, len_mid);
2638 bcopy (temp, start1_addr, len2);
2639 if (len2 > 20000)
2640 free (temp);
2641 #ifdef USE_TEXT_PROPERTIES
2642 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
2643 len1, current_buffer, 0);
2644 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
2645 len_mid, current_buffer, 0);
2646 graft_intervals_into_buffer (tmp_interval2, start1,
2647 len2, current_buffer, 0);
2648 #endif /* USE_TEXT_PROPERTIES */
2649 }
2650 else
2651 /* Second region smaller than first. */
2652 {
2653 len_mid = start2 - end1;
2654 record_change (start1, (end2 - start1));
2655 modify_region (current_buffer, start1, end2);
2656
2657 #ifdef USE_TEXT_PROPERTIES
2658 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
2659 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
2660 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
2661 Fset_text_properties (make_number (start1), make_number (end2),
2662 Qnil, Qnil);
2663 #endif /* USE_TEXT_PROPERTIES */
2664
2665 /* holds region 1 */
2666 if (len1 > 20000)
2667 temp = (unsigned char *) xmalloc (len1);
2668 else
2669 temp = (unsigned char *) alloca (len1);
2670 start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
2671 start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
2672 bcopy (start1_addr, temp, len1);
2673 bcopy (start2_addr, start1_addr, len2);
2674 bcopy (start1_addr + len1, start1_addr + len2, len_mid);
2675 bcopy (temp, start1_addr + len2 + len_mid, len1);
2676 if (len1 > 20000)
2677 free (temp);
2678 #ifdef USE_TEXT_PROPERTIES
2679 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
2680 len1, current_buffer, 0);
2681 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
2682 len_mid, current_buffer, 0);
2683 graft_intervals_into_buffer (tmp_interval2, start1,
2684 len2, current_buffer, 0);
2685 #endif /* USE_TEXT_PROPERTIES */
2686 }
2687 }
2688
2689 /* todo: this will be slow, because for every transposition, we
2690 traverse the whole friggin marker list. Possible solutions:
2691 somehow get a list of *all* the markers across multiple
2692 transpositions and do it all in one swell phoop. Or maybe modify
2693 Emacs' marker code to keep an ordered list or tree. This might
2694 be nicer, and more beneficial in the long run, but would be a
2695 bunch of work. Plus the way they're arranged now is nice. */
2696 if (NILP (leave_markers))
2697 {
2698 transpose_markers (start1, end1, start2, end2);
2699 fix_overlays_in_range (start1, end2);
2700 }
2701
2702 return Qnil;
2703 }
2704
2705 \f
2706 void
2707 syms_of_editfns ()
2708 {
2709 environbuf = 0;
2710
2711 Qbuffer_access_fontify_functions
2712 = intern ("buffer-access-fontify-functions");
2713 staticpro (&Qbuffer_access_fontify_functions);
2714
2715 DEFVAR_LISP ("buffer-access-fontify-functions",
2716 &Vbuffer_access_fontify_functions,
2717 "List of functions called by `buffer-substring' to fontify if necessary.\n\
2718 Each function is called with two arguments which specify the range\n\
2719 of the buffer being accessed.");
2720 Vbuffer_access_fontify_functions = Qnil;
2721
2722 {
2723 Lisp_Object obuf;
2724 extern Lisp_Object Vprin1_to_string_buffer;
2725 obuf = Fcurrent_buffer ();
2726 /* Do this here, because init_buffer_once is too early--it won't work. */
2727 Fset_buffer (Vprin1_to_string_buffer);
2728 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
2729 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
2730 Qnil);
2731 Fset_buffer (obuf);
2732 }
2733
2734 DEFVAR_LISP ("buffer-access-fontified-property",
2735 &Vbuffer_access_fontified_property,
2736 "Property which (if non-nil) indicates text has been fontified.\n\
2737 `buffer-substring' need not call the `buffer-access-fontify-functions'\n\
2738 functions if all the text being accessed has this property.");
2739 Vbuffer_access_fontified_property = Qnil;
2740
2741 DEFVAR_LISP ("system-name", &Vsystem_name,
2742 "The name of the machine Emacs is running on.");
2743
2744 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
2745 "The full name of the user logged in.");
2746
2747 DEFVAR_LISP ("user-login-name", &Vuser_login_name,
2748 "The user's name, taken from environment variables if possible.");
2749
2750 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name,
2751 "The user's name, based upon the real uid only.");
2752
2753 defsubr (&Schar_equal);
2754 defsubr (&Sgoto_char);
2755 defsubr (&Sstring_to_char);
2756 defsubr (&Schar_to_string);
2757 defsubr (&Ssref);
2758 defsubr (&Sbuffer_substring);
2759 defsubr (&Sbuffer_substring_no_properties);
2760 defsubr (&Sbuffer_string);
2761
2762 defsubr (&Spoint_marker);
2763 defsubr (&Smark_marker);
2764 defsubr (&Spoint);
2765 defsubr (&Sregion_beginning);
2766 defsubr (&Sregion_end);
2767 /* defsubr (&Smark); */
2768 /* defsubr (&Sset_mark); */
2769 defsubr (&Ssave_excursion);
2770 defsubr (&Ssave_current_buffer);
2771
2772 defsubr (&Sbufsize);
2773 defsubr (&Spoint_max);
2774 defsubr (&Spoint_min);
2775 defsubr (&Spoint_min_marker);
2776 defsubr (&Spoint_max_marker);
2777
2778 defsubr (&Sline_beginning_position);
2779 defsubr (&Sline_end_position);
2780
2781 defsubr (&Sbobp);
2782 defsubr (&Seobp);
2783 defsubr (&Sbolp);
2784 defsubr (&Seolp);
2785 defsubr (&Sfollowing_char);
2786 defsubr (&Sprevious_char);
2787 defsubr (&Schar_after);
2788 defsubr (&Schar_before);
2789 defsubr (&Sinsert);
2790 defsubr (&Sinsert_before_markers);
2791 defsubr (&Sinsert_and_inherit);
2792 defsubr (&Sinsert_and_inherit_before_markers);
2793 defsubr (&Sinsert_char);
2794
2795 defsubr (&Suser_login_name);
2796 defsubr (&Suser_real_login_name);
2797 defsubr (&Suser_uid);
2798 defsubr (&Suser_real_uid);
2799 defsubr (&Suser_full_name);
2800 defsubr (&Semacs_pid);
2801 defsubr (&Scurrent_time);
2802 defsubr (&Sformat_time_string);
2803 defsubr (&Sdecode_time);
2804 defsubr (&Sencode_time);
2805 defsubr (&Scurrent_time_string);
2806 defsubr (&Scurrent_time_zone);
2807 defsubr (&Sset_time_zone_rule);
2808 defsubr (&Ssystem_name);
2809 defsubr (&Smessage);
2810 defsubr (&Smessage_box);
2811 defsubr (&Smessage_or_box);
2812 defsubr (&Scurrent_message);
2813 defsubr (&Sformat);
2814
2815 defsubr (&Sinsert_buffer_substring);
2816 defsubr (&Scompare_buffer_substrings);
2817 defsubr (&Ssubst_char_in_region);
2818 defsubr (&Stranslate_region);
2819 defsubr (&Sdelete_region);
2820 defsubr (&Swiden);
2821 defsubr (&Snarrow_to_region);
2822 defsubr (&Ssave_restriction);
2823 defsubr (&Stranspose_regions);
2824 }