(syms_of_callint): Fix DEFVAR_LISP for Vmark_even_if_inactive to use right
[bpt/emacs.git] / src / editfns.c
CommitLineData
35692fe0 1/* Lisp functions pertaining to editing.
9772455e 2 Copyright (C) 1985, 1986, 1987, 1989, 1993 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
8the Free Software Foundation; either version 1, or (at your option)
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
35692fe0 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
41/* Some static data, and a function to initialize it for each run */
42
43Lisp_Object Vsystem_name;
44Lisp_Object Vuser_real_name; /* login name of current user ID */
45Lisp_Object Vuser_full_name; /* full name of current user */
46Lisp_Object Vuser_name; /* user name from USER or LOGNAME. */
47
48void
49init_editfns ()
50{
52b14ac0 51 char *user_name;
35692fe0
JB
52 register unsigned char *p, *q, *r;
53 struct passwd *pw; /* password entry for the current user */
54 extern char *index ();
55 Lisp_Object tem;
56
57 /* Set up system_name even when dumping. */
58
59 Vsystem_name = build_string (get_system_name ());
60 p = XSTRING (Vsystem_name)->data;
61 while (*p)
62 {
63 if (*p == ' ' || *p == '\t')
64 *p = '-';
65 p++;
66 }
67
68#ifndef CANNOT_DUMP
69 /* Don't bother with this on initial start when just dumping out */
70 if (!initialized)
71 return;
72#endif /* not CANNOT_DUMP */
73
74 pw = (struct passwd *) getpwuid (getuid ());
75 Vuser_real_name = build_string (pw ? pw->pw_name : "unknown");
76
52b14ac0
JB
77 /* Get the effective user name, by consulting environment variables,
78 or the effective uid if those are unset. */
79 user_name = (char *) getenv ("USER");
35692fe0 80 if (!user_name)
52b14ac0
JB
81 user_name = (char *) getenv ("LOGNAME");
82 if (!user_name)
83 {
84 pw = (struct passwd *) getpwuid (geteuid ());
85 user_name = (char *) (pw ? pw->pw_name : "unknown");
86 }
87 Vuser_name = build_string (user_name);
35692fe0 88
52b14ac0
JB
89 /* If the user name claimed in the environment vars differs from
90 the real uid, use the claimed name to find the full name. */
35692fe0 91 tem = Fstring_equal (Vuser_name, Vuser_real_name);
56a98455 92 if (NILP (tem))
52b14ac0 93 pw = (struct passwd *) getpwnam (XSTRING (Vuser_name)->data);
35692fe0
JB
94
95 p = (unsigned char *) (pw ? USER_FULL_NAME : "unknown");
96 q = (unsigned char *) index (p, ',');
97 Vuser_full_name = make_string (p, q ? q - p : strlen (p));
98
99#ifdef AMPERSAND_FULL_NAME
100 p = XSTRING (Vuser_full_name)->data;
101 q = (char *) index (p, '&');
102 /* Substitute the login name for the &, upcasing the first character. */
103 if (q)
104 {
105 r = (char *) alloca (strlen (p) + XSTRING (Vuser_name)->size + 1);
106 bcopy (p, r, q - p);
107 r[q - p] = 0;
52b14ac0 108 strcat (r, XSTRING (Vuser_name)->data);
35692fe0
JB
109 r[q - p] = UPCASE (r[q - p]);
110 strcat (r, q + 1);
111 Vuser_full_name = build_string (r);
112 }
113#endif /* AMPERSAND_FULL_NAME */
114}
115\f
116DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
117 "Convert arg CHAR to a one-character string containing that character.")
118 (n)
119 Lisp_Object n;
120{
121 char c;
122 CHECK_NUMBER (n, 0);
123
124 c = XINT (n);
125 return make_string (&c, 1);
126}
127
128DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
129 "Convert arg STRING to a character, the first character of that string.")
130 (str)
131 register Lisp_Object str;
132{
133 register Lisp_Object val;
134 register struct Lisp_String *p;
135 CHECK_STRING (str, 0);
136
137 p = XSTRING (str);
138 if (p->size)
139 XFASTINT (val) = ((unsigned char *) p->data)[0];
140 else
141 XFASTINT (val) = 0;
142 return val;
143}
144\f
145static Lisp_Object
146buildmark (val)
147 int val;
148{
149 register Lisp_Object mark;
150 mark = Fmake_marker ();
151 Fset_marker (mark, make_number (val), Qnil);
152 return mark;
153}
154
155DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
156 "Return value of point, as an integer.\n\
157Beginning of buffer is position (point-min)")
158 ()
159{
160 Lisp_Object temp;
161 XFASTINT (temp) = point;
162 return temp;
163}
164
165DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
166 "Return value of point, as a marker object.")
167 ()
168{
169 return buildmark (point);
170}
171
172int
173clip_to_bounds (lower, num, upper)
174 int lower, num, upper;
175{
176 if (num < lower)
177 return lower;
178 else if (num > upper)
179 return upper;
180 else
181 return num;
182}
183
184DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
185 "Set point to POSITION, a number or marker.\n\
186Beginning of buffer is position (point-min), end is (point-max).")
187 (n)
188 register Lisp_Object n;
189{
190 CHECK_NUMBER_COERCE_MARKER (n, 0);
191
192 SET_PT (clip_to_bounds (BEGV, XINT (n), ZV));
193 return n;
194}
195
196static Lisp_Object
197region_limit (beginningp)
198 int beginningp;
199{
200 register Lisp_Object m;
c9dd14e1
RM
201 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
202 && NILP (current_buffer->mark_active))
203 Fsignal (Qmark_inactive, Qnil);
35692fe0 204 m = Fmarker_position (current_buffer->mark);
56a98455 205 if (NILP (m)) error ("There is no region now");
35692fe0
JB
206 if ((point < XFASTINT (m)) == beginningp)
207 return (make_number (point));
208 else
209 return (m);
210}
211
212DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
213 "Return position of beginning of region, as an integer.")
214 ()
215{
216 return (region_limit (1));
217}
218
219DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
220 "Return position of end of region, as an integer.")
221 ()
222{
223 return (region_limit (0));
224}
225
226#if 0 /* now in lisp code */
227DEFUN ("mark", Fmark, Smark, 0, 0, 0,
228 "Return this buffer's mark value as integer, or nil if no mark.\n\
229If you are using this in an editing command, you are most likely making\n\
230a mistake; see the documentation of `set-mark'.")
231 ()
232{
233 return Fmarker_position (current_buffer->mark);
234}
235#endif /* commented out code */
236
237DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
238 "Return this buffer's mark, as a marker object.\n\
239Watch out! Moving this marker changes the mark position.\n\
240If you set the marker not to point anywhere, the buffer will have no mark.")
241 ()
242{
243 return current_buffer->mark;
244}
245
246#if 0 /* this is now in lisp code */
247DEFUN ("set-mark", Fset_mark, Sset_mark, 1, 1, 0,
248 "Set this buffer's mark to POS. Don't use this function!\n\
249That is to say, don't use this function unless you want\n\
250the user to see that the mark has moved, and you want the previous\n\
251mark position to be lost.\n\
252\n\
253Normally, when a new mark is set, the old one should go on the stack.\n\
254This is why most applications should use push-mark, not set-mark.\n\
255\n\
256Novice programmers often try to use the mark for the wrong purposes.\n\
257The mark saves a location for the user's convenience.\n\
258Most editing commands should not alter the mark.\n\
259To remember a location for internal use in the Lisp program,\n\
260store it in a Lisp variable. Example:\n\
261\n\
262 (let ((beg (point))) (forward-line 1) (delete-region beg (point))).")
263 (pos)
264 Lisp_Object pos;
265{
56a98455 266 if (NILP (pos))
35692fe0
JB
267 {
268 current_buffer->mark = Qnil;
269 return Qnil;
270 }
271 CHECK_NUMBER_COERCE_MARKER (pos, 0);
272
56a98455 273 if (NILP (current_buffer->mark))
35692fe0
JB
274 current_buffer->mark = Fmake_marker ();
275
276 Fset_marker (current_buffer->mark, pos, Qnil);
277 return pos;
278}
279#endif /* commented-out code */
280
281Lisp_Object
282save_excursion_save ()
283{
0e2c9c70
JB
284 register int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
285 == current_buffer);
35692fe0
JB
286
287 return Fcons (Fpoint_marker (),
0e2c9c70 288 Fcons (Fcopy_marker (current_buffer->mark),
9772455e
RS
289 Fcons (visible ? Qt : Qnil,
290 current_buffer->mark_active)));
35692fe0
JB
291}
292
293Lisp_Object
294save_excursion_restore (info)
295 register Lisp_Object info;
296{
9772455e 297 register Lisp_Object tem, tem1;
35692fe0
JB
298
299 tem = Fmarker_buffer (Fcar (info));
300 /* If buffer being returned to is now deleted, avoid error */
301 /* Otherwise could get error here while unwinding to top level
302 and crash */
303 /* In that case, Fmarker_buffer returns nil now. */
56a98455 304 if (NILP (tem))
35692fe0
JB
305 return Qnil;
306 Fset_buffer (tem);
307 tem = Fcar (info);
308 Fgoto_char (tem);
309 unchain_marker (tem);
310 tem = Fcar (Fcdr (info));
311 Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
312 unchain_marker (tem);
313 tem = Fcdr (Fcdr (info));
9772455e
RS
314 tem1 = Fcar (tem);
315 if (!NILP (tem1)
0e2c9c70 316 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
35692fe0 317 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
9772455e
RS
318
319 tem1 = current_buffer->mark_active;
320 current_buffer->mark_active = Fcdr (tem);
321 if (! NILP (current_buffer->mark_active))
322 call1 (Vrun_hooks, intern ("activate-mark-hook"));
323 else if (! NILP (tem1))
324 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
35692fe0
JB
325 return Qnil;
326}
327
328DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
329 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
330Executes BODY just like `progn'.\n\
331The values of point, mark and the current buffer are restored\n\
9772455e
RS
332even in case of abnormal exit (throw or error).\n\
333The state of activation of the mark is also restored.")
35692fe0
JB
334 (args)
335 Lisp_Object args;
336{
337 register Lisp_Object val;
338 int count = specpdl_ptr - specpdl;
339
340 record_unwind_protect (save_excursion_restore, save_excursion_save ());
341
342 val = Fprogn (args);
343 return unbind_to (count, val);
344}
345\f
346DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 0, 0,
347 "Return the number of characters in the current buffer.")
348 ()
349{
350 Lisp_Object temp;
351 XFASTINT (temp) = Z - BEG;
352 return temp;
353}
354
355DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
356 "Return the minimum permissible value of point in the current buffer.\n\
357This is 1, unless a clipping restriction is in effect.")
358 ()
359{
360 Lisp_Object temp;
361 XFASTINT (temp) = BEGV;
362 return temp;
363}
364
365DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
366 "Return a marker to the minimum permissible value of point in this buffer.\n\
367This is the beginning, unless a clipping restriction is in effect.")
368 ()
369{
370 return buildmark (BEGV);
371}
372
373DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
374 "Return the maximum permissible value of point in the current buffer.\n\
375This is (1+ (buffer-size)), unless a clipping restriction is in effect,\n\
376in which case it is less.")
377 ()
378{
379 Lisp_Object temp;
380 XFASTINT (temp) = ZV;
381 return temp;
382}
383
384DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
385 "Return a marker to the maximum permissible value of point in this buffer.\n\
386This is (1+ (buffer-size)), unless a clipping restriction is in effect,\n\
387in which case it is less.")
388 ()
389{
390 return buildmark (ZV);
391}
392
850a8179
JB
393DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
394 "Return the character following point, as a number.\n\
395At the end of the buffer or accessible region, return 0.")
35692fe0
JB
396 ()
397{
398 Lisp_Object temp;
850a8179
JB
399 if (point >= ZV)
400 XFASTINT (temp) = 0;
401 else
402 XFASTINT (temp) = FETCH_CHAR (point);
35692fe0
JB
403 return temp;
404}
405
850a8179
JB
406DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
407 "Return the character preceding point, as a number.\n\
408At the beginning of the buffer or accessible region, return 0.")
35692fe0
JB
409 ()
410{
411 Lisp_Object temp;
412 if (point <= BEGV)
413 XFASTINT (temp) = 0;
414 else
415 XFASTINT (temp) = FETCH_CHAR (point - 1);
416 return temp;
417}
418
419DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
420 "Return T if point is at the beginning of the buffer.\n\
421If the buffer is narrowed, this means the beginning of the narrowed part.")
422 ()
423{
424 if (point == BEGV)
425 return Qt;
426 return Qnil;
427}
428
429DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
430 "Return T if point is at the end of the buffer.\n\
431If the buffer is narrowed, this means the end of the narrowed part.")
432 ()
433{
434 if (point == ZV)
435 return Qt;
436 return Qnil;
437}
438
439DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
440 "Return T if point is at the beginning of a line.")
441 ()
442{
443 if (point == BEGV || FETCH_CHAR (point - 1) == '\n')
444 return Qt;
445 return Qnil;
446}
447
448DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
449 "Return T if point is at the end of a line.\n\
450`End of a line' includes point being at the end of the buffer.")
451 ()
452{
453 if (point == ZV || FETCH_CHAR (point) == '\n')
454 return Qt;
455 return Qnil;
456}
457
458DEFUN ("char-after", Fchar_after, Schar_after, 1, 1, 0,
459 "Return character in current buffer at position POS.\n\
460POS is an integer or a buffer pointer.\n\
461If POS is out of range, the value is nil.")
462 (pos)
463 Lisp_Object pos;
464{
465 register Lisp_Object val;
466 register int n;
467
468 CHECK_NUMBER_COERCE_MARKER (pos, 0);
469
470 n = XINT (pos);
471 if (n < BEGV || n >= ZV) return Qnil;
472
473 XFASTINT (val) = FETCH_CHAR (n);
474 return val;
475}
476\f
477DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 0, 0,
478 "Return the name under which the user logged in, as a string.\n\
479This is based on the effective uid, not the real uid.\n\
480Also, if the environment variable USER or LOGNAME is set,\n\
481that determines the value of this function.")
482 ()
483{
484 return Vuser_name;
485}
486
487DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
488 0, 0, 0,
489 "Return the name of the user's real uid, as a string.\n\
490Differs from `user-login-name' when running under `su'.")
491 ()
492{
493 return Vuser_real_name;
494}
495
496DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
497 "Return the effective uid of Emacs, as an integer.")
498 ()
499{
500 return make_number (geteuid ());
501}
502
503DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
504 "Return the real uid of Emacs, as an integer.")
505 ()
506{
507 return make_number (getuid ());
508}
509
510DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 0, 0,
511 "Return the full name of the user logged in, as a string.")
512 ()
513{
514 return Vuser_full_name;
515}
516
517DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
518 "Return the name of the machine you are running on, as a string.")
519 ()
520{
521 return Vsystem_name;
522}
523
d940e0e4 524DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
956ace37
JB
525 "Return the current time, as the number of seconds since 12:00 AM January 1970.\n\
526The time is returned as a list of three integers. The first has the\n\
527most significant 16 bits of the seconds, while the second has the\n\
528least significant 16 bits. The third integer gives the microsecond\n\
529count.\n\
530\n\
531The microsecond count is zero on systems that do not provide\n\
532resolution finer than a second.")
d940e0e4
JB
533 ()
534{
956ace37
JB
535 EMACS_TIME t;
536 Lisp_Object result[3];
537
538 EMACS_GET_TIME (t);
539 XSET (result[0], Lisp_Int, (EMACS_SECS (t) >> 16) & 0xffff);
540 XSET (result[1], Lisp_Int, (EMACS_SECS (t) >> 0) & 0xffff);
541 XSET (result[2], Lisp_Int, EMACS_USECS (t));
542
543 return Flist (3, result);
d940e0e4
JB
544}
545\f
546
e3120ab5
JB
547static int
548lisp_time_argument (specified_time, result)
549 Lisp_Object specified_time;
550 time_t *result;
551{
552 if (NILP (specified_time))
553 return time (result) != -1;
554 else
555 {
556 Lisp_Object high, low;
557 high = Fcar (specified_time);
558 CHECK_NUMBER (high, 0);
559 low = Fcdr (specified_time);
560 if (XTYPE (low) == Lisp_Cons)
561 low = Fcar (low);
562 CHECK_NUMBER (low, 0);
563 *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
564 return *result >> 16 == XINT (high);
565 }
566}
567
2148f2b4 568DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
35692fe0 569 "Return the current time, as a human-readable string.\n\
2148f2b4
RS
570Programs can use this function to decode a time,\n\
571since the number of columns in each field is fixed.\n\
572The format is `Sun Sep 16 01:03:52 1973'.\n\
573If an argument is given, it specifies a time to format\n\
574instead of the current time. The argument should have the form:\n\
575 (HIGH . LOW)\n\
576or the form:\n\
577 (HIGH LOW . IGNORED).\n\
578Thus, you can use times obtained from `current-time'\n\
579and from `file-attributes'.")
580 (specified_time)
581 Lisp_Object specified_time;
582{
e3120ab5 583 time_t value;
35692fe0 584 char buf[30];
2148f2b4
RS
585 register char *tem;
586
e3120ab5
JB
587 if (! lisp_time_argument (specified_time, &value))
588 value = -1;
2148f2b4 589 tem = (char *) ctime (&value);
35692fe0
JB
590
591 strncpy (buf, tem, 24);
592 buf[24] = 0;
593
594 return build_string (buf);
595}
c2662aea 596
e3120ab5
JB
597#define TM_YEAR_ORIGIN 1900
598
599/* Yield A - B, measured in seconds. */
600static long
601difftm(a, b)
602 struct tm *a, *b;
603{
604 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
605 int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
606 return
607 (
608 (
609 (
610 /* difference in day of year */
611 a->tm_yday - b->tm_yday
612 /* + intervening leap days */
613 + ((ay >> 2) - (by >> 2))
614 - (ay/100 - by/100)
615 + ((ay/100 >> 2) - (by/100 >> 2))
616 /* + difference in years * 365 */
617 + (long)(ay-by) * 365
618 )*24 + (a->tm_hour - b->tm_hour)
619 )*60 + (a->tm_min - b->tm_min)
620 )*60 + (a->tm_sec - b->tm_sec);
621}
622
623DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
624 "Return the offset and name for the local time zone.\n\
625This returns a list of the form (OFFSET NAME).\n\
626OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
627 A negative value means west of Greenwich.\n\
628NAME is a string giving the name of the time zone.\n\
629If an argument is given, it specifies when the time zone offset is determined\n\
630instead of using the current time. The argument should have the form:\n\
631 (HIGH . LOW)\n\
632or the form:\n\
633 (HIGH LOW . IGNORED).\n\
634Thus, you can use times obtained from `current-time'\n\
635and from `file-attributes'.\n\
773c1fd3
JB
636\n\
637Some operating systems cannot provide all this information to Emacs;\n\
2d88f747 638in this case, `current-time-zone' returns a list containing nil for\n\
773c1fd3 639the data it can't find.")
e3120ab5
JB
640 (specified_time)
641 Lisp_Object specified_time;
c2662aea 642{
e3120ab5
JB
643 time_t value;
644 struct tm *t;
c2662aea 645
e3120ab5 646 if (lisp_time_argument (specified_time, &value)
2d88f747 647 && (t = gmtime (&value)) != 0)
e3120ab5 648 {
2d88f747 649 struct tm gmt;
e3120ab5
JB
650 long offset;
651 char *s, buf[6];
2d88f747
RS
652
653 gmt = *t; /* Make a copy, in case localtime modifies *t. */
654 t = localtime (&value);
655 offset = difftm (t, &gmt);
e3120ab5
JB
656 s = 0;
657#ifdef HAVE_TM_ZONE
658 if (t->tm_zone)
659 s = t->tm_zone;
a7971c39
RS
660#else /* not HAVE_TM_ZONE */
661#ifdef HAVE_TZNAME
662 if (t->tm_isdst == 0 || t->tm_isdst == 1)
663 s = tzname[t->tm_isdst];
c2662aea 664#endif
a7971c39 665#endif /* not HAVE_TM_ZONE */
e3120ab5
JB
666 if (!s)
667 {
668 /* No local time zone name is available; use "+-NNNN" instead. */
00fc94d0 669 int am = (offset < 0 ? -offset : offset) / 60;
e3120ab5
JB
670 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
671 s = buf;
672 }
673 return Fcons (make_number (offset), Fcons (build_string (s), Qnil));
674 }
675 else
676 return Fmake_list (2, Qnil);
c2662aea
JB
677}
678
35692fe0
JB
679\f
680void
681insert1 (arg)
682 Lisp_Object arg;
683{
684 Finsert (1, &arg);
685}
686
52b14ac0
JB
687
688/* Callers passing one argument to Finsert need not gcpro the
689 argument "array", since the only element of the array will
690 not be used after calling insert or insert_from_string, so
691 we don't care if it gets trashed. */
692
35692fe0
JB
693DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
694 "Insert the arguments, either strings or characters, at point.\n\
695Point moves forward so that it ends up after the inserted text.\n\
696Any other markers at the point of insertion remain before the text.")
697 (nargs, args)
698 int nargs;
699 register Lisp_Object *args;
700{
701 register int argnum;
702 register Lisp_Object tem;
703 char str[1];
35692fe0
JB
704
705 for (argnum = 0; argnum < nargs; argnum++)
706 {
707 tem = args[argnum];
708 retry:
709 if (XTYPE (tem) == Lisp_Int)
710 {
711 str[0] = XINT (tem);
712 insert (str, 1);
713 }
714 else if (XTYPE (tem) == Lisp_String)
715 {
716 insert_from_string (tem, 0, XSTRING (tem)->size);
717 }
718 else
719 {
720 tem = wrong_type_argument (Qchar_or_string_p, tem);
721 goto retry;
722 }
723 }
724
35692fe0
JB
725 return Qnil;
726}
727
728DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
729 "Insert strings or characters at point, relocating markers after the text.\n\
730Point moves forward so that it ends up after the inserted text.\n\
731Any other markers at the point of insertion also end up after the text.")
732 (nargs, args)
733 int nargs;
734 register Lisp_Object *args;
735{
736 register int argnum;
737 register Lisp_Object tem;
738 char str[1];
35692fe0
JB
739
740 for (argnum = 0; argnum < nargs; argnum++)
741 {
742 tem = args[argnum];
743 retry:
744 if (XTYPE (tem) == Lisp_Int)
745 {
746 str[0] = XINT (tem);
747 insert_before_markers (str, 1);
748 }
749 else if (XTYPE (tem) == Lisp_String)
750 {
751 insert_from_string_before_markers (tem, 0, XSTRING (tem)->size);
752 }
753 else
754 {
755 tem = wrong_type_argument (Qchar_or_string_p, tem);
756 goto retry;
757 }
758 }
759
35692fe0
JB
760 return Qnil;
761}
762\f
763DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 2, 0,
764 "Insert COUNT (second arg) copies of CHAR (first arg).\n\
765Point and all markers are affected as in the function `insert'.\n\
766Both arguments are required.")
767 (chr, count)
768 Lisp_Object chr, count;
769{
770 register unsigned char *string;
771 register int strlen;
772 register int i, n;
773
774 CHECK_NUMBER (chr, 0);
775 CHECK_NUMBER (count, 1);
776
777 n = XINT (count);
778 if (n <= 0)
779 return Qnil;
780 strlen = min (n, 256);
781 string = (unsigned char *) alloca (strlen);
782 for (i = 0; i < strlen; i++)
783 string[i] = XFASTINT (chr);
784 while (n >= strlen)
785 {
786 insert (string, strlen);
787 n -= strlen;
788 }
789 if (n > 0)
790 insert (string, n);
791 return Qnil;
792}
793
794\f
ffd56f97
JB
795/* Making strings from buffer contents. */
796
797/* Return a Lisp_String containing the text of the current buffer from
74d6d8c5 798 START to END. If text properties are in use and the current buffer
eb8c3be9 799 has properties in the range specified, the resulting string will also
74d6d8c5 800 have them.
ffd56f97
JB
801
802 We don't want to use plain old make_string here, because it calls
803 make_uninit_string, which can cause the buffer arena to be
804 compacted. make_string has no way of knowing that the data has
805 been moved, and thus copies the wrong data into the string. This
806 doesn't effect most of the other users of make_string, so it should
807 be left as is. But we should use this function when conjuring
808 buffer substrings. */
74d6d8c5 809
ffd56f97
JB
810Lisp_Object
811make_buffer_string (start, end)
812 int start, end;
813{
814 Lisp_Object result;
815
816 if (start < GPT && GPT < end)
817 move_gap (start);
818
819 result = make_uninit_string (end - start);
820 bcopy (&FETCH_CHAR (start), XSTRING (result)->data, end - start);
821
74d6d8c5
JA
822 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
823 copy_intervals_to_string (result, current_buffer, start, end - start);
824
ffd56f97
JB
825 return result;
826}
35692fe0
JB
827
828DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
829 "Return the contents of part of the current buffer as a string.\n\
830The two arguments START and END are character positions;\n\
831they can be in either order.")
832 (b, e)
833 Lisp_Object b, e;
834{
835 register int beg, end;
35692fe0
JB
836
837 validate_region (&b, &e);
838 beg = XINT (b);
839 end = XINT (e);
840
ffd56f97 841 return make_buffer_string (beg, end);
35692fe0
JB
842}
843
844DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
845 "Return the contents of the current buffer as a string.")
846 ()
847{
ffd56f97 848 return make_buffer_string (BEGV, ZV);
35692fe0
JB
849}
850
851DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
852 1, 3, 0,
83ea6fc2 853 "Insert before point a substring of the contents of buffer BUFFER.\n\
35692fe0
JB
854BUFFER may be a buffer or a buffer name.\n\
855Arguments START and END are character numbers specifying the substring.\n\
856They default to the beginning and the end of BUFFER.")
857 (buf, b, e)
858 Lisp_Object buf, b, e;
859{
74d6d8c5 860 register int beg, end, temp, len, opoint, start;
35692fe0 861 register struct buffer *bp;
3fff2dfa 862 Lisp_Object buffer;
35692fe0 863
3fff2dfa
RS
864 buffer = Fget_buffer (buf);
865 if (NILP (buffer))
866 nsberror (buf);
867 bp = XBUFFER (buffer);
35692fe0 868
56a98455 869 if (NILP (b))
35692fe0
JB
870 beg = BUF_BEGV (bp);
871 else
872 {
873 CHECK_NUMBER_COERCE_MARKER (b, 0);
874 beg = XINT (b);
875 }
56a98455 876 if (NILP (e))
35692fe0
JB
877 end = BUF_ZV (bp);
878 else
879 {
880 CHECK_NUMBER_COERCE_MARKER (e, 1);
881 end = XINT (e);
882 }
883
884 if (beg > end)
74d6d8c5 885 temp = beg, beg = end, end = temp;
35692fe0
JB
886
887 /* Move the gap or create enough gap in the current buffer. */
888
889 if (point != GPT)
890 move_gap (point);
891 if (GAP_SIZE < end - beg)
892 make_gap (end - beg - GAP_SIZE);
893
74d6d8c5
JA
894 len = end - beg;
895 start = beg;
896 opoint = point;
897
35692fe0
JB
898 if (!(BUF_BEGV (bp) <= beg
899 && beg <= end
900 && end <= BUF_ZV (bp)))
901 args_out_of_range (b, e);
902
903 /* Now the actual insertion will not do any gap motion,
904 so it matters not if BUF is the current buffer. */
905 if (beg < BUF_GPT (bp))
906 {
907 insert (BUF_CHAR_ADDRESS (bp, beg), min (end, BUF_GPT (bp)) - beg);
908 beg = min (end, BUF_GPT (bp));
909 }
910 if (beg < end)
911 insert (BUF_CHAR_ADDRESS (bp, beg), end - beg);
912
74d6d8c5
JA
913 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
914 graft_intervals_into_buffer (copy_intervals (bp->intervals, start, len),
915 opoint, bp);
916
35692fe0
JB
917 return Qnil;
918}
e9cf2084
RS
919
920DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
921 6, 6, 0,
922 "Compare two substrings of two buffers; return result as number.\n\
923the value is -N if first string is less after N-1 chars,\n\
924+N if first string is greater after N-1 chars, or 0 if strings match.\n\
925Each substring is represented as three arguments: BUFFER, START and END.\n\
926That makes six args in all, three for each substring.\n\n\
927The value of `case-fold-search' in the current buffer\n\
928determines whether case is significant or ignored.")
929 (buffer1, start1, end1, buffer2, start2, end2)
930 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
931{
932 register int begp1, endp1, begp2, endp2, temp, len1, len2, length, i;
933 register struct buffer *bp1, *bp2;
934 register unsigned char *trt
935 = (!NILP (current_buffer->case_fold_search)
936 ? XSTRING (current_buffer->case_canon_table)->data : 0);
937
938 /* Find the first buffer and its substring. */
939
940 if (NILP (buffer1))
941 bp1 = current_buffer;
942 else
943 {
3fff2dfa
RS
944 Lisp_Object buf1;
945 buf1 = Fget_buffer (buffer1);
946 if (NILP (buf1))
947 nsberror (buffer1);
948 bp1 = XBUFFER (buf1);
e9cf2084
RS
949 }
950
951 if (NILP (start1))
952 begp1 = BUF_BEGV (bp1);
953 else
954 {
955 CHECK_NUMBER_COERCE_MARKER (start1, 1);
956 begp1 = XINT (start1);
957 }
958 if (NILP (end1))
959 endp1 = BUF_ZV (bp1);
960 else
961 {
962 CHECK_NUMBER_COERCE_MARKER (end1, 2);
963 endp1 = XINT (end1);
964 }
965
966 if (begp1 > endp1)
967 temp = begp1, begp1 = endp1, endp1 = temp;
968
969 if (!(BUF_BEGV (bp1) <= begp1
970 && begp1 <= endp1
971 && endp1 <= BUF_ZV (bp1)))
972 args_out_of_range (start1, end1);
973
974 /* Likewise for second substring. */
975
976 if (NILP (buffer2))
977 bp2 = current_buffer;
978 else
979 {
3fff2dfa
RS
980 Lisp_Object buf2;
981 buf2 = Fget_buffer (buffer2);
982 if (NILP (buf2))
983 nsberror (buffer2);
e9cf2084
RS
984 bp2 = XBUFFER (buffer2);
985 }
986
987 if (NILP (start2))
988 begp2 = BUF_BEGV (bp2);
989 else
990 {
991 CHECK_NUMBER_COERCE_MARKER (start2, 4);
992 begp2 = XINT (start2);
993 }
994 if (NILP (end2))
995 endp2 = BUF_ZV (bp2);
996 else
997 {
998 CHECK_NUMBER_COERCE_MARKER (end2, 5);
999 endp2 = XINT (end2);
1000 }
1001
1002 if (begp2 > endp2)
1003 temp = begp2, begp2 = endp2, endp2 = temp;
1004
1005 if (!(BUF_BEGV (bp2) <= begp2
1006 && begp2 <= endp2
1007 && endp2 <= BUF_ZV (bp2)))
1008 args_out_of_range (start2, end2);
1009
1010 len1 = endp1 - begp1;
1011 len2 = endp2 - begp2;
1012 length = len1;
1013 if (len2 < length)
1014 length = len2;
1015
1016 for (i = 0; i < length; i++)
1017 {
1018 int c1 = *BUF_CHAR_ADDRESS (bp1, begp1 + i);
1019 int c2 = *BUF_CHAR_ADDRESS (bp2, begp2 + i);
1020 if (trt)
1021 {
1022 c1 = trt[c1];
1023 c2 = trt[c2];
1024 }
1025 if (c1 < c2)
1026 return make_number (- 1 - i);
1027 if (c1 > c2)
1028 return make_number (i + 1);
1029 }
1030
1031 /* The strings match as far as they go.
1032 If one is shorter, that one is less. */
1033 if (length < len1)
1034 return make_number (length + 1);
1035 else if (length < len2)
1036 return make_number (- length - 1);
1037
1038 /* Same length too => they are equal. */
1039 return make_number (0);
1040}
35692fe0
JB
1041\f
1042DEFUN ("subst-char-in-region", Fsubst_char_in_region,
1043 Ssubst_char_in_region, 4, 5, 0,
1044 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1045If optional arg NOUNDO is non-nil, don't record this change for undo\n\
1046and don't mark the buffer as really changed.")
1047 (start, end, fromchar, tochar, noundo)
1048 Lisp_Object start, end, fromchar, tochar, noundo;
1049{
1050 register int pos, stop, look;
1051
1052 validate_region (&start, &end);
1053 CHECK_NUMBER (fromchar, 2);
1054 CHECK_NUMBER (tochar, 3);
1055
1056 pos = XINT (start);
1057 stop = XINT (end);
1058 look = XINT (fromchar);
1059
04a759c8 1060 modify_region (current_buffer, pos, stop);
56a98455 1061 if (! NILP (noundo))
35692fe0
JB
1062 {
1063 if (MODIFF - 1 == current_buffer->save_modified)
1064 current_buffer->save_modified++;
1065 if (MODIFF - 1 == current_buffer->auto_save_modified)
1066 current_buffer->auto_save_modified++;
1067 }
1068
1069 while (pos < stop)
1070 {
1071 if (FETCH_CHAR (pos) == look)
1072 {
56a98455 1073 if (NILP (noundo))
35692fe0
JB
1074 record_change (pos, 1);
1075 FETCH_CHAR (pos) = XINT (tochar);
56a98455 1076 if (NILP (noundo))
35692fe0
JB
1077 signal_after_change (pos, 1, 1);
1078 }
1079 pos++;
1080 }
1081
1082 return Qnil;
1083}
1084
1085DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
1086 "From START to END, translate characters according to TABLE.\n\
1087TABLE is a string; the Nth character in it is the mapping\n\
1088for the character with code N. Returns the number of characters changed.")
1089 (start, end, table)
1090 Lisp_Object start;
1091 Lisp_Object end;
1092 register Lisp_Object table;
1093{
1094 register int pos, stop; /* Limits of the region. */
1095 register unsigned char *tt; /* Trans table. */
1096 register int oc; /* Old character. */
1097 register int nc; /* New character. */
1098 int cnt; /* Number of changes made. */
1099 Lisp_Object z; /* Return. */
1100 int size; /* Size of translate table. */
1101
1102 validate_region (&start, &end);
1103 CHECK_STRING (table, 2);
1104
1105 size = XSTRING (table)->size;
1106 tt = XSTRING (table)->data;
1107
1108 pos = XINT (start);
1109 stop = XINT (end);
04a759c8 1110 modify_region (current_buffer, pos, stop);
35692fe0
JB
1111
1112 cnt = 0;
1113 for (; pos < stop; ++pos)
1114 {
1115 oc = FETCH_CHAR (pos);
1116 if (oc < size)
1117 {
1118 nc = tt[oc];
1119 if (nc != oc)
1120 {
1121 record_change (pos, 1);
1122 FETCH_CHAR (pos) = nc;
1123 signal_after_change (pos, 1, 1);
1124 ++cnt;
1125 }
1126 }
1127 }
1128
1129 XFASTINT (z) = cnt;
1130 return (z);
1131}
1132
1133DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
1134 "Delete the text between point and mark.\n\
1135When called from a program, expects two arguments,\n\
1136positions (integers or markers) specifying the stretch to be deleted.")
1137 (b, e)
1138 Lisp_Object b, e;
1139{
1140 validate_region (&b, &e);
1141 del_range (XINT (b), XINT (e));
1142 return Qnil;
1143}
1144\f
1145DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
1146 "Remove restrictions (narrowing) from current buffer.\n\
1147This allows the buffer's full text to be seen and edited.")
1148 ()
1149{
1150 BEGV = BEG;
1151 SET_BUF_ZV (current_buffer, Z);
1152 clip_changed = 1;
52b14ac0
JB
1153 /* Changing the buffer bounds invalidates any recorded current column. */
1154 invalidate_current_column ();
35692fe0
JB
1155 return Qnil;
1156}
1157
1158DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
1159 "Restrict editing in this buffer to the current region.\n\
1160The rest of the text becomes temporarily invisible and untouchable\n\
1161but is not deleted; if you save the buffer in a file, the invisible\n\
1162text is included in the file. \\[widen] makes all visible again.\n\
1163See also `save-restriction'.\n\
1164\n\
1165When calling from a program, pass two arguments; positions (integers\n\
1166or markers) bounding the text that should remain visible.")
1167 (b, e)
1168 register Lisp_Object b, e;
1169{
1170 register int i;
1171
1172 CHECK_NUMBER_COERCE_MARKER (b, 0);
1173 CHECK_NUMBER_COERCE_MARKER (e, 1);
1174
1175 if (XINT (b) > XINT (e))
1176 {
1177 i = XFASTINT (b);
1178 b = e;
1179 XFASTINT (e) = i;
1180 }
1181
1182 if (!(BEG <= XINT (b) && XINT (b) <= XINT (e) && XINT (e) <= Z))
1183 args_out_of_range (b, e);
1184
1185 BEGV = XFASTINT (b);
1186 SET_BUF_ZV (current_buffer, XFASTINT (e));
1187 if (point < XFASTINT (b))
1188 SET_PT (XFASTINT (b));
1189 if (point > XFASTINT (e))
1190 SET_PT (XFASTINT (e));
1191 clip_changed = 1;
52b14ac0
JB
1192 /* Changing the buffer bounds invalidates any recorded current column. */
1193 invalidate_current_column ();
35692fe0
JB
1194 return Qnil;
1195}
1196
1197Lisp_Object
1198save_restriction_save ()
1199{
1200 register Lisp_Object bottom, top;
1201 /* Note: I tried using markers here, but it does not win
1202 because insertion at the end of the saved region
1203 does not advance mh and is considered "outside" the saved region. */
1204 XFASTINT (bottom) = BEGV - BEG;
1205 XFASTINT (top) = Z - ZV;
1206
1207 return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
1208}
1209
1210Lisp_Object
1211save_restriction_restore (data)
1212 Lisp_Object data;
1213{
1214 register struct buffer *buf;
1215 register int newhead, newtail;
1216 register Lisp_Object tem;
1217
1218 buf = XBUFFER (XCONS (data)->car);
1219
1220 data = XCONS (data)->cdr;
1221
1222 tem = XCONS (data)->car;
1223 newhead = XINT (tem);
1224 tem = XCONS (data)->cdr;
1225 newtail = XINT (tem);
1226 if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
1227 {
1228 newhead = 0;
1229 newtail = 0;
1230 }
1231 BUF_BEGV (buf) = BUF_BEG (buf) + newhead;
1232 SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
1233 clip_changed = 1;
1234
1235 /* If point is outside the new visible range, move it inside. */
1236 SET_BUF_PT (buf,
1237 clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)));
1238
1239 return Qnil;
1240}
1241
1242DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
1243 "Execute BODY, saving and restoring current buffer's restrictions.\n\
1244The buffer's restrictions make parts of the beginning and end invisible.\n\
1245\(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
1246This special form, `save-restriction', saves the current buffer's restrictions\n\
1247when it is entered, and restores them when it is exited.\n\
1248So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
1249The old restrictions settings are restored\n\
1250even in case of abnormal exit (throw or error).\n\
1251\n\
1252The value returned is the value of the last form in BODY.\n\
1253\n\
1254`save-restriction' can get confused if, within the BODY, you widen\n\
1255and then make changes outside the area within the saved restrictions.\n\
1256\n\
1257Note: if you are using both `save-excursion' and `save-restriction',\n\
1258use `save-excursion' outermost:\n\
1259 (save-excursion (save-restriction ...))")
1260 (body)
1261 Lisp_Object body;
1262{
1263 register Lisp_Object val;
1264 int count = specpdl_ptr - specpdl;
1265
1266 record_unwind_protect (save_restriction_restore, save_restriction_save ());
1267 val = Fprogn (body);
1268 return unbind_to (count, val);
1269}
1270\f
1271DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
1272 "Print a one-line message at the bottom of the screen.\n\
1273The first argument is a control string.\n\
1274It may contain %s or %d or %c to print successive following arguments.\n\
1275%s means print an argument as a string, %d means print as number in decimal,\n\
1276%c means print a number as a single character.\n\
1277The argument used by %s must be a string or a symbol;\n\
ccdac5be
JB
1278the argument used by %d or %c must be a number.\n\
1279If the first argument is nil, clear any existing message; let the\n\
1280minibuffer contents show.")
35692fe0
JB
1281 (nargs, args)
1282 int nargs;
1283 Lisp_Object *args;
1284{
ccdac5be 1285 if (NILP (args[0]))
f0250249
JB
1286 {
1287 message (0);
1288 return Qnil;
1289 }
ccdac5be
JB
1290 else
1291 {
1292 register Lisp_Object val;
1293 val = Fformat (nargs, args);
1294 message ("%s", XSTRING (val)->data);
1295 return val;
1296 }
35692fe0
JB
1297}
1298
1299DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
1300 "Format a string out of a control-string and arguments.\n\
1301The first argument is a control string.\n\
1302The other arguments are substituted into it to make the result, a string.\n\
1303It may contain %-sequences meaning to substitute the next argument.\n\
1304%s means print a string argument. Actually, prints any object, with `princ'.\n\
1305%d means print as number in decimal (%o octal, %x hex).\n\
1306%c means print a number as a single character.\n\
1307%S means print any object as an s-expression (using prin1).\n\
52b14ac0
JB
1308 The argument used for %d, %o, %x or %c must be a number.\n\
1309Use %% to put a single % into the output.")
35692fe0
JB
1310 (nargs, args)
1311 int nargs;
1312 register Lisp_Object *args;
1313{
1314 register int n; /* The number of the next arg to substitute */
1315 register int total = 5; /* An estimate of the final length */
1316 char *buf;
1317 register unsigned char *format, *end;
1318 int length;
1319 extern char *index ();
1320 /* It should not be necessary to GCPRO ARGS, because
1321 the caller in the interpreter should take care of that. */
1322
1323 CHECK_STRING (args[0], 0);
1324 format = XSTRING (args[0])->data;
1325 end = format + XSTRING (args[0])->size;
1326
1327 n = 0;
1328 while (format != end)
1329 if (*format++ == '%')
1330 {
1331 int minlen;
1332
1333 /* Process a numeric arg and skip it. */
1334 minlen = atoi (format);
1335 if (minlen > 0)
1336 total += minlen;
1337 else
1338 total -= minlen;
1339 while ((*format >= '0' && *format <= '9')
1340 || *format == '-' || *format == ' ' || *format == '.')
1341 format++;
1342
1343 if (*format == '%')
1344 format++;
1345 else if (++n >= nargs)
1346 ;
1347 else if (*format == 'S')
1348 {
1349 /* For `S', prin1 the argument and then treat like a string. */
1350 register Lisp_Object tem;
1351 tem = Fprin1_to_string (args[n], Qnil);
1352 args[n] = tem;
1353 goto string;
1354 }
1355 else if (XTYPE (args[n]) == Lisp_Symbol)
1356 {
1357 XSET (args[n], Lisp_String, XSYMBOL (args[n])->name);
1358 goto string;
1359 }
1360 else if (XTYPE (args[n]) == Lisp_String)
1361 {
1362 string:
1363 total += XSTRING (args[n])->size;
1364 }
1365 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
1366 else if (XTYPE (args[n]) == Lisp_Int && *format != 's')
1367 {
4746118a 1368#ifdef LISP_FLOAT_TYPE
eb8c3be9 1369 /* The following loop assumes the Lisp type indicates
35692fe0
JB
1370 the proper way to pass the argument.
1371 So make sure we have a flonum if the argument should
1372 be a double. */
1373 if (*format == 'e' || *format == 'f' || *format == 'g')
1374 args[n] = Ffloat (args[n]);
4746118a 1375#endif
35692fe0
JB
1376 total += 10;
1377 }
4746118a 1378#ifdef LISP_FLOAT_TYPE
35692fe0
JB
1379 else if (XTYPE (args[n]) == Lisp_Float && *format != 's')
1380 {
1381 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
1382 args[n] = Ftruncate (args[n]);
1383 total += 20;
1384 }
4746118a 1385#endif
35692fe0
JB
1386 else
1387 {
1388 /* Anything but a string, convert to a string using princ. */
1389 register Lisp_Object tem;
1390 tem = Fprin1_to_string (args[n], Qt);
1391 args[n] = tem;
1392 goto string;
1393 }
1394 }
1395
1396 {
1397 register int nstrings = n + 1;
50aa2f90
JB
1398
1399 /* Allocate twice as many strings as we have %-escapes; floats occupy
1400 two slots, and we're not sure how many of those we have. */
35692fe0 1401 register unsigned char **strings
50aa2f90
JB
1402 = (unsigned char **) alloca (2 * nstrings * sizeof (unsigned char *));
1403 int i;
35692fe0 1404
50aa2f90 1405 i = 0;
35692fe0
JB
1406 for (n = 0; n < nstrings; n++)
1407 {
1408 if (n >= nargs)
50aa2f90 1409 strings[i++] = (unsigned char *) "";
35692fe0
JB
1410 else if (XTYPE (args[n]) == Lisp_Int)
1411 /* We checked above that the corresponding format effector
1412 isn't %s, which would cause MPV. */
50aa2f90 1413 strings[i++] = (unsigned char *) XINT (args[n]);
4746118a 1414#ifdef LISP_FLOAT_TYPE
35692fe0
JB
1415 else if (XTYPE (args[n]) == Lisp_Float)
1416 {
1417 union { double d; int half[2]; } u;
1418
1419 u.d = XFLOAT (args[n])->data;
50aa2f90
JB
1420 strings[i++] = (unsigned char *) u.half[0];
1421 strings[i++] = (unsigned char *) u.half[1];
35692fe0 1422 }
4746118a 1423#endif
35692fe0 1424 else
50aa2f90 1425 strings[i++] = XSTRING (args[n])->data;
35692fe0
JB
1426 }
1427
1428 /* Format it in bigger and bigger buf's until it all fits. */
1429 while (1)
1430 {
1431 buf = (char *) alloca (total + 1);
1432 buf[total - 1] = 0;
1433
50aa2f90 1434 length = doprnt (buf, total + 1, strings[0], end, i-1, strings + 1);
35692fe0
JB
1435 if (buf[total - 1] == 0)
1436 break;
1437
1438 total *= 2;
1439 }
1440 }
1441
1442 /* UNGCPRO; */
1443 return make_string (buf, length);
1444}
1445
1446/* VARARGS 1 */
1447Lisp_Object
1448#ifdef NO_ARG_ARRAY
1449format1 (string1, arg0, arg1, arg2, arg3, arg4)
1450 int arg0, arg1, arg2, arg3, arg4;
1451#else
1452format1 (string1)
1453#endif
1454 char *string1;
1455{
1456 char buf[100];
1457#ifdef NO_ARG_ARRAY
1458 int args[5];
1459 args[0] = arg0;
1460 args[1] = arg1;
1461 args[2] = arg2;
1462 args[3] = arg3;
1463 args[4] = arg4;
1464 doprnt (buf, sizeof buf, string1, 0, 5, args);
1465#else
1466 doprnt (buf, sizeof buf, string1, 0, 5, &string1 + 1);
1467#endif
1468 return build_string (buf);
1469}
1470\f
1471DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
1472 "Return t if two characters match, optionally ignoring case.\n\
1473Both arguments must be characters (i.e. integers).\n\
1474Case is ignored if `case-fold-search' is non-nil in the current buffer.")
1475 (c1, c2)
1476 register Lisp_Object c1, c2;
1477{
1478 unsigned char *downcase = DOWNCASE_TABLE;
1479 CHECK_NUMBER (c1, 0);
1480 CHECK_NUMBER (c2, 1);
1481
56a98455 1482 if (!NILP (current_buffer->case_fold_search)
c34beca9
RS
1483 ? (downcase[0xff & XFASTINT (c1)] == downcase[0xff & XFASTINT (c2)]
1484 && (XFASTINT (c1) & ~0xff) == (XFASTINT (c2) & ~0xff))
35692fe0
JB
1485 : XINT (c1) == XINT (c2))
1486 return Qt;
1487 return Qnil;
1488}
1489
35692fe0
JB
1490\f
1491void
1492syms_of_editfns ()
1493{
1494 DEFVAR_LISP ("system-name", &Vsystem_name,
1495 "The name of the machine Emacs is running on.");
1496
1497 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
1498 "The full name of the user logged in.");
1499
1500 DEFVAR_LISP ("user-name", &Vuser_name,
1501 "The user's name, based on the effective uid.");
1502
1503 DEFVAR_LISP ("user-real-name", &Vuser_real_name,
1504 "The user's name, base upon the real uid.");
1505
1506 defsubr (&Schar_equal);
1507 defsubr (&Sgoto_char);
1508 defsubr (&Sstring_to_char);
1509 defsubr (&Schar_to_string);
1510 defsubr (&Sbuffer_substring);
1511 defsubr (&Sbuffer_string);
1512
1513 defsubr (&Spoint_marker);
1514 defsubr (&Smark_marker);
1515 defsubr (&Spoint);
1516 defsubr (&Sregion_beginning);
1517 defsubr (&Sregion_end);
1518/* defsubr (&Smark); */
1519/* defsubr (&Sset_mark); */
1520 defsubr (&Ssave_excursion);
1521
1522 defsubr (&Sbufsize);
1523 defsubr (&Spoint_max);
1524 defsubr (&Spoint_min);
1525 defsubr (&Spoint_min_marker);
1526 defsubr (&Spoint_max_marker);
1527
1528 defsubr (&Sbobp);
1529 defsubr (&Seobp);
1530 defsubr (&Sbolp);
1531 defsubr (&Seolp);
850a8179
JB
1532 defsubr (&Sfollowing_char);
1533 defsubr (&Sprevious_char);
35692fe0
JB
1534 defsubr (&Schar_after);
1535 defsubr (&Sinsert);
1536 defsubr (&Sinsert_before_markers);
1537 defsubr (&Sinsert_char);
1538
1539 defsubr (&Suser_login_name);
1540 defsubr (&Suser_real_login_name);
1541 defsubr (&Suser_uid);
1542 defsubr (&Suser_real_uid);
1543 defsubr (&Suser_full_name);
d940e0e4 1544 defsubr (&Scurrent_time);
35692fe0 1545 defsubr (&Scurrent_time_string);
c2662aea 1546 defsubr (&Scurrent_time_zone);
35692fe0 1547 defsubr (&Ssystem_name);
35692fe0
JB
1548 defsubr (&Smessage);
1549 defsubr (&Sformat);
35692fe0
JB
1550
1551 defsubr (&Sinsert_buffer_substring);
e9cf2084 1552 defsubr (&Scompare_buffer_substrings);
35692fe0
JB
1553 defsubr (&Ssubst_char_in_region);
1554 defsubr (&Stranslate_region);
1555 defsubr (&Sdelete_region);
1556 defsubr (&Swiden);
1557 defsubr (&Snarrow_to_region);
1558 defsubr (&Ssave_restriction);
1559}