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