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