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