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