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