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