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