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