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