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