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