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