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