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