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