Change prototype of resize_mini_window.
[bpt/emacs.git] / src / editfns.c
CommitLineData
35692fe0 1/* Lisp functions pertaining to editing.
31c8f881 2 Copyright (C) 1985,86,87,89,93,94,95,96,97,98 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
dfcf069d
AS
32#ifdef STDC_HEADERS
33#include <stdlib.h>
34#endif
35
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
39
35692fe0 40#include "lisp.h"
74d6d8c5 41#include "intervals.h"
35692fe0 42#include "buffer.h"
fb8106e8 43#include "charset.h"
35692fe0
JB
44#include "window.h"
45
956ace37 46#include "systime.h"
35692fe0
JB
47
48#define min(a, b) ((a) < (b) ? (a) : (b))
49#define max(a, b) ((a) > (b) ? (a) : (b))
50
a03eaf1c
RS
51#ifndef NULL
52#define NULL 0
53#endif
54
c59b5089
PE
55extern char **environ;
56extern Lisp_Object make_time ();
b1b0ee5a 57extern void insert_from_buffer ();
94751666 58static int tm_diff ();
260e2e2a 59static void update_buffer_properties ();
b771d0da 60size_t emacs_strftime ();
a92ae0ce 61void set_time_zone_rule ();
260e2e2a
KH
62
63Lisp_Object Vbuffer_access_fontify_functions;
64Lisp_Object Qbuffer_access_fontify_functions;
65Lisp_Object Vbuffer_access_fontified_property;
b1b0ee5a 66
e3ed8469
KH
67Lisp_Object Fuser_full_name ();
68
35692fe0
JB
69/* Some static data, and a function to initialize it for each run */
70
71Lisp_Object Vsystem_name;
35b34f72
KH
72Lisp_Object Vuser_real_login_name; /* login name of current user ID */
73Lisp_Object Vuser_full_name; /* full name of current user */
74Lisp_Object Vuser_login_name; /* user name from LOGNAME or USER */
35692fe0
JB
75
76void
77init_editfns ()
78{
52b14ac0 79 char *user_name;
35692fe0
JB
80 register unsigned char *p, *q, *r;
81 struct passwd *pw; /* password entry for the current user */
35692fe0
JB
82 Lisp_Object tem;
83
84 /* Set up system_name even when dumping. */
ac988277 85 init_system_name ();
35692fe0
JB
86
87#ifndef CANNOT_DUMP
88 /* Don't bother with this on initial start when just dumping out */
89 if (!initialized)
90 return;
91#endif /* not CANNOT_DUMP */
92
93 pw = (struct passwd *) getpwuid (getuid ());
87485d6f
MW
94#ifdef MSDOS
95 /* We let the real user name default to "root" because that's quite
96 accurate on MSDOG and because it lets Emacs find the init file.
97 (The DVX libraries override the Djgpp libraries here.) */
35b34f72 98 Vuser_real_login_name = build_string (pw ? pw->pw_name : "root");
87485d6f 99#else
35b34f72 100 Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown");
87485d6f 101#endif
35692fe0 102
52b14ac0
JB
103 /* Get the effective user name, by consulting environment variables,
104 or the effective uid if those are unset. */
2c9ae24e 105 user_name = (char *) getenv ("LOGNAME");
35692fe0 106 if (!user_name)
4691c06d
RS
107#ifdef WINDOWSNT
108 user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */
109#else /* WINDOWSNT */
2c9ae24e 110 user_name = (char *) getenv ("USER");
4691c06d 111#endif /* WINDOWSNT */
52b14ac0
JB
112 if (!user_name)
113 {
114 pw = (struct passwd *) getpwuid (geteuid ());
115 user_name = (char *) (pw ? pw->pw_name : "unknown");
116 }
35b34f72 117 Vuser_login_name = build_string (user_name);
35692fe0 118
52b14ac0
JB
119 /* If the user name claimed in the environment vars differs from
120 the real uid, use the claimed name to find the full name. */
35b34f72 121 tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name);
3415b0e9
RS
122 Vuser_full_name = Fuser_full_name (NILP (tem)? make_number (geteuid())
123 : Vuser_login_name);
35692fe0 124
8f1e2d16 125 p = (unsigned char *) getenv ("NAME");
9d36d071
RS
126 if (p)
127 Vuser_full_name = build_string (p);
3347526c
RS
128 else if (NILP (Vuser_full_name))
129 Vuser_full_name = build_string ("unknown");
35692fe0
JB
130}
131\f
132DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
5f75e666 133 "Convert arg CHAR to a string containing that character.")
2591ec64
EN
134 (character)
135 Lisp_Object character;
35692fe0 136{
fb8106e8 137 int len;
d11ba98c 138 unsigned char workbuf[4], *str;
fb8106e8 139
2591ec64 140 CHECK_NUMBER (character, 0);
35692fe0 141
fb8106e8 142 len = CHAR_STRING (XFASTINT (character), workbuf, str);
5f75e666 143 return make_string_from_bytes (str, 1, len);
35692fe0
JB
144}
145
146DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
fb8106e8
KH
147 "Convert arg STRING to a character, the first character of that string.\n\
148A multibyte character is handled correctly.")
2591ec64
EN
149 (string)
150 register Lisp_Object string;
35692fe0
JB
151{
152 register Lisp_Object val;
153 register struct Lisp_String *p;
2591ec64 154 CHECK_STRING (string, 0);
2591ec64 155 p = XSTRING (string);
35692fe0 156 if (p->size)
d9d851ea
KH
157 {
158 if (STRING_MULTIBYTE (string))
159 XSETFASTINT (val, STRING_CHAR (p->data, STRING_BYTES (p)));
160 else
161 XSETFASTINT (val, p->data[0]);
162 }
35692fe0 163 else
55561c63 164 XSETFASTINT (val, 0);
35692fe0
JB
165 return val;
166}
167\f
168static Lisp_Object
ec1c14f6
RS
169buildmark (charpos, bytepos)
170 int charpos, bytepos;
35692fe0
JB
171{
172 register Lisp_Object mark;
173 mark = Fmake_marker ();
ec1c14f6 174 set_marker_both (mark, Qnil, charpos, bytepos);
35692fe0
JB
175 return mark;
176}
177
178DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
179 "Return value of point, as an integer.\n\
180Beginning of buffer is position (point-min)")
181 ()
182{
183 Lisp_Object temp;
6ec8bbd2 184 XSETFASTINT (temp, PT);
35692fe0
JB
185 return temp;
186}
187
188DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
189 "Return value of point, as a marker object.")
190 ()
191{
ec1c14f6 192 return buildmark (PT, PT_BYTE);
35692fe0
JB
193}
194
195int
196clip_to_bounds (lower, num, upper)
197 int lower, num, upper;
198{
199 if (num < lower)
200 return lower;
201 else if (num > upper)
202 return upper;
203 else
204 return num;
205}
206
207DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
208 "Set point to POSITION, a number or marker.\n\
fb8106e8
KH
209Beginning of buffer is position (point-min), end is (point-max).\n\
210If the position is in the middle of a multibyte form,\n\
211the actual point is set at the head of the multibyte form\n\
212except in the case that `enable-multibyte-characters' is nil.")
2591ec64
EN
213 (position)
214 register Lisp_Object position;
35692fe0 215{
fb8106e8
KH
216 int pos;
217 unsigned char *p;
218
72ef82ec
RS
219 if (MARKERP (position)
220 && current_buffer == XMARKER (position)->buffer)
ec1c14f6
RS
221 {
222 pos = marker_position (position);
223 if (pos < BEGV)
224 SET_PT_BOTH (BEGV, BEGV_BYTE);
225 else if (pos > ZV)
226 SET_PT_BOTH (ZV, ZV_BYTE);
227 else
228 SET_PT_BOTH (pos, marker_byte_position (position));
229
230 return position;
231 }
232
2591ec64 233 CHECK_NUMBER_COERCE_MARKER (position, 0);
35692fe0 234
fb8106e8 235 pos = clip_to_bounds (BEGV, XINT (position), ZV);
fb8106e8 236 SET_PT (pos);
2591ec64 237 return position;
35692fe0
JB
238}
239
240static Lisp_Object
241region_limit (beginningp)
242 int beginningp;
243{
646d9d18 244 extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */
35692fe0 245 register Lisp_Object m;
c9dd14e1
RM
246 if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
247 && NILP (current_buffer->mark_active))
248 Fsignal (Qmark_inactive, Qnil);
35692fe0 249 m = Fmarker_position (current_buffer->mark);
56a98455 250 if (NILP (m)) error ("There is no region now");
6ec8bbd2
KH
251 if ((PT < XFASTINT (m)) == beginningp)
252 return (make_number (PT));
35692fe0
JB
253 else
254 return (m);
255}
256
257DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
258 "Return position of beginning of region, as an integer.")
259 ()
260{
261 return (region_limit (1));
262}
263
264DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
265 "Return position of end of region, as an integer.")
266 ()
267{
268 return (region_limit (0));
269}
270
35692fe0
JB
271DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
272 "Return this buffer's mark, as a marker object.\n\
273Watch out! Moving this marker changes the mark position.\n\
274If you set the marker not to point anywhere, the buffer will have no mark.")
275 ()
276{
277 return current_buffer->mark;
278}
c9ed721d
RS
279\f
280DEFUN ("line-beginning-position", Fline_beginning_position, Sline_beginning_position,
281 0, 1, 0,
282 "Return the character position of the first character on the current line.\n\
283With argument N not nil or 1, move forward N - 1 lines first.\n\
284If scan reaches end of buffer, return that position.\n\
e2dae3f2
RS
285This function does not move point.\n\n\
286In the minibuffer, if point is not within the prompt,\n\
287the return value is never within the prompt either.")
288
c9ed721d
RS
289 (n)
290 Lisp_Object n;
291{
ec1c14f6 292 register int orig, orig_byte, end;
c9ed721d
RS
293
294 if (NILP (n))
295 XSETFASTINT (n, 1);
296 else
297 CHECK_NUMBER (n, 0);
298
299 orig = PT;
ec1c14f6 300 orig_byte = PT_BYTE;
c9ed721d
RS
301 Fforward_line (make_number (XINT (n) - 1));
302 end = PT;
e2dae3f2
RS
303
304 if (INTEGERP (current_buffer->minibuffer_prompt_length)
305 && orig >= XFASTINT (current_buffer->minibuffer_prompt_length)
306 && end < XFASTINT (current_buffer->minibuffer_prompt_length))
307 end = XFASTINT (current_buffer->minibuffer_prompt_length);
308
ec1c14f6 309 SET_PT_BOTH (orig, orig_byte);
35692fe0 310
c9ed721d
RS
311 return make_number (end);
312}
313
314DEFUN ("line-end-position", Fline_end_position, Sline_end_position,
315 0, 1, 0,
316 "Return the character position of the last character on the current line.\n\
317With argument N not nil or 1, move forward N - 1 lines first.\n\
318If scan reaches end of buffer, return that position.\n\
319This function does not move point.")
320 (n)
321 Lisp_Object n;
322{
323 if (NILP (n))
324 XSETFASTINT (n, 1);
325 else
326 CHECK_NUMBER (n, 0);
327
328 return make_number (find_before_next_newline
329 (PT, 0, XINT (n) - (XINT (n) <= 0)));
330}
331\f
35692fe0
JB
332Lisp_Object
333save_excursion_save ()
334{
0e2c9c70
JB
335 register int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
336 == current_buffer);
35692fe0
JB
337
338 return Fcons (Fpoint_marker (),
aea4a109 339 Fcons (Fcopy_marker (current_buffer->mark, Qnil),
9772455e
RS
340 Fcons (visible ? Qt : Qnil,
341 current_buffer->mark_active)));
35692fe0
JB
342}
343
344Lisp_Object
345save_excursion_restore (info)
4ad8681a 346 Lisp_Object info;
35692fe0 347{
4ad8681a
RS
348 Lisp_Object tem, tem1, omark, nmark;
349 struct gcpro gcpro1, gcpro2, gcpro3;
35692fe0
JB
350
351 tem = Fmarker_buffer (Fcar (info));
352 /* If buffer being returned to is now deleted, avoid error */
353 /* Otherwise could get error here while unwinding to top level
354 and crash */
355 /* In that case, Fmarker_buffer returns nil now. */
56a98455 356 if (NILP (tem))
35692fe0 357 return Qnil;
4ad8681a
RS
358
359 omark = nmark = Qnil;
360 GCPRO3 (info, omark, nmark);
361
35692fe0
JB
362 Fset_buffer (tem);
363 tem = Fcar (info);
364 Fgoto_char (tem);
365 unchain_marker (tem);
366 tem = Fcar (Fcdr (info));
03d18690 367 omark = Fmarker_position (current_buffer->mark);
35692fe0 368 Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
03d18690 369 nmark = Fmarker_position (tem);
35692fe0
JB
370 unchain_marker (tem);
371 tem = Fcdr (Fcdr (info));
ef580991
RS
372#if 0 /* We used to make the current buffer visible in the selected window
373 if that was true previously. That avoids some anomalies.
374 But it creates others, and it wasn't documented, and it is simpler
375 and cleaner never to alter the window/buffer connections. */
9772455e
RS
376 tem1 = Fcar (tem);
377 if (!NILP (tem1)
0e2c9c70 378 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
35692fe0 379 Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
ef580991 380#endif /* 0 */
9772455e
RS
381
382 tem1 = current_buffer->mark_active;
383 current_buffer->mark_active = Fcdr (tem);
9fed2b18
RS
384 if (!NILP (Vrun_hooks))
385 {
03d18690
RS
386 /* If mark is active now, and either was not active
387 or was at a different place, run the activate hook. */
9fed2b18 388 if (! NILP (current_buffer->mark_active))
03d18690
RS
389 {
390 if (! EQ (omark, nmark))
391 call1 (Vrun_hooks, intern ("activate-mark-hook"));
392 }
393 /* If mark has ceased to be active, run deactivate hook. */
9fed2b18
RS
394 else if (! NILP (tem1))
395 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
396 }
4ad8681a 397 UNGCPRO;
35692fe0
JB
398 return Qnil;
399}
400
401DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
402 "Save point, mark, and current buffer; execute BODY; restore those things.\n\
403Executes BODY just like `progn'.\n\
404The values of point, mark and the current buffer are restored\n\
9772455e 405even in case of abnormal exit (throw or error).\n\
85cac557
RS
406The state of activation of the mark is also restored.\n\
407\n\
408This construct does not save `deactivate-mark', and therefore\n\
409functions that change the buffer will still cause deactivation\n\
410of the mark at the end of the command. To prevent that, bind\n\
411`deactivate-mark' with `let'.")
35692fe0
JB
412 (args)
413 Lisp_Object args;
414{
415 register Lisp_Object val;
416 int count = specpdl_ptr - specpdl;
417
418 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4bc8c7d2
RS
419
420 val = Fprogn (args);
421 return unbind_to (count, val);
422}
423
424DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0,
425 "Save the current buffer; execute BODY; restore the current buffer.\n\
426Executes BODY just like `progn'.")
427 (args)
428 Lisp_Object args;
429{
430 register Lisp_Object val;
431 int count = specpdl_ptr - specpdl;
432
cb5e5f74 433 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
4bc8c7d2 434
35692fe0
JB
435 val = Fprogn (args);
436 return unbind_to (count, val);
437}
438\f
95dccf75
RS
439DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0,
440 "Return the number of characters in the current buffer.\n\
441If BUFFER, return the number of characters in that buffer instead.")
442 (buffer)
443 Lisp_Object buffer;
35692fe0 444{
95dccf75
RS
445 if (NILP (buffer))
446 return make_number (Z - BEG);
02050596
RS
447 else
448 {
449 CHECK_BUFFER (buffer, 1);
450 return make_number (BUF_Z (XBUFFER (buffer))
451 - BUF_BEG (XBUFFER (buffer)));
452 }
35692fe0
JB
453}
454
455DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
456 "Return the minimum permissible value of point in the current buffer.\n\
4c390850 457This is 1, unless narrowing (a buffer restriction) is in effect.")
35692fe0
JB
458 ()
459{
460 Lisp_Object temp;
55561c63 461 XSETFASTINT (temp, BEGV);
35692fe0
JB
462 return temp;
463}
464
465DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
466 "Return a marker to the minimum permissible value of point in this buffer.\n\
4c390850 467This is the beginning, unless narrowing (a buffer restriction) is in effect.")
35692fe0
JB
468 ()
469{
ec1c14f6 470 return buildmark (BEGV, BEGV_BYTE);
35692fe0
JB
471}
472
473DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
474 "Return the maximum permissible value of point in the current buffer.\n\
4c390850
RS
475This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
476is in effect, in which case it is less.")
35692fe0
JB
477 ()
478{
479 Lisp_Object temp;
55561c63 480 XSETFASTINT (temp, ZV);
35692fe0
JB
481 return temp;
482}
483
484DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
485 "Return a marker to the maximum permissible value of point in this buffer.\n\
4c390850
RS
486This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\
487is in effect, in which case it is less.")
35692fe0
JB
488 ()
489{
ec1c14f6 490 return buildmark (ZV, ZV_BYTE);
35692fe0
JB
491}
492
c86212b9
RS
493DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0,
494 "Return the position of the gap, in the current buffer.\n\
495See also `gap-size'.")
496 ()
497{
498 Lisp_Object temp;
499 XSETFASTINT (temp, GPT);
500 return temp;
501}
502
503DEFUN ("gap-size", Fgap_size, Sgap_size, 0, 0, 0,
504 "Return the size of the current buffer's gap.\n\
505See also `gap-position'.")
506 ()
507{
508 Lisp_Object temp;
509 XSETFASTINT (temp, GAP_SIZE);
510 return temp;
511}
512
7df74da6 513DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0,
fcf9683e
KH
514 "Return the byte position for character position POSITION.\n\
515If POSITION is out of range, the value is nil.")
7df74da6 516 (position)
80e01f8d 517 Lisp_Object position;
7df74da6 518{
fa8a5a15 519 CHECK_NUMBER_COERCE_MARKER (position, 1);
fcf9683e
KH
520 if (XINT (position) < BEG || XINT (position) > Z)
521 return Qnil;
fa8a5a15 522 return make_number (CHAR_TO_BYTE (XINT (position)));
7df74da6 523}
3ab0732d
RS
524
525DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
fcf9683e
KH
526 "Return the character position for byte position BYTEPOS.\n\
527If BYTEPOS is out of range, the value is nil.")
3ab0732d
RS
528 (bytepos)
529 Lisp_Object bytepos;
530{
531 CHECK_NUMBER (bytepos, 1);
fcf9683e
KH
532 if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
533 return Qnil;
3ab0732d
RS
534 return make_number (BYTE_TO_CHAR (XINT (bytepos)));
535}
7df74da6 536\f
850a8179
JB
537DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
538 "Return the character following point, as a number.\n\
fb8106e8
KH
539At the end of the buffer or accessible region, return 0.\n\
540If `enable-multibyte-characters' is nil or point is not\n\
541 at character boundary, multibyte form is ignored,\n\
542 and only one byte following point is returned as a character.")
35692fe0
JB
543 ()
544{
545 Lisp_Object temp;
6ec8bbd2 546 if (PT >= ZV)
55561c63 547 XSETFASTINT (temp, 0);
850a8179 548 else
ec1c14f6 549 XSETFASTINT (temp, FETCH_CHAR (PT_BYTE));
35692fe0
JB
550 return temp;
551}
552
850a8179
JB
553DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
554 "Return the character preceding point, as a number.\n\
fb8106e8
KH
555At the beginning of the buffer or accessible region, return 0.\n\
556If `enable-multibyte-characters' is nil or point is not\n\
557 at character boundary, multi-byte form is ignored,\n\
558 and only one byte preceding point is returned as a character.")
35692fe0
JB
559 ()
560{
561 Lisp_Object temp;
6ec8bbd2 562 if (PT <= BEGV)
55561c63 563 XSETFASTINT (temp, 0);
fb8106e8
KH
564 else if (!NILP (current_buffer->enable_multibyte_characters))
565 {
ec1c14f6 566 int pos = PT_BYTE;
fb8106e8
KH
567 DEC_POS (pos);
568 XSETFASTINT (temp, FETCH_CHAR (pos));
569 }
35692fe0 570 else
ec1c14f6 571 XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1));
35692fe0
JB
572 return temp;
573}
574
575DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
ec1c14f6 576 "Return t if point is at the beginning of the buffer.\n\
35692fe0
JB
577If the buffer is narrowed, this means the beginning of the narrowed part.")
578 ()
579{
6ec8bbd2 580 if (PT == BEGV)
35692fe0
JB
581 return Qt;
582 return Qnil;
583}
584
585DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
ec1c14f6 586 "Return t if point is at the end of the buffer.\n\
35692fe0
JB
587If the buffer is narrowed, this means the end of the narrowed part.")
588 ()
589{
6ec8bbd2 590 if (PT == ZV)
35692fe0
JB
591 return Qt;
592 return Qnil;
593}
594
595DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
ec1c14f6 596 "Return t if point is at the beginning of a line.")
35692fe0
JB
597 ()
598{
ec1c14f6 599 if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n')
35692fe0
JB
600 return Qt;
601 return Qnil;
602}
603
604DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
ec1c14f6 605 "Return t if point is at the end of a line.\n\
35692fe0
JB
606`End of a line' includes point being at the end of the buffer.")
607 ()
608{
ec1c14f6 609 if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n')
35692fe0
JB
610 return Qt;
611 return Qnil;
612}
613
fa1d3816 614DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0,
35692fe0
JB
615 "Return character in current buffer at position POS.\n\
616POS is an integer or a buffer pointer.\n\
39a4c932 617If POS is out of range, the value is nil.")
35692fe0
JB
618 (pos)
619 Lisp_Object pos;
620{
ec1c14f6 621 register int pos_byte;
35692fe0 622 register Lisp_Object val;
35692fe0 623
fa1d3816 624 if (NILP (pos))
39a4c932
RS
625 {
626 pos_byte = PT_BYTE;
3c52e568 627 XSETFASTINT (pos, PT);
39a4c932
RS
628 }
629
630 if (MARKERP (pos))
85cac557
RS
631 {
632 pos_byte = marker_byte_position (pos);
633 if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE)
634 return Qnil;
635 }
fa1d3816
RS
636 else
637 {
638 CHECK_NUMBER_COERCE_MARKER (pos, 0);
b98ef0dc 639 if (XINT (pos) < BEGV || XINT (pos) >= ZV)
85cac557 640 return Qnil;
ec1c14f6
RS
641
642 pos_byte = CHAR_TO_BYTE (XINT (pos));
fa1d3816 643 }
35692fe0 644
ec1c14f6 645 return make_number (FETCH_CHAR (pos_byte));
35692fe0 646}
fb8106e8 647
fa1d3816 648DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0,
fb8106e8
KH
649 "Return character in current buffer preceding position POS.\n\
650POS is an integer or a buffer pointer.\n\
39a4c932 651If POS is out of range, the value is nil.")
fb8106e8
KH
652 (pos)
653 Lisp_Object pos;
654{
655 register Lisp_Object val;
ec1c14f6 656 register int pos_byte;
fb8106e8 657
fa1d3816 658 if (NILP (pos))
39a4c932
RS
659 {
660 pos_byte = PT_BYTE;
3c52e568 661 XSETFASTINT (pos, PT);
39a4c932
RS
662 }
663
664 if (MARKERP (pos))
85cac557
RS
665 {
666 pos_byte = marker_byte_position (pos);
667
668 if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE)
669 return Qnil;
670 }
fa1d3816
RS
671 else
672 {
673 CHECK_NUMBER_COERCE_MARKER (pos, 0);
fb8106e8 674
b98ef0dc 675 if (XINT (pos) <= BEGV || XINT (pos) > ZV)
85cac557
RS
676 return Qnil;
677
ec1c14f6 678 pos_byte = CHAR_TO_BYTE (XINT (pos));
fa1d3816 679 }
fb8106e8
KH
680
681 if (!NILP (current_buffer->enable_multibyte_characters))
682 {
ec1c14f6
RS
683 DEC_POS (pos_byte);
684 XSETFASTINT (val, FETCH_CHAR (pos_byte));
fb8106e8
KH
685 }
686 else
687 {
ec1c14f6
RS
688 pos_byte--;
689 XSETFASTINT (val, FETCH_BYTE (pos_byte));
fb8106e8
KH
690 }
691 return val;
692}
35692fe0 693\f
87485d6f 694DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0,
35692fe0
JB
695 "Return the name under which the user logged in, as a string.\n\
696This is based on the effective uid, not the real uid.\n\
2c9ae24e 697Also, if the environment variable LOGNAME or USER is set,\n\
87485d6f
MW
698that determines the value of this function.\n\n\
699If optional argument UID is an integer, return the login name of the user\n\
700with that uid, or nil if there is no such user.")
701 (uid)
702 Lisp_Object uid;
35692fe0 703{
87485d6f
MW
704 struct passwd *pw;
705
f8a0e364
RS
706 /* Set up the user name info if we didn't do it before.
707 (That can happen if Emacs is dumpable
708 but you decide to run `temacs -l loadup' and not dump. */
35b34f72 709 if (INTEGERP (Vuser_login_name))
f8a0e364 710 init_editfns ();
87485d6f
MW
711
712 if (NILP (uid))
35b34f72 713 return Vuser_login_name;
87485d6f
MW
714
715 CHECK_NUMBER (uid, 0);
716 pw = (struct passwd *) getpwuid (XINT (uid));
717 return (pw ? build_string (pw->pw_name) : Qnil);
35692fe0
JB
718}
719
720DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
721 0, 0, 0,
722 "Return the name of the user's real uid, as a string.\n\
9658bdd0 723This ignores the environment variables LOGNAME and USER, so it differs from\n\
b1da234a 724`user-login-name' when running under `su'.")
35692fe0
JB
725 ()
726{
f8a0e364
RS
727 /* Set up the user name info if we didn't do it before.
728 (That can happen if Emacs is dumpable
729 but you decide to run `temacs -l loadup' and not dump. */
35b34f72 730 if (INTEGERP (Vuser_login_name))
f8a0e364 731 init_editfns ();
35b34f72 732 return Vuser_real_login_name;
35692fe0
JB
733}
734
735DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
736 "Return the effective uid of Emacs, as an integer.")
737 ()
738{
739 return make_number (geteuid ());
740}
741
742DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
743 "Return the real uid of Emacs, as an integer.")
744 ()
745{
746 return make_number (getuid ());
747}
748
c9ed721d
RS
749DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0,
750 "Return the full name of the user logged in, as a string.\n\
016c8f3f
KH
751If the full name corresponding to Emacs's userid is not known,\n\
752return \"unknown\".\n\
753\n\
c9ed721d 754If optional argument UID is an integer, return the full name of the user\n\
016c8f3f 755with that uid, or nil if there is no such user.\n\
3415b0e9 756If UID is a string, return the full name of the user with that login\n\
016c8f3f 757name, or nil if there is no such user.")
c9ed721d
RS
758 (uid)
759 Lisp_Object uid;
35692fe0 760{
c9ed721d 761 struct passwd *pw;
b0e92acd 762 register unsigned char *p, *q;
3415b0e9
RS
763 extern char *index ();
764 Lisp_Object full;
c9ed721d
RS
765
766 if (NILP (uid))
3415b0e9
RS
767 return Vuser_full_name;
768 else if (NUMBERP (uid))
769 pw = (struct passwd *) getpwuid (XINT (uid));
770 else if (STRINGP (uid))
771 pw = (struct passwd *) getpwnam (XSTRING (uid)->data);
772 else
773 error ("Invalid UID specification");
c9ed721d 774
3415b0e9 775 if (!pw)
3347526c 776 return Qnil;
3415b0e9
RS
777
778 p = (unsigned char *) USER_FULL_NAME;
779 /* Chop off everything after the first comma. */
780 q = (unsigned char *) index (p, ',');
781 full = make_string (p, q ? q - p : strlen (p));
782
783#ifdef AMPERSAND_FULL_NAME
784 p = XSTRING (full)->data;
785 q = (unsigned char *) index (p, '&');
786 /* Substitute the login name for the &, upcasing the first character. */
787 if (q)
788 {
b0e92acd 789 register unsigned char *r;
3415b0e9
RS
790 Lisp_Object login;
791
792 login = Fuser_login_name (make_number (pw->pw_uid));
793 r = (unsigned char *) alloca (strlen (p) + XSTRING (login)->size + 1);
794 bcopy (p, r, q - p);
795 r[q - p] = 0;
796 strcat (r, XSTRING (login)->data);
797 r[q - p] = UPCASE (r[q - p]);
798 strcat (r, q + 1);
799 full = build_string (r);
800 }
801#endif /* AMPERSAND_FULL_NAME */
802
803 return full;
35692fe0
JB
804}
805
806DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
807 "Return the name of the machine you are running on, as a string.")
808 ()
809{
810 return Vsystem_name;
811}
812
ac988277
KH
813/* For the benefit of callers who don't want to include lisp.h */
814char *
815get_system_name ()
816{
3d976a9a
RS
817 if (STRINGP (Vsystem_name))
818 return (char *) XSTRING (Vsystem_name)->data;
819 else
820 return "";
ac988277
KH
821}
822
7fd233b3
RS
823DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
824 "Return the process ID of Emacs, as an integer.")
825 ()
826{
827 return make_number (getpid ());
828}
829
d940e0e4 830DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
e983fdb2 831 "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\
956ace37
JB
832The time is returned as a list of three integers. The first has the\n\
833most significant 16 bits of the seconds, while the second has the\n\
834least significant 16 bits. The third integer gives the microsecond\n\
835count.\n\
836\n\
837The microsecond count is zero on systems that do not provide\n\
838resolution finer than a second.")
d940e0e4
JB
839 ()
840{
956ace37
JB
841 EMACS_TIME t;
842 Lisp_Object result[3];
843
844 EMACS_GET_TIME (t);
d2fd0445
KH
845 XSETINT (result[0], (EMACS_SECS (t) >> 16) & 0xffff);
846 XSETINT (result[1], (EMACS_SECS (t) >> 0) & 0xffff);
847 XSETINT (result[2], EMACS_USECS (t));
956ace37
JB
848
849 return Flist (3, result);
d940e0e4
JB
850}
851\f
852
e3120ab5
JB
853static int
854lisp_time_argument (specified_time, result)
855 Lisp_Object specified_time;
856 time_t *result;
857{
858 if (NILP (specified_time))
859 return time (result) != -1;
860 else
861 {
862 Lisp_Object high, low;
863 high = Fcar (specified_time);
864 CHECK_NUMBER (high, 0);
865 low = Fcdr (specified_time);
ae683129 866 if (CONSP (low))
e3120ab5
JB
867 low = Fcar (low);
868 CHECK_NUMBER (low, 0);
869 *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
870 return *result >> 16 == XINT (high);
871 }
872}
873
70ebbe5f
PE
874/* Write information into buffer S of size MAXSIZE, according to the
875 FORMAT of length FORMAT_LEN, using time information taken from *TP.
876 Return the number of bytes written, not including the terminating
877 '\0'. If S is NULL, nothing will be written anywhere; so to
878 determine how many bytes would be written, use NULL for S and
879 ((size_t) -1) for MAXSIZE.
880
881 This function behaves like emacs_strftime, except it allows null
882 bytes in FORMAT. */
883static size_t
884emacs_memftime (s, maxsize, format, format_len, tp)
885 char *s;
886 size_t maxsize;
887 const char *format;
888 size_t format_len;
889 const struct tm *tp;
890{
891 size_t total = 0;
892
be09e6e6
PE
893 /* Loop through all the null-terminated strings in the format
894 argument. Normally there's just one null-terminated string, but
895 there can be arbitrarily many, concatenated together, if the
896 format contains '\0' bytes. emacs_strftime stops at the first
897 '\0' byte so we must invoke it separately for each such string. */
70ebbe5f
PE
898 for (;;)
899 {
900 size_t len;
901 size_t result;
902
903 if (s)
904 s[0] = '\1';
905
906 result = emacs_strftime (s, maxsize, format, tp);
907
908 if (s)
909 {
910 if (result == 0 && s[0] != '\0')
911 return 0;
912 s += result + 1;
913 }
914
915 maxsize -= result + 1;
916 total += result;
917 len = strlen (format);
918 if (len == format_len)
919 return total;
920 total++;
921 format += len + 1;
922 format_len -= len + 1;
923 }
924}
925
3efcc98a 926/*
b48382a0
RS
927DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
928 "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\
929TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\
930`current-time' or `file-attributes'.\n\
931The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\
932as Universal Time; nil means describe TIME in the local time zone.\n\
933The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\
934by text that describes the specified date and time in TIME:\n\
a82d387c 935\n\
b48382a0
RS
936%Y is the year, %y within the century, %C the century.\n\
937%G is the year corresponding to the ISO week, %g within the century.\n\
b771d0da
PE
938%m is the numeric month.\n\
939%b and %h are the locale's abbreviated month name, %B the full name.\n\
b48382a0
RS
940%d is the day of the month, zero-padded, %e is blank-padded.\n\
941%u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\
b771d0da 942%a is the locale's abbreviated name of the day of week, %A the full name.\n\
b48382a0
RS
943%U is the week number starting on Sunday, %W starting on Monday,\n\
944 %V according to ISO 8601.\n\
945%j is the day of the year.\n\
946\n\
947%H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\
948 only blank-padded, %l is like %I blank-padded.\n\
b771d0da 949%p is the locale's equivalent of either AM or PM.\n\
b48382a0
RS
950%M is the minute.\n\
951%S is the second.\n\
952%Z is the time zone name, %z is the numeric form.\n\
953%s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\
954\n\
955%c is the locale's date and time format.\n\
956%x is the locale's \"preferred\" date format.\n\
957%D is like \"%m/%d/%y\".\n\
958\n\
959%R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\
960%X is the locale's \"preferred\" time format.\n\
961\n\
b771d0da 962Finally, %n is a newline, %t is a tab, %% is a literal %.\n\
b48382a0 963\n\
3efcc98a 964Certain flags and modifiers are available with some format controls.\n\
b48382a0
RS
965The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\
966but padded with blanks; %-X is like %X, but without padding.\n\
967%NX (where N stands for an integer) is like %X,\n\
968but takes up at least N (a number) positions.\n\
969The modifiers are `E' and `O'. For certain characters X,\n\
970%EX is a locale's alternative version of %X;\n\
971%OX is like %X, but uses the locale's number symbols.\n\
972\n\
973For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".")
726ed0da 974 (format_string, time, universal)
3efcc98a
RS
975*/
976
977DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
978 0 /* See immediately above */)
b48382a0
RS
979 (format_string, time, universal)
980 Lisp_Object format_string, time, universal;
a82d387c
RS
981{
982 time_t value;
983 int size;
177ea5f1 984 struct tm *tm;
a82d387c
RS
985
986 CHECK_STRING (format_string, 1);
987
988 if (! lisp_time_argument (time, &value))
989 error ("Invalid time specification");
990
991 /* This is probably enough. */
dc3620af 992 size = STRING_BYTES (XSTRING (format_string)) * 6 + 50;
a82d387c 993
177ea5f1
PE
994 tm = NILP (universal) ? localtime (&value) : gmtime (&value);
995 if (! tm)
996 error ("Specified time is not representable");
997
a82d387c
RS
998 while (1)
999 {
b48382a0
RS
1000 char *buf = (char *) alloca (size + 1);
1001 int result;
1002
bfbcc5ee 1003 buf[0] = '\1';
70ebbe5f
PE
1004 result = emacs_memftime (buf, size, XSTRING (format_string)->data,
1005 STRING_BYTES (XSTRING (format_string)),
177ea5f1 1006 tm);
bfbcc5ee 1007 if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
70ebbe5f 1008 return make_string (buf, result);
b48382a0
RS
1009
1010 /* If buffer was too small, make it bigger and try again. */
70ebbe5f
PE
1011 result = emacs_memftime (NULL, (size_t) -1,
1012 XSTRING (format_string)->data,
1013 STRING_BYTES (XSTRING (format_string)),
177ea5f1 1014 tm);
b48382a0 1015 size = result + 1;
a82d387c
RS
1016 }
1017}
1018
4691c06d
RS
1019DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0,
1020 "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\
1021The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\
1022or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\
1023to use the current time. The list has the following nine members:\n\
145b0681
RS
1024SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\
1025only some operating systems support. MINUTE is an integer between 0 and 59.\n\
4691c06d
RS
1026HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\
1027MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\
1028four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\
10290 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\
1030ZONE is an integer indicating the number of seconds east of Greenwich.\n\
2c6c7c72 1031\(Note that Common Lisp has different meanings for DOW and ZONE.)")
4691c06d
RS
1032 (specified_time)
1033 Lisp_Object specified_time;
1034{
1035 time_t time_spec;
3c887943 1036 struct tm save_tm;
4691c06d
RS
1037 struct tm *decoded_time;
1038 Lisp_Object list_args[9];
1039
1040 if (! lisp_time_argument (specified_time, &time_spec))
1041 error ("Invalid time specification");
1042
1043 decoded_time = localtime (&time_spec);
177ea5f1
PE
1044 if (! decoded_time)
1045 error ("Specified time is not representable");
3c887943
KH
1046 XSETFASTINT (list_args[0], decoded_time->tm_sec);
1047 XSETFASTINT (list_args[1], decoded_time->tm_min);
1048 XSETFASTINT (list_args[2], decoded_time->tm_hour);
1049 XSETFASTINT (list_args[3], decoded_time->tm_mday);
1050 XSETFASTINT (list_args[4], decoded_time->tm_mon + 1);
236ebf35 1051 XSETINT (list_args[5], decoded_time->tm_year + 1900);
3c887943 1052 XSETFASTINT (list_args[6], decoded_time->tm_wday);
4691c06d 1053 list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
3c887943
KH
1054
1055 /* Make a copy, in case gmtime modifies the struct. */
1056 save_tm = *decoded_time;
1057 decoded_time = gmtime (&time_spec);
1058 if (decoded_time == 0)
1059 list_args[8] = Qnil;
1060 else
94751666 1061 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
4691c06d
RS
1062 return Flist (9, list_args);
1063}
1064
6ee9061c 1065DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
2591ec64 1066 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\
6ee9061c
RS
1067This is the reverse operation of `decode-time', which see.\n\
1068ZONE defaults to the current time zone rule. This can\n\
085e9fcb 1069be a string or t (as from `set-time-zone-rule'), or it can be a list\n\
7459d65b 1070\(as from `current-time-zone') or an integer (as from `decode-time')\n\
c59b5089 1071applied without consideration for daylight savings time.\n\
6ee9061c
RS
1072\n\
1073You can pass more than 7 arguments; then the first six arguments\n\
1074are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\
1075The intervening arguments are ignored.\n\
1076This feature lets (apply 'encode-time (decode-time ...)) work.\n\
1077\n\
c59b5089
PE
1078Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\
1079for example, a DAY of 0 means the day preceding the given month.\n\
01ba8cce 1080Year numbers less than 100 are treated just like other year numbers.\n\
c59b5089 1081If you want them to stand for years in this century, you must do that yourself.")
6ee9061c
RS
1082 (nargs, args)
1083 int nargs;
1084 register Lisp_Object *args;
cce7b8a0 1085{
1b8fa736 1086 time_t time;
c59b5089 1087 struct tm tm;
60653898 1088 Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
6ee9061c
RS
1089
1090 CHECK_NUMBER (args[0], 0); /* second */
1091 CHECK_NUMBER (args[1], 1); /* minute */
1092 CHECK_NUMBER (args[2], 2); /* hour */
1093 CHECK_NUMBER (args[3], 3); /* day */
1094 CHECK_NUMBER (args[4], 4); /* month */
1095 CHECK_NUMBER (args[5], 5); /* year */
1096
1097 tm.tm_sec = XINT (args[0]);
1098 tm.tm_min = XINT (args[1]);
1099 tm.tm_hour = XINT (args[2]);
1100 tm.tm_mday = XINT (args[3]);
1101 tm.tm_mon = XINT (args[4]) - 1;
1102 tm.tm_year = XINT (args[5]) - 1900;
c59b5089
PE
1103 tm.tm_isdst = -1;
1104
1105 if (CONSP (zone))
1106 zone = Fcar (zone);
1b8fa736 1107 if (NILP (zone))
c59b5089
PE
1108 time = mktime (&tm);
1109 else
1b8fa736 1110 {
c59b5089
PE
1111 char tzbuf[100];
1112 char *tzstring;
1113 char **oldenv = environ, **newenv;
1114
2e34157c 1115 if (EQ (zone, Qt))
085e9fcb
EN
1116 tzstring = "UTC0";
1117 else if (STRINGP (zone))
4d4c1514 1118 tzstring = (char *) XSTRING (zone)->data;
c59b5089 1119 else if (INTEGERP (zone))
1b8fa736 1120 {
c59b5089
PE
1121 int abszone = abs (XINT (zone));
1122 sprintf (tzbuf, "XXX%s%d:%02d:%02d", "-" + (XINT (zone) < 0),
1123 abszone / (60*60), (abszone/60) % 60, abszone % 60);
1124 tzstring = tzbuf;
1b8fa736 1125 }
c59b5089
PE
1126 else
1127 error ("Invalid time zone specification");
1128
1129 /* Set TZ before calling mktime; merely adjusting mktime's returned
1130 value doesn't suffice, since that would mishandle leap seconds. */
1131 set_time_zone_rule (tzstring);
1132
1133 time = mktime (&tm);
1134
1135 /* Restore TZ to previous value. */
1136 newenv = environ;
1137 environ = oldenv;
c0efcacf 1138 xfree (newenv);
c59b5089
PE
1139#ifdef LOCALTIME_CACHE
1140 tzset ();
1141#endif
1b8fa736 1142 }
1b8fa736 1143
c59b5089
PE
1144 if (time == (time_t) -1)
1145 error ("Specified time is not representable");
1146
1147 return make_time (time);
cce7b8a0
RS
1148}
1149
2148f2b4 1150DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
35692fe0 1151 "Return the current time, as a human-readable string.\n\
2148f2b4
RS
1152Programs can use this function to decode a time,\n\
1153since the number of columns in each field is fixed.\n\
1154The format is `Sun Sep 16 01:03:52 1973'.\n\
7a8630da
RS
1155However, see also the functions `decode-time' and `format-time-string'\n\
1156which provide a much more powerful and general facility.\n\
1157\n\
2148f2b4
RS
1158If an argument is given, it specifies a time to format\n\
1159instead of the current time. The argument should have the form:\n\
1160 (HIGH . LOW)\n\
1161or the form:\n\
1162 (HIGH LOW . IGNORED).\n\
1163Thus, you can use times obtained from `current-time'\n\
1164and from `file-attributes'.")
1165 (specified_time)
1166 Lisp_Object specified_time;
1167{
e3120ab5 1168 time_t value;
35692fe0 1169 char buf[30];
2148f2b4
RS
1170 register char *tem;
1171
e3120ab5
JB
1172 if (! lisp_time_argument (specified_time, &value))
1173 value = -1;
2148f2b4 1174 tem = (char *) ctime (&value);
35692fe0
JB
1175
1176 strncpy (buf, tem, 24);
1177 buf[24] = 0;
1178
1179 return build_string (buf);
1180}
c2662aea 1181
94751666 1182#define TM_YEAR_BASE 1900
e3120ab5 1183
94751666
PE
1184/* Yield A - B, measured in seconds.
1185 This function is copied from the GNU C Library. */
1186static int
1187tm_diff (a, b)
e3120ab5
JB
1188 struct tm *a, *b;
1189{
94751666
PE
1190 /* Compute intervening leap days correctly even if year is negative.
1191 Take care to avoid int overflow in leap day calculations,
1192 but it's OK to assume that A and B are close to each other. */
1193 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
1194 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
1195 int a100 = a4 / 25 - (a4 % 25 < 0);
1196 int b100 = b4 / 25 - (b4 % 25 < 0);
1197 int a400 = a100 >> 2;
1198 int b400 = b100 >> 2;
1199 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
1200 int years = a->tm_year - b->tm_year;
1201 int days = (365 * years + intervening_leap_days
1202 + (a->tm_yday - b->tm_yday));
1203 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
1204 + (a->tm_min - b->tm_min))
8e718b4e 1205 + (a->tm_sec - b->tm_sec));
e3120ab5
JB
1206}
1207
1208DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
1209 "Return the offset and name for the local time zone.\n\
1210This returns a list of the form (OFFSET NAME).\n\
1211OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
1212 A negative value means west of Greenwich.\n\
1213NAME is a string giving the name of the time zone.\n\
1214If an argument is given, it specifies when the time zone offset is determined\n\
1215instead of using the current time. The argument should have the form:\n\
1216 (HIGH . LOW)\n\
1217or the form:\n\
1218 (HIGH LOW . IGNORED).\n\
1219Thus, you can use times obtained from `current-time'\n\
1220and from `file-attributes'.\n\
773c1fd3
JB
1221\n\
1222Some operating systems cannot provide all this information to Emacs;\n\
2d88f747 1223in this case, `current-time-zone' returns a list containing nil for\n\
773c1fd3 1224the data it can't find.")
e3120ab5
JB
1225 (specified_time)
1226 Lisp_Object specified_time;
c2662aea 1227{
e3120ab5
JB
1228 time_t value;
1229 struct tm *t;
177ea5f1 1230 struct tm gmt;
c2662aea 1231
e3120ab5 1232 if (lisp_time_argument (specified_time, &value)
177ea5f1
PE
1233 && (t = gmtime (&value)) != 0
1234 && (gmt = *t, t = localtime (&value)) != 0)
e3120ab5 1235 {
177ea5f1
PE
1236 int offset = tm_diff (t, &gmt);
1237 char *s = 0;
1238 char buf[6];
e3120ab5
JB
1239#ifdef HAVE_TM_ZONE
1240 if (t->tm_zone)
5fd4de15 1241 s = (char *)t->tm_zone;
a7971c39
RS
1242#else /* not HAVE_TM_ZONE */
1243#ifdef HAVE_TZNAME
1244 if (t->tm_isdst == 0 || t->tm_isdst == 1)
1245 s = tzname[t->tm_isdst];
c2662aea 1246#endif
a7971c39 1247#endif /* not HAVE_TM_ZONE */
e3120ab5
JB
1248 if (!s)
1249 {
1250 /* No local time zone name is available; use "+-NNNN" instead. */
00fc94d0 1251 int am = (offset < 0 ? -offset : offset) / 60;
e3120ab5
JB
1252 sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
1253 s = buf;
1254 }
1255 return Fcons (make_number (offset), Fcons (build_string (s), Qnil));
1256 }
1257 else
09dbcf71 1258 return Fmake_list (make_number (2), Qnil);
c2662aea
JB
1259}
1260
260e2e2a
KH
1261/* This holds the value of `environ' produced by the previous
1262 call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule
1263 has never been called. */
1264static char **environbuf;
1265
143cb9a9
RS
1266DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0,
1267 "Set the local time zone using TZ, a string specifying a time zone rule.\n\
085e9fcb
EN
1268If TZ is nil, use implementation-defined default time zone information.\n\
1269If TZ is t, use Universal Time.")
143cb9a9
RS
1270 (tz)
1271 Lisp_Object tz;
1272{
143cb9a9
RS
1273 char *tzstring;
1274
1275 if (NILP (tz))
1276 tzstring = 0;
2e34157c 1277 else if (EQ (tz, Qt))
085e9fcb 1278 tzstring = "UTC0";
143cb9a9
RS
1279 else
1280 {
1281 CHECK_STRING (tz, 0);
4d4c1514 1282 tzstring = (char *) XSTRING (tz)->data;
143cb9a9
RS
1283 }
1284
c59b5089
PE
1285 set_time_zone_rule (tzstring);
1286 if (environbuf)
1287 free (environbuf);
1288 environbuf = environ;
1289
1290 return Qnil;
1291}
1292
e0bf9faf
PE
1293#ifdef LOCALTIME_CACHE
1294
1295/* These two values are known to load tz files in buggy implementations,
1296 i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2.
1155c453
RS
1297 Their values shouldn't matter in non-buggy implementations.
1298 We don't use string literals for these strings,
1299 since if a string in the environment is in readonly
1300 storage, it runs afoul of bugs in SVR4 and Solaris 2.3.
1301 See Sun bugs 1113095 and 1114114, ``Timezone routines
1302 improperly modify environment''. */
1303
e0bf9faf
PE
1304static char set_time_zone_rule_tz1[] = "TZ=GMT+0";
1305static char set_time_zone_rule_tz2[] = "TZ=GMT+1";
1306
1307#endif
1155c453 1308
c59b5089
PE
1309/* Set the local time zone rule to TZSTRING.
1310 This allocates memory into `environ', which it is the caller's
1311 responsibility to free. */
a92ae0ce 1312void
c59b5089
PE
1313set_time_zone_rule (tzstring)
1314 char *tzstring;
1315{
1316 int envptrs;
1317 char **from, **to, **newenv;
1318
aafe5147 1319 /* Make the ENVIRON vector longer with room for TZSTRING. */
143cb9a9
RS
1320 for (from = environ; *from; from++)
1321 continue;
1322 envptrs = from - environ + 2;
1323 newenv = to = (char **) xmalloc (envptrs * sizeof (char *)
1324 + (tzstring ? strlen (tzstring) + 4 : 0));
aafe5147
RS
1325
1326 /* Add TZSTRING to the end of environ, as a value for TZ. */
143cb9a9
RS
1327 if (tzstring)
1328 {
1329 char *t = (char *) (to + envptrs);
1330 strcpy (t, "TZ=");
1331 strcat (t, tzstring);
1332 *to++ = t;
1333 }
1334
aafe5147
RS
1335 /* Copy the old environ vector elements into NEWENV,
1336 but don't copy the TZ variable.
1337 So we have only one definition of TZ, which came from TZSTRING. */
143cb9a9
RS
1338 for (from = environ; *from; from++)
1339 if (strncmp (*from, "TZ=", 3) != 0)
1340 *to++ = *from;
1341 *to = 0;
1342
1343 environ = newenv;
143cb9a9 1344
aafe5147
RS
1345 /* If we do have a TZSTRING, NEWENV points to the vector slot where
1346 the TZ variable is stored. If we do not have a TZSTRING,
1347 TO points to the vector slot which has the terminating null. */
1348
143cb9a9 1349#ifdef LOCALTIME_CACHE
aafe5147
RS
1350 {
1351 /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like
1352 "US/Pacific" that loads a tz file, then changes to a value like
1353 "XXX0" that does not load a tz file, and then changes back to
1354 its original value, the last change is (incorrectly) ignored.
1355 Also, if TZ changes twice in succession to values that do
1356 not load a tz file, tzset can dump core (see Sun bug#1225179).
1357 The following code works around these bugs. */
1358
aafe5147
RS
1359 if (tzstring)
1360 {
1361 /* Temporarily set TZ to a value that loads a tz file
1362 and that differs from tzstring. */
1363 char *tz = *newenv;
1155c453
RS
1364 *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0
1365 ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1);
aafe5147
RS
1366 tzset ();
1367 *newenv = tz;
1368 }
1369 else
1370 {
1371 /* The implied tzstring is unknown, so temporarily set TZ to
1372 two different values that each load a tz file. */
1155c453 1373 *to = set_time_zone_rule_tz1;
aafe5147
RS
1374 to[1] = 0;
1375 tzset ();
1155c453 1376 *to = set_time_zone_rule_tz2;
aafe5147
RS
1377 tzset ();
1378 *to = 0;
1379 }
1380
1381 /* Now TZ has the desired value, and tzset can be invoked safely. */
1382 }
1383
143cb9a9
RS
1384 tzset ();
1385#endif
143cb9a9 1386}
35692fe0 1387\f
fb8106e8
KH
1388/* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC
1389 (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a
1390 type of object is Lisp_String). INHERIT is passed to
1391 INSERT_FROM_STRING_FUNC as the last argument. */
1392
d11ba98c 1393void
fb8106e8
KH
1394general_insert_function (insert_func, insert_from_string_func,
1395 inherit, nargs, args)
d11ba98c 1396 void (*insert_func) P_ ((unsigned char *, int));
1f24f4fd 1397 void (*insert_from_string_func) P_ ((Lisp_Object, int, int, int, int, int));
fb8106e8
KH
1398 int inherit, nargs;
1399 register Lisp_Object *args;
1400{
1401 register int argnum;
1402 register Lisp_Object val;
1403
1404 for (argnum = 0; argnum < nargs; argnum++)
1405 {
1406 val = args[argnum];
1407 retry:
1408 if (INTEGERP (val))
1409 {
d11ba98c 1410 unsigned char workbuf[4], *str;
fb8106e8
KH
1411 int len;
1412
1413 if (!NILP (current_buffer->enable_multibyte_characters))
1414 len = CHAR_STRING (XFASTINT (val), workbuf, str);
1415 else
13c148b8
KH
1416 {
1417 workbuf[0] = (SINGLE_BYTE_CHAR_P (XINT (val))
1418 ? XINT (val)
1419 : multibyte_char_to_unibyte (XINT (val), Qnil));
1420 str = workbuf;
1421 len = 1;
1422 }
fb8106e8
KH
1423 (*insert_func) (str, len);
1424 }
1425 else if (STRINGP (val))
1426 {
1f24f4fd
RS
1427 (*insert_from_string_func) (val, 0, 0,
1428 XSTRING (val)->size,
dc3620af 1429 STRING_BYTES (XSTRING (val)),
1f24f4fd 1430 inherit);
fb8106e8
KH
1431 }
1432 else
1433 {
1434 val = wrong_type_argument (Qchar_or_string_p, val);
1435 goto retry;
1436 }
1437 }
1438}
1439
35692fe0
JB
1440void
1441insert1 (arg)
1442 Lisp_Object arg;
1443{
1444 Finsert (1, &arg);
1445}
1446
52b14ac0
JB
1447
1448/* Callers passing one argument to Finsert need not gcpro the
1449 argument "array", since the only element of the array will
1450 not be used after calling insert or insert_from_string, so
1451 we don't care if it gets trashed. */
1452
35692fe0
JB
1453DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
1454 "Insert the arguments, either strings or characters, at point.\n\
24d6bf26 1455Point and before-insertion markers move forward to end up\n\
fb8106e8 1456 after the inserted text.\n\
24d6bf26
RS
1457Any other markers at the point of insertion remain before the text.\n\
1458\n\
1459If the current buffer is multibyte, unibyte strings are converted\n\
1460to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
13903932 1461If the current buffer is unibyte, multibyte strings are converted\n\
24d6bf26 1462to unibyte for insertion.")
35692fe0
JB
1463 (nargs, args)
1464 int nargs;
1465 register Lisp_Object *args;
1466{
fb8106e8 1467 general_insert_function (insert, insert_from_string, 0, nargs, args);
be91036a
RS
1468 return Qnil;
1469}
1470
1471DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit,
1472 0, MANY, 0,
1473 "Insert the arguments at point, inheriting properties from adjoining text.\n\
24d6bf26 1474Point and before-insertion markers move forward to end up\n\
fb8106e8 1475 after the inserted text.\n\
24d6bf26
RS
1476Any other markers at the point of insertion remain before the text.\n\
1477\n\
1478If the current buffer is multibyte, unibyte strings are converted\n\
1479to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
13903932 1480If the current buffer is unibyte, multibyte strings are converted\n\
24d6bf26 1481to unibyte for insertion.")
be91036a
RS
1482 (nargs, args)
1483 int nargs;
1484 register Lisp_Object *args;
1485{
fb8106e8
KH
1486 general_insert_function (insert_and_inherit, insert_from_string, 1,
1487 nargs, args);
35692fe0
JB
1488 return Qnil;
1489}
1490
1491DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
1492 "Insert strings or characters at point, relocating markers after the text.\n\
24d6bf26
RS
1493Point and markers move forward to end up after the inserted text.\n\
1494\n\
1495If the current buffer is multibyte, unibyte strings are converted\n\
1496to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
13903932 1497If the current buffer is unibyte, multibyte strings are converted\n\
24d6bf26 1498to unibyte for insertion.")
35692fe0
JB
1499 (nargs, args)
1500 int nargs;
1501 register Lisp_Object *args;
1502{
fb8106e8
KH
1503 general_insert_function (insert_before_markers,
1504 insert_from_string_before_markers, 0,
1505 nargs, args);
be91036a
RS
1506 return Qnil;
1507}
1508
a0d76c27
EN
1509DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers,
1510 Sinsert_and_inherit_before_markers, 0, MANY, 0,
be91036a 1511 "Insert text at point, relocating markers and inheriting properties.\n\
24d6bf26
RS
1512Point and markers move forward to end up after the inserted text.\n\
1513\n\
1514If the current buffer is multibyte, unibyte strings are converted\n\
1515to multibyte for insertion (see `unibyte-char-to-multibyte').\n\
13903932 1516If the current buffer is unibyte, multibyte strings are converted\n\
24d6bf26 1517to unibyte for insertion.")
be91036a
RS
1518 (nargs, args)
1519 int nargs;
1520 register Lisp_Object *args;
1521{
fb8106e8
KH
1522 general_insert_function (insert_before_markers_and_inherit,
1523 insert_from_string_before_markers, 1,
1524 nargs, args);
35692fe0
JB
1525 return Qnil;
1526}
1527\f
e2eeabbb 1528DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0,
2591ec64 1529 "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\
e2eeabbb 1530Both arguments are required.\n\
ba88b322 1531Point, and before-insertion markers, are relocated as in the function `insert'.\n\
e2eeabbb
RS
1532The optional third arg INHERIT, if non-nil, says to inherit text properties\n\
1533from adjoining text, if those properties are sticky.")
2591ec64
EN
1534 (character, count, inherit)
1535 Lisp_Object character, count, inherit;
35692fe0
JB
1536{
1537 register unsigned char *string;
1538 register int strlen;
1539 register int i, n;
fb8106e8
KH
1540 int len;
1541 unsigned char workbuf[4], *str;
35692fe0 1542
2591ec64 1543 CHECK_NUMBER (character, 0);
35692fe0
JB
1544 CHECK_NUMBER (count, 1);
1545
fb8106e8
KH
1546 if (!NILP (current_buffer->enable_multibyte_characters))
1547 len = CHAR_STRING (XFASTINT (character), workbuf, str);
1548 else
1549 workbuf[0] = XFASTINT (character), str = workbuf, len = 1;
1550 n = XINT (count) * len;
35692fe0
JB
1551 if (n <= 0)
1552 return Qnil;
fb8106e8 1553 strlen = min (n, 256 * len);
35692fe0
JB
1554 string = (unsigned char *) alloca (strlen);
1555 for (i = 0; i < strlen; i++)
fb8106e8 1556 string[i] = str[i % len];
35692fe0
JB
1557 while (n >= strlen)
1558 {
54e42e2d 1559 QUIT;
e2eeabbb
RS
1560 if (!NILP (inherit))
1561 insert_and_inherit (string, strlen);
1562 else
1563 insert (string, strlen);
35692fe0
JB
1564 n -= strlen;
1565 }
1566 if (n > 0)
83951f1e
KH
1567 {
1568 if (!NILP (inherit))
1569 insert_and_inherit (string, n);
1570 else
1571 insert (string, n);
1572 }
35692fe0
JB
1573 return Qnil;
1574}
1575
1576\f
ffd56f97
JB
1577/* Making strings from buffer contents. */
1578
1579/* Return a Lisp_String containing the text of the current buffer from
74d6d8c5 1580 START to END. If text properties are in use and the current buffer
eb8c3be9 1581 has properties in the range specified, the resulting string will also
260e2e2a 1582 have them, if PROPS is nonzero.
ffd56f97
JB
1583
1584 We don't want to use plain old make_string here, because it calls
1585 make_uninit_string, which can cause the buffer arena to be
1586 compacted. make_string has no way of knowing that the data has
1587 been moved, and thus copies the wrong data into the string. This
1588 doesn't effect most of the other users of make_string, so it should
1589 be left as is. But we should use this function when conjuring
1590 buffer substrings. */
74d6d8c5 1591
ffd56f97 1592Lisp_Object
260e2e2a 1593make_buffer_string (start, end, props)
ffd56f97 1594 int start, end;
260e2e2a 1595 int props;
ffd56f97 1596{
ec1c14f6
RS
1597 int start_byte = CHAR_TO_BYTE (start);
1598 int end_byte = CHAR_TO_BYTE (end);
ffd56f97 1599
88441c8e
RS
1600 return make_buffer_string_both (start, start_byte, end, end_byte, props);
1601}
1602
1603/* Return a Lisp_String containing the text of the current buffer from
1604 START / START_BYTE to END / END_BYTE.
1605
1606 If text properties are in use and the current buffer
1607 has properties in the range specified, the resulting string will also
1608 have them, if PROPS is nonzero.
1609
1610 We don't want to use plain old make_string here, because it calls
1611 make_uninit_string, which can cause the buffer arena to be
1612 compacted. make_string has no way of knowing that the data has
1613 been moved, and thus copies the wrong data into the string. This
1614 doesn't effect most of the other users of make_string, so it should
1615 be left as is. But we should use this function when conjuring
1616 buffer substrings. */
1617
1618Lisp_Object
1619make_buffer_string_both (start, start_byte, end, end_byte, props)
1620 int start, start_byte, end, end_byte;
1621 int props;
1622{
1623 Lisp_Object result, tem, tem1;
1624
5e6d5493
GM
1625 if (INTEGERP (current_buffer->minibuffer_prompt_length))
1626 {
1627 int len = XFASTINT (current_buffer->minibuffer_prompt_length);
1628 start = min (end, max (len, start));
1629 start_byte = CHAR_TO_BYTE (start);
1630 }
5e6d5493 1631
ffd56f97
JB
1632 if (start < GPT && GPT < end)
1633 move_gap (start);
1634
5f75e666
RS
1635 if (! NILP (current_buffer->enable_multibyte_characters))
1636 result = make_uninit_multibyte_string (end - start, end_byte - start_byte);
1637 else
1638 result = make_uninit_string (end - start);
ec1c14f6
RS
1639 bcopy (BYTE_POS_ADDR (start_byte), XSTRING (result)->data,
1640 end_byte - start_byte);
ffd56f97 1641
260e2e2a 1642 /* If desired, update and copy the text properties. */
60b96ee7 1643#ifdef USE_TEXT_PROPERTIES
260e2e2a
KH
1644 if (props)
1645 {
1646 update_buffer_properties (start, end);
1647
1648 tem = Fnext_property_change (make_number (start), Qnil, make_number (end));
1649 tem1 = Ftext_properties_at (make_number (start), Qnil);
1650
1651 if (XINT (tem) != end || !NILP (tem1))
ec1c14f6
RS
1652 copy_intervals_to_string (result, current_buffer, start,
1653 end - start);
260e2e2a 1654 }
60b96ee7 1655#endif
74d6d8c5 1656
ffd56f97
JB
1657 return result;
1658}
35692fe0 1659
260e2e2a
KH
1660/* Call Vbuffer_access_fontify_functions for the range START ... END
1661 in the current buffer, if necessary. */
1662
1663static void
1664update_buffer_properties (start, end)
1665 int start, end;
1666{
1667#ifdef USE_TEXT_PROPERTIES
1668 /* If this buffer has some access functions,
1669 call them, specifying the range of the buffer being accessed. */
1670 if (!NILP (Vbuffer_access_fontify_functions))
1671 {
1672 Lisp_Object args[3];
1673 Lisp_Object tem;
1674
1675 args[0] = Qbuffer_access_fontify_functions;
1676 XSETINT (args[1], start);
1677 XSETINT (args[2], end);
1678
1679 /* But don't call them if we can tell that the work
1680 has already been done. */
1681 if (!NILP (Vbuffer_access_fontified_property))
1682 {
1683 tem = Ftext_property_any (args[1], args[2],
1684 Vbuffer_access_fontified_property,
1685 Qnil, Qnil);
1686 if (! NILP (tem))
ced1d19a 1687 Frun_hook_with_args (3, args);
260e2e2a
KH
1688 }
1689 else
ced1d19a 1690 Frun_hook_with_args (3, args);
260e2e2a
KH
1691 }
1692#endif
1693}
1694
35692fe0
JB
1695DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
1696 "Return the contents of part of the current buffer as a string.\n\
1697The two arguments START and END are character positions;\n\
24d6bf26
RS
1698they can be in either order.\n\
1699The string returned is multibyte if the buffer is multibyte.")
2591ec64
EN
1700 (start, end)
1701 Lisp_Object start, end;
35692fe0 1702{
2591ec64 1703 register int b, e;
35692fe0 1704
2591ec64
EN
1705 validate_region (&start, &end);
1706 b = XINT (start);
1707 e = XINT (end);
35692fe0 1708
2591ec64 1709 return make_buffer_string (b, e, 1);
260e2e2a
KH
1710}
1711
1712DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties,
1713 Sbuffer_substring_no_properties, 2, 2, 0,
1714 "Return the characters of part of the buffer, without the text properties.\n\
1715The two arguments START and END are character positions;\n\
1716they can be in either order.")
2591ec64
EN
1717 (start, end)
1718 Lisp_Object start, end;
260e2e2a 1719{
2591ec64 1720 register int b, e;
260e2e2a 1721
2591ec64
EN
1722 validate_region (&start, &end);
1723 b = XINT (start);
1724 e = XINT (end);
260e2e2a 1725
2591ec64 1726 return make_buffer_string (b, e, 0);
35692fe0
JB
1727}
1728
1729DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
af7bd86c
KH
1730 "Return the contents of the current buffer as a string.\n\
1731If narrowing is in effect, this function returns only the visible part\n\
1732of the buffer.")
35692fe0
JB
1733 ()
1734{
260e2e2a 1735 return make_buffer_string (BEGV, ZV, 1);
35692fe0
JB
1736}
1737
1738DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
1739 1, 3, 0,
83ea6fc2 1740 "Insert before point a substring of the contents of buffer BUFFER.\n\
35692fe0
JB
1741BUFFER may be a buffer or a buffer name.\n\
1742Arguments START and END are character numbers specifying the substring.\n\
1743They default to the beginning and the end of BUFFER.")
2591ec64
EN
1744 (buf, start, end)
1745 Lisp_Object buf, start, end;
35692fe0 1746{
2591ec64 1747 register int b, e, temp;
260e2e2a 1748 register struct buffer *bp, *obuf;
3fff2dfa 1749 Lisp_Object buffer;
35692fe0 1750
3fff2dfa
RS
1751 buffer = Fget_buffer (buf);
1752 if (NILP (buffer))
1753 nsberror (buf);
1754 bp = XBUFFER (buffer);
93b62e82
KH
1755 if (NILP (bp->name))
1756 error ("Selecting deleted buffer");
35692fe0 1757
2591ec64
EN
1758 if (NILP (start))
1759 b = BUF_BEGV (bp);
35692fe0
JB
1760 else
1761 {
2591ec64
EN
1762 CHECK_NUMBER_COERCE_MARKER (start, 0);
1763 b = XINT (start);
35692fe0 1764 }
2591ec64
EN
1765 if (NILP (end))
1766 e = BUF_ZV (bp);
35692fe0
JB
1767 else
1768 {
2591ec64
EN
1769 CHECK_NUMBER_COERCE_MARKER (end, 1);
1770 e = XINT (end);
35692fe0
JB
1771 }
1772
2591ec64
EN
1773 if (b > e)
1774 temp = b, b = e, e = temp;
35692fe0 1775
2591ec64
EN
1776 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
1777 args_out_of_range (start, end);
35692fe0 1778
260e2e2a
KH
1779 obuf = current_buffer;
1780 set_buffer_internal_1 (bp);
2591ec64 1781 update_buffer_properties (b, e);
260e2e2a
KH
1782 set_buffer_internal_1 (obuf);
1783
2591ec64 1784 insert_from_buffer (bp, b, e - b, 0);
35692fe0
JB
1785 return Qnil;
1786}
e9cf2084
RS
1787
1788DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
1789 6, 6, 0,
1790 "Compare two substrings of two buffers; return result as number.\n\
1791the value is -N if first string is less after N-1 chars,\n\
1792+N if first string is greater after N-1 chars, or 0 if strings match.\n\
1793Each substring is represented as three arguments: BUFFER, START and END.\n\
1794That makes six args in all, three for each substring.\n\n\
1795The value of `case-fold-search' in the current buffer\n\
1796determines whether case is significant or ignored.")
1797 (buffer1, start1, end1, buffer2, start2, end2)
1798 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
1799{
07422a12 1800 register int begp1, endp1, begp2, endp2, temp;
e9cf2084 1801 register struct buffer *bp1, *bp2;
2a8b0ff0 1802 register Lisp_Object *trt
e9cf2084 1803 = (!NILP (current_buffer->case_fold_search)
2a8b0ff0 1804 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0);
ec1c14f6 1805 int chars = 0;
07422a12 1806 int i1, i2, i1_byte, i2_byte;
e9cf2084
RS
1807
1808 /* Find the first buffer and its substring. */
1809
1810 if (NILP (buffer1))
1811 bp1 = current_buffer;
1812 else
1813 {
3fff2dfa
RS
1814 Lisp_Object buf1;
1815 buf1 = Fget_buffer (buffer1);
1816 if (NILP (buf1))
1817 nsberror (buffer1);
1818 bp1 = XBUFFER (buf1);
93b62e82
KH
1819 if (NILP (bp1->name))
1820 error ("Selecting deleted buffer");
e9cf2084
RS
1821 }
1822
1823 if (NILP (start1))
1824 begp1 = BUF_BEGV (bp1);
1825 else
1826 {
1827 CHECK_NUMBER_COERCE_MARKER (start1, 1);
1828 begp1 = XINT (start1);
1829 }
1830 if (NILP (end1))
1831 endp1 = BUF_ZV (bp1);
1832 else
1833 {
1834 CHECK_NUMBER_COERCE_MARKER (end1, 2);
1835 endp1 = XINT (end1);
1836 }
1837
1838 if (begp1 > endp1)
1839 temp = begp1, begp1 = endp1, endp1 = temp;
1840
1841 if (!(BUF_BEGV (bp1) <= begp1
1842 && begp1 <= endp1
1843 && endp1 <= BUF_ZV (bp1)))
1844 args_out_of_range (start1, end1);
1845
1846 /* Likewise for second substring. */
1847
1848 if (NILP (buffer2))
1849 bp2 = current_buffer;
1850 else
1851 {
3fff2dfa
RS
1852 Lisp_Object buf2;
1853 buf2 = Fget_buffer (buffer2);
1854 if (NILP (buf2))
1855 nsberror (buffer2);
3b1fdd85 1856 bp2 = XBUFFER (buf2);
93b62e82
KH
1857 if (NILP (bp2->name))
1858 error ("Selecting deleted buffer");
e9cf2084
RS
1859 }
1860
1861 if (NILP (start2))
1862 begp2 = BUF_BEGV (bp2);
1863 else
1864 {
1865 CHECK_NUMBER_COERCE_MARKER (start2, 4);
1866 begp2 = XINT (start2);
1867 }
1868 if (NILP (end2))
1869 endp2 = BUF_ZV (bp2);
1870 else
1871 {
1872 CHECK_NUMBER_COERCE_MARKER (end2, 5);
1873 endp2 = XINT (end2);
1874 }
1875
1876 if (begp2 > endp2)
1877 temp = begp2, begp2 = endp2, endp2 = temp;
1878
1879 if (!(BUF_BEGV (bp2) <= begp2
1880 && begp2 <= endp2
1881 && endp2 <= BUF_ZV (bp2)))
1882 args_out_of_range (start2, end2);
1883
07422a12
RS
1884 i1 = begp1;
1885 i2 = begp2;
1886 i1_byte = buf_charpos_to_bytepos (bp1, i1);
1887 i2_byte = buf_charpos_to_bytepos (bp2, i2);
e9cf2084 1888
07422a12 1889 while (i1 < endp1 && i2 < endp2)
e9cf2084 1890 {
07422a12
RS
1891 /* When we find a mismatch, we must compare the
1892 characters, not just the bytes. */
1893 int c1, c2;
ec1c14f6 1894
07422a12
RS
1895 if (! NILP (bp1->enable_multibyte_characters))
1896 {
1897 c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte);
1898 BUF_INC_POS (bp1, i1_byte);
1899 i1++;
1900 }
1901 else
1902 {
1903 c1 = BUF_FETCH_BYTE (bp1, i1);
1904 c1 = unibyte_char_to_multibyte (c1);
1905 i1++;
1906 }
1907
1908 if (! NILP (bp2->enable_multibyte_characters))
1909 {
1910 c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte);
1911 BUF_INC_POS (bp2, i2_byte);
1912 i2++;
1913 }
1914 else
1915 {
1916 c2 = BUF_FETCH_BYTE (bp2, i2);
1917 c2 = unibyte_char_to_multibyte (c2);
1918 i2++;
1919 }
ec1c14f6 1920
e9cf2084
RS
1921 if (trt)
1922 {
1b10fb77
RS
1923 c1 = XINT (trt[c1]);
1924 c2 = XINT (trt[c2]);
e9cf2084
RS
1925 }
1926 if (c1 < c2)
ec1c14f6 1927 return make_number (- 1 - chars);
e9cf2084 1928 if (c1 > c2)
ec1c14f6 1929 return make_number (chars + 1);
07422a12
RS
1930
1931 chars++;
e9cf2084
RS
1932 }
1933
1934 /* The strings match as far as they go.
1935 If one is shorter, that one is less. */
07422a12 1936 if (chars < endp1 - begp1)
ec1c14f6 1937 return make_number (chars + 1);
07422a12 1938 else if (chars < endp2 - begp2)
ec1c14f6 1939 return make_number (- chars - 1);
e9cf2084
RS
1940
1941 /* Same length too => they are equal. */
1942 return make_number (0);
1943}
35692fe0 1944\f
d5a539cd
RS
1945static Lisp_Object
1946subst_char_in_region_unwind (arg)
1947 Lisp_Object arg;
1948{
1949 return current_buffer->undo_list = arg;
1950}
1951
c8e76b47
RS
1952static Lisp_Object
1953subst_char_in_region_unwind_1 (arg)
1954 Lisp_Object arg;
1955{
1956 return current_buffer->filename = arg;
1957}
1958
35692fe0
JB
1959DEFUN ("subst-char-in-region", Fsubst_char_in_region,
1960 Ssubst_char_in_region, 4, 5, 0,
1961 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
1962If optional arg NOUNDO is non-nil, don't record this change for undo\n\
fb8106e8
KH
1963and don't mark the buffer as really changed.\n\
1964Both characters must have the same length of multi-byte form.")
35692fe0
JB
1965 (start, end, fromchar, tochar, noundo)
1966 Lisp_Object start, end, fromchar, tochar, noundo;
1967{
84246b95 1968 register int pos, pos_byte, stop, i, len, end_byte;
60b96ee7 1969 int changed = 0;
fb8106e8 1970 unsigned char fromwork[4], *fromstr, towork[4], *tostr, *p;
d5a539cd 1971 int count = specpdl_ptr - specpdl;
aa801467
KH
1972#define COMBINING_NO 0
1973#define COMBINING_BEFORE 1
1974#define COMBINING_AFTER 2
1975#define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
1976 int maybe_byte_combining = COMBINING_NO;
35692fe0
JB
1977
1978 validate_region (&start, &end);
1979 CHECK_NUMBER (fromchar, 2);
1980 CHECK_NUMBER (tochar, 3);
1981
fb8106e8
KH
1982 if (! NILP (current_buffer->enable_multibyte_characters))
1983 {
1984 len = CHAR_STRING (XFASTINT (fromchar), fromwork, fromstr);
1985 if (CHAR_STRING (XFASTINT (tochar), towork, tostr) != len)
1986 error ("Characters in subst-char-in-region have different byte-lengths");
aa801467
KH
1987 if (!ASCII_BYTE_P (*tostr))
1988 {
1989 /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a
1990 complete multibyte character, it may be combined with the
1991 after bytes. If it is in the range 0xA0..0xFF, it may be
1992 combined with the before and after bytes. */
1993 if (!CHAR_HEAD_P (*tostr))
1994 maybe_byte_combining = COMBINING_BOTH;
1995 else if (BYTES_BY_CHAR_HEAD (*tostr) > len)
1996 maybe_byte_combining = COMBINING_AFTER;
1997 }
fb8106e8
KH
1998 }
1999 else
2000 {
2001 len = 1;
2002 fromwork[0] = XFASTINT (fromchar), fromstr = fromwork;
2003 towork[0] = XFASTINT (tochar), tostr = towork;
2004 }
2005
84246b95
KH
2006 pos = XINT (start);
2007 pos_byte = CHAR_TO_BYTE (pos);
ec1c14f6
RS
2008 stop = CHAR_TO_BYTE (XINT (end));
2009 end_byte = stop;
35692fe0 2010
d5a539cd
RS
2011 /* If we don't want undo, turn off putting stuff on the list.
2012 That's faster than getting rid of things,
c8e76b47
RS
2013 and it prevents even the entry for a first change.
2014 Also inhibit locking the file. */
d5a539cd
RS
2015 if (!NILP (noundo))
2016 {
2017 record_unwind_protect (subst_char_in_region_unwind,
2018 current_buffer->undo_list);
2019 current_buffer->undo_list = Qt;
c8e76b47
RS
2020 /* Don't do file-locking. */
2021 record_unwind_protect (subst_char_in_region_unwind_1,
2022 current_buffer->filename);
2023 current_buffer->filename = Qnil;
d5a539cd
RS
2024 }
2025
84246b95 2026 if (pos_byte < GPT_BYTE)
ec1c14f6 2027 stop = min (stop, GPT_BYTE);
fb8106e8 2028 while (1)
35692fe0 2029 {
a3360ff9
KH
2030 int pos_byte_next = pos_byte;
2031
84246b95 2032 if (pos_byte >= stop)
fb8106e8 2033 {
84246b95 2034 if (pos_byte >= end_byte) break;
ec1c14f6 2035 stop = end_byte;
fb8106e8 2036 }
84246b95 2037 p = BYTE_POS_ADDR (pos_byte);
a3360ff9
KH
2038 INC_POS (pos_byte_next);
2039 if (pos_byte_next - pos_byte == len
2040 && p[0] == fromstr[0]
fb8106e8
KH
2041 && (len == 1
2042 || (p[1] == fromstr[1]
2043 && (len == 2 || (p[2] == fromstr[2]
2044 && (len == 3 || p[3] == fromstr[3]))))))
35692fe0 2045 {
60b96ee7
RS
2046 if (! changed)
2047 {
fb8106e8 2048 modify_region (current_buffer, XINT (start), XINT (end));
7653d030
RS
2049
2050 if (! NILP (noundo))
2051 {
1e158d25
RS
2052 if (MODIFF - 1 == SAVE_MODIFF)
2053 SAVE_MODIFF++;
7653d030
RS
2054 if (MODIFF - 1 == current_buffer->auto_save_modified)
2055 current_buffer->auto_save_modified++;
2056 }
2057
fb8106e8 2058 changed = 1;
60b96ee7
RS
2059 }
2060
0c1e3b85
RS
2061 /* Take care of the case where the new character
2062 combines with neighboring bytes. */
a3360ff9 2063 if (maybe_byte_combining
aa801467
KH
2064 && (maybe_byte_combining == COMBINING_AFTER
2065 ? (pos_byte_next < Z_BYTE
2066 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2067 : ((pos_byte_next < Z_BYTE
2068 && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next)))
2069 || (pos_byte > BEG_BYTE
2070 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1))))))
0c1e3b85
RS
2071 {
2072 Lisp_Object tem, string;
2073
2074 struct gcpro gcpro1;
2075
2076 tem = current_buffer->undo_list;
2077 GCPRO1 (tem);
2078
aa801467
KH
2079 /* Make a multibyte string containing this single character. */
2080 string = make_multibyte_string (tostr, 1, len);
0c1e3b85
RS
2081 /* replace_range is less efficient, because it moves the gap,
2082 but it handles combining correctly. */
2083 replace_range (pos, pos + 1, string,
9869520f 2084 0, 0, 1);
a3360ff9
KH
2085 pos_byte_next = CHAR_TO_BYTE (pos);
2086 if (pos_byte_next > pos_byte)
2087 /* Before combining happened. We should not increment
3f5409d3
KH
2088 POS. So, to cancel the later increment of POS,
2089 decrease it now. */
2090 pos--;
a3360ff9 2091 else
3f5409d3 2092 INC_POS (pos_byte_next);
a3360ff9 2093
0c1e3b85
RS
2094 if (! NILP (noundo))
2095 current_buffer->undo_list = tem;
2096
2097 UNGCPRO;
2098 }
2099 else
2100 {
2101 if (NILP (noundo))
2102 record_change (pos, 1);
2103 for (i = 0; i < len; i++) *p++ = tostr[i];
2104 }
35692fe0 2105 }
3f5409d3
KH
2106 pos_byte = pos_byte_next;
2107 pos++;
35692fe0
JB
2108 }
2109
60b96ee7
RS
2110 if (changed)
2111 signal_after_change (XINT (start),
84246b95 2112 XINT (end) - XINT (start), XINT (end) - XINT (start));
60b96ee7 2113
d5a539cd 2114 unbind_to (count, Qnil);
35692fe0
JB
2115 return Qnil;
2116}
2117
2118DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
2119 "From START to END, translate characters according to TABLE.\n\
2120TABLE is a string; the Nth character in it is the mapping\n\
1f24f4fd
RS
2121for the character with code N.\n\
2122This function does not alter multibyte characters.\n\
2123It returns the number of characters changed.")
35692fe0
JB
2124 (start, end, table)
2125 Lisp_Object start;
2126 Lisp_Object end;
2127 register Lisp_Object table;
2128{
ec1c14f6 2129 register int pos_byte, stop; /* Limits of the region. */
35692fe0 2130 register unsigned char *tt; /* Trans table. */
35692fe0
JB
2131 register int nc; /* New character. */
2132 int cnt; /* Number of changes made. */
35692fe0 2133 int size; /* Size of translate table. */
1f24f4fd 2134 int pos;
35692fe0
JB
2135
2136 validate_region (&start, &end);
2137 CHECK_STRING (table, 2);
2138
dc3620af 2139 size = STRING_BYTES (XSTRING (table));
35692fe0
JB
2140 tt = XSTRING (table)->data;
2141
ec1c14f6
RS
2142 pos_byte = CHAR_TO_BYTE (XINT (start));
2143 stop = CHAR_TO_BYTE (XINT (end));
2144 modify_region (current_buffer, XINT (start), XINT (end));
1f24f4fd 2145 pos = XINT (start);
35692fe0
JB
2146
2147 cnt = 0;
1f24f4fd 2148 for (; pos_byte < stop; )
35692fe0 2149 {
ec1c14f6 2150 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
1f24f4fd
RS
2151 int len;
2152 int oc;
a3360ff9 2153 int pos_byte_next;
ec1c14f6 2154
1f24f4fd 2155 oc = STRING_CHAR_AND_LENGTH (p, stop - pos_byte, len);
a3360ff9 2156 pos_byte_next = pos_byte + len;
1f24f4fd 2157 if (oc < size && len == 1)
35692fe0
JB
2158 {
2159 nc = tt[oc];
2160 if (nc != oc)
2161 {
0c1e3b85
RS
2162 /* Take care of the case where the new character
2163 combines with neighboring bytes. */
a3360ff9
KH
2164 if (!ASCII_BYTE_P (nc)
2165 && (CHAR_HEAD_P (nc)
2166 ? ! CHAR_HEAD_P (FETCH_BYTE (pos_byte + 1))
48839d2e 2167 : (pos_byte > BEG_BYTE
a3360ff9 2168 && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1)))))
0c1e3b85
RS
2169 {
2170 Lisp_Object string;
2171
a3360ff9 2172 string = make_multibyte_string (tt + oc, 1, 1);
0c1e3b85
RS
2173 /* This is less efficient, because it moves the gap,
2174 but it handles combining correctly. */
2175 replace_range (pos, pos + 1, string,
a3360ff9
KH
2176 1, 0, 1);
2177 pos_byte_next = CHAR_TO_BYTE (pos);
2178 if (pos_byte_next > pos_byte)
2179 /* Before combining happened. We should not
3f5409d3
KH
2180 increment POS. So, to cancel the later
2181 increment of POS, we decrease it now. */
2182 pos--;
a3360ff9 2183 else
3f5409d3 2184 INC_POS (pos_byte_next);
0c1e3b85
RS
2185 }
2186 else
2187 {
2188 record_change (pos, 1);
2189 *p = nc;
2190 signal_after_change (pos, 1, 1);
2191 }
35692fe0
JB
2192 ++cnt;
2193 }
2194 }
3f5409d3
KH
2195 pos_byte = pos_byte_next;
2196 pos++;
35692fe0
JB
2197 }
2198
ec1c14f6 2199 return make_number (cnt);
35692fe0
JB
2200}
2201
2202DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
2203 "Delete the text between point and mark.\n\
2204When called from a program, expects two arguments,\n\
2205positions (integers or markers) specifying the stretch to be deleted.")
2591ec64
EN
2206 (start, end)
2207 Lisp_Object start, end;
35692fe0 2208{
2591ec64
EN
2209 validate_region (&start, &end);
2210 del_range (XINT (start), XINT (end));
35692fe0
JB
2211 return Qnil;
2212}
2213\f
2214DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
2215 "Remove restrictions (narrowing) from current buffer.\n\
2216This allows the buffer's full text to be seen and edited.")
2217 ()
2218{
2cad2e34
RS
2219 if (BEG != BEGV || Z != ZV)
2220 current_buffer->clip_changed = 1;
35692fe0 2221 BEGV = BEG;
ec1c14f6
RS
2222 BEGV_BYTE = BEG_BYTE;
2223 SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE);
52b14ac0
JB
2224 /* Changing the buffer bounds invalidates any recorded current column. */
2225 invalidate_current_column ();
35692fe0
JB
2226 return Qnil;
2227}
2228
2229DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
2230 "Restrict editing in this buffer to the current region.\n\
2231The rest of the text becomes temporarily invisible and untouchable\n\
2232but is not deleted; if you save the buffer in a file, the invisible\n\
2233text is included in the file. \\[widen] makes all visible again.\n\
2234See also `save-restriction'.\n\
2235\n\
2236When calling from a program, pass two arguments; positions (integers\n\
2237or markers) bounding the text that should remain visible.")
2591ec64
EN
2238 (start, end)
2239 register Lisp_Object start, end;
35692fe0 2240{
2591ec64
EN
2241 CHECK_NUMBER_COERCE_MARKER (start, 0);
2242 CHECK_NUMBER_COERCE_MARKER (end, 1);
35692fe0 2243
2591ec64 2244 if (XINT (start) > XINT (end))
35692fe0 2245 {
b5a6948e 2246 Lisp_Object tem;
2591ec64 2247 tem = start; start = end; end = tem;
35692fe0
JB
2248 }
2249
2591ec64
EN
2250 if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z))
2251 args_out_of_range (start, end);
35692fe0 2252
2cad2e34
RS
2253 if (BEGV != XFASTINT (start) || ZV != XFASTINT (end))
2254 current_buffer->clip_changed = 1;
2255
ec1c14f6 2256 SET_BUF_BEGV (current_buffer, XFASTINT (start));
2591ec64 2257 SET_BUF_ZV (current_buffer, XFASTINT (end));
6ec8bbd2 2258 if (PT < XFASTINT (start))
2591ec64 2259 SET_PT (XFASTINT (start));
6ec8bbd2 2260 if (PT > XFASTINT (end))
2591ec64 2261 SET_PT (XFASTINT (end));
52b14ac0
JB
2262 /* Changing the buffer bounds invalidates any recorded current column. */
2263 invalidate_current_column ();
35692fe0
JB
2264 return Qnil;
2265}
2266
2267Lisp_Object
2268save_restriction_save ()
2269{
2270 register Lisp_Object bottom, top;
2271 /* Note: I tried using markers here, but it does not win
2272 because insertion at the end of the saved region
2273 does not advance mh and is considered "outside" the saved region. */
55561c63
KH
2274 XSETFASTINT (bottom, BEGV - BEG);
2275 XSETFASTINT (top, Z - ZV);
35692fe0
JB
2276
2277 return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
2278}
2279
2280Lisp_Object
2281save_restriction_restore (data)
2282 Lisp_Object data;
2283{
2284 register struct buffer *buf;
2285 register int newhead, newtail;
2286 register Lisp_Object tem;
2cad2e34 2287 int obegv, ozv;
35692fe0
JB
2288
2289 buf = XBUFFER (XCONS (data)->car);
2290
2291 data = XCONS (data)->cdr;
2292
2293 tem = XCONS (data)->car;
2294 newhead = XINT (tem);
2295 tem = XCONS (data)->cdr;
2296 newtail = XINT (tem);
2297 if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
2298 {
2299 newhead = 0;
2300 newtail = 0;
2301 }
2cad2e34
RS
2302
2303 obegv = BUF_BEGV (buf);
2304 ozv = BUF_ZV (buf);
2305
ec1c14f6 2306 SET_BUF_BEGV (buf, BUF_BEG (buf) + newhead);
35692fe0 2307 SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
2cad2e34
RS
2308
2309 if (obegv != BUF_BEGV (buf) || ozv != BUF_ZV (buf))
2310 current_buffer->clip_changed = 1;
35692fe0
JB
2311
2312 /* If point is outside the new visible range, move it inside. */
ec1c14f6
RS
2313 SET_BUF_PT_BOTH (buf,
2314 clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)),
2315 clip_to_bounds (BUF_BEGV_BYTE (buf), BUF_PT_BYTE (buf),
2316 BUF_ZV_BYTE (buf)));
35692fe0
JB
2317
2318 return Qnil;
2319}
2320
2321DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
2322 "Execute BODY, saving and restoring current buffer's restrictions.\n\
2323The buffer's restrictions make parts of the beginning and end invisible.\n\
2324\(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
2325This special form, `save-restriction', saves the current buffer's restrictions\n\
2326when it is entered, and restores them when it is exited.\n\
2327So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
2328The old restrictions settings are restored\n\
2329even in case of abnormal exit (throw or error).\n\
2330\n\
2331The value returned is the value of the last form in BODY.\n\
2332\n\
2333`save-restriction' can get confused if, within the BODY, you widen\n\
2334and then make changes outside the area within the saved restrictions.\n\
5b2a5316 2335See Info node `(elisp)Narrowing' for details and an appropriate technique.\n\
35692fe0
JB
2336\n\
2337Note: if you are using both `save-excursion' and `save-restriction',\n\
2338use `save-excursion' outermost:\n\
2339 (save-excursion (save-restriction ...))")
2340 (body)
2341 Lisp_Object body;
2342{
2343 register Lisp_Object val;
2344 int count = specpdl_ptr - specpdl;
2345
2346 record_unwind_protect (save_restriction_restore, save_restriction_save ());
2347 val = Fprogn (body);
2348 return unbind_to (count, val);
2349}
2350\f
671fbc4d
KH
2351/* Buffer for the most recent text displayed by Fmessage. */
2352static char *message_text;
2353
2354/* Allocated length of that buffer. */
2355static int message_length;
2356
35692fe0
JB
2357DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
2358 "Print a one-line message at the bottom of the screen.\n\
98fc5c3c
RS
2359The first argument is a format control string, and the rest are data\n\
2360to be formatted under control of the string. See `format' for details.\n\
2361\n\
ccdac5be
JB
2362If the first argument is nil, clear any existing message; let the\n\
2363minibuffer contents show.")
35692fe0
JB
2364 (nargs, args)
2365 int nargs;
2366 Lisp_Object *args;
2367{
ccdac5be 2368 if (NILP (args[0]))
f0250249
JB
2369 {
2370 message (0);
2371 return Qnil;
2372 }
ccdac5be
JB
2373 else
2374 {
2375 register Lisp_Object val;
2376 val = Fformat (nargs, args);
5e6d5493 2377 message3 (val, STRING_BYTES (XSTRING (val)), STRING_MULTIBYTE (val));
ccdac5be
JB
2378 return val;
2379 }
35692fe0
JB
2380}
2381
cacc3e2c
RS
2382DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0,
2383 "Display a message, in a dialog box if possible.\n\
2384If a dialog box is not available, use the echo area.\n\
f8250f01
RS
2385The first argument is a format control string, and the rest are data\n\
2386to be formatted under control of the string. See `format' for details.\n\
2387\n\
cacc3e2c
RS
2388If the first argument is nil, clear any existing message; let the\n\
2389minibuffer contents show.")
2390 (nargs, args)
2391 int nargs;
2392 Lisp_Object *args;
2393{
2394 if (NILP (args[0]))
2395 {
2396 message (0);
2397 return Qnil;
2398 }
2399 else
2400 {
2401 register Lisp_Object val;
2402 val = Fformat (nargs, args);
f8250f01 2403#ifdef HAVE_MENUS
cacc3e2c
RS
2404 {
2405 Lisp_Object pane, menu, obj;
2406 struct gcpro gcpro1;
2407 pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil);
2408 GCPRO1 (pane);
2409 menu = Fcons (val, pane);
2410 obj = Fx_popup_dialog (Qt, menu);
2411 UNGCPRO;
2412 return val;
2413 }
f8250f01 2414#else /* not HAVE_MENUS */
cacc3e2c
RS
2415 /* Copy the data so that it won't move when we GC. */
2416 if (! message_text)
2417 {
2418 message_text = (char *)xmalloc (80);
2419 message_length = 80;
2420 }
dc3620af 2421 if (STRING_BYTES (XSTRING (val)) > message_length)
cacc3e2c 2422 {
dc3620af 2423 message_length = STRING_BYTES (XSTRING (val));
cacc3e2c
RS
2424 message_text = (char *)xrealloc (message_text, message_length);
2425 }
dc3620af 2426 bcopy (XSTRING (val)->data, message_text, STRING_BYTES (XSTRING (val)));
d13a8480
RS
2427 message2 (message_text, STRING_BYTES (XSTRING (val)),
2428 STRING_MULTIBYTE (val));
cacc3e2c 2429 return val;
f8250f01 2430#endif /* not HAVE_MENUS */
cacc3e2c
RS
2431 }
2432}
f8250f01 2433#ifdef HAVE_MENUS
cacc3e2c
RS
2434extern Lisp_Object last_nonmenu_event;
2435#endif
f8250f01 2436
cacc3e2c
RS
2437DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0,
2438 "Display a message in a dialog box or in the echo area.\n\
2439If this command was invoked with the mouse, use a dialog box.\n\
2440Otherwise, use the echo area.\n\
f8250f01
RS
2441The first argument is a format control string, and the rest are data\n\
2442to be formatted under control of the string. See `format' for details.\n\
cacc3e2c 2443\n\
cacc3e2c
RS
2444If the first argument is nil, clear any existing message; let the\n\
2445minibuffer contents show.")
2446 (nargs, args)
2447 int nargs;
2448 Lisp_Object *args;
2449{
f8250f01 2450#ifdef HAVE_MENUS
cacc3e2c 2451 if (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
0a56ee6b 2452 return Fmessage_box (nargs, args);
cacc3e2c
RS
2453#endif
2454 return Fmessage (nargs, args);
2455}
2456
b14dda8a
RS
2457DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0,
2458 "Return the string currently displayed in the echo area, or nil if none.")
2459 ()
2460{
0634a78e 2461 return current_message ();
b14dda8a
RS
2462}
2463
1f24f4fd
RS
2464/* Number of bytes that STRING will occupy when put into the result.
2465 MULTIBYTE is nonzero if the result should be multibyte. */
2466
2467#define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \
2468 (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \
8d6179dc 2469 ? count_size_as_multibyte (XSTRING (STRING)->data, \
dc3620af
RS
2470 STRING_BYTES (XSTRING (STRING))) \
2471 : STRING_BYTES (XSTRING (STRING)))
1f24f4fd 2472
35692fe0
JB
2473DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
2474 "Format a string out of a control-string and arguments.\n\
2475The first argument is a control string.\n\
2476The other arguments are substituted into it to make the result, a string.\n\
2477It may contain %-sequences meaning to substitute the next argument.\n\
2478%s means print a string argument. Actually, prints any object, with `princ'.\n\
2479%d means print as number in decimal (%o octal, %x hex).\n\
9db1775a
RS
2480%e means print a number in exponential notation.\n\
2481%f means print a number in decimal-point notation.\n\
2482%g means print a number in exponential notation\n\
2483 or decimal-point notation, whichever uses fewer characters.\n\
35692fe0 2484%c means print a number as a single character.\n\
d6c0ea99 2485%S means print any object as an s-expression (using `prin1').\n\
9db1775a 2486 The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\
52b14ac0 2487Use %% to put a single % into the output.")
35692fe0
JB
2488 (nargs, args)
2489 int nargs;
2490 register Lisp_Object *args;
2491{
2492 register int n; /* The number of the next arg to substitute */
e781c49e 2493 register int total; /* An estimate of the final length */
1f24f4fd 2494 char *buf, *p;
35692fe0 2495 register unsigned char *format, *end;
1f24f4fd
RS
2496 int length, nchars;
2497 /* Nonzero if the output should be a multibyte string,
2498 which is true if any of the inputs is one. */
2499 int multibyte = 0;
8f2917e4
KH
2500 /* When we make a multibyte string, we must pay attention to the
2501 byte combining problem, i.e., a byte may be combined with a
2502 multibyte charcter of the previous string. This flag tells if we
2503 must consider such a situation or not. */
2504 int maybe_combine_byte;
1f24f4fd 2505 unsigned char *this_format;
e781c49e 2506 int longest_format;
8d6179dc 2507 Lisp_Object val;
5e6d5493
GM
2508 struct info
2509 {
2510 int start, end;
2511 } *info = 0;
1f24f4fd 2512
35692fe0 2513 extern char *index ();
1f24f4fd 2514
35692fe0
JB
2515 /* It should not be necessary to GCPRO ARGS, because
2516 the caller in the interpreter should take care of that. */
2517
e781c49e
RS
2518 /* Try to determine whether the result should be multibyte.
2519 This is not always right; sometimes the result needs to be multibyte
2520 because of an object that we will pass through prin1,
2521 and in that case, we won't know it here. */
1f24f4fd
RS
2522 for (n = 0; n < nargs; n++)
2523 if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n]))
2524 multibyte = 1;
2525
35692fe0 2526 CHECK_STRING (args[0], 0);
e781c49e
RS
2527
2528 /* If we start out planning a unibyte result,
2529 and later find it has to be multibyte, we jump back to retry. */
2530 retry:
2531
35692fe0 2532 format = XSTRING (args[0])->data;
dc3620af 2533 end = format + STRING_BYTES (XSTRING (args[0]));
e781c49e 2534 longest_format = 0;
1f24f4fd
RS
2535
2536 /* Make room in result for all the non-%-codes in the control string. */
e781c49e 2537 total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]);
1f24f4fd
RS
2538
2539 /* Add to TOTAL enough space to hold the converted arguments. */
35692fe0
JB
2540
2541 n = 0;
2542 while (format != end)
2543 if (*format++ == '%')
2544 {
1f24f4fd
RS
2545 int minlen, thissize = 0;
2546 unsigned char *this_format_start = format - 1;
35692fe0
JB
2547
2548 /* Process a numeric arg and skip it. */
2549 minlen = atoi (format);
537dfb13
RS
2550 if (minlen < 0)
2551 minlen = - minlen;
2552
35692fe0
JB
2553 while ((*format >= '0' && *format <= '9')
2554 || *format == '-' || *format == ' ' || *format == '.')
2555 format++;
2556
1f24f4fd
RS
2557 if (format - this_format_start + 1 > longest_format)
2558 longest_format = format - this_format_start + 1;
2559
bf6ab66c
KH
2560 if (format == end)
2561 error ("Format string ends in middle of format specifier");
35692fe0
JB
2562 if (*format == '%')
2563 format++;
2564 else if (++n >= nargs)
537dfb13 2565 error ("Not enough arguments for format string");
35692fe0
JB
2566 else if (*format == 'S')
2567 {
2568 /* For `S', prin1 the argument and then treat like a string. */
2569 register Lisp_Object tem;
2570 tem = Fprin1_to_string (args[n], Qnil);
e781c49e
RS
2571 if (STRING_MULTIBYTE (tem) && ! multibyte)
2572 {
2573 multibyte = 1;
2574 goto retry;
2575 }
35692fe0
JB
2576 args[n] = tem;
2577 goto string;
2578 }
ae683129 2579 else if (SYMBOLP (args[n]))
35692fe0 2580 {
d2fd0445 2581 XSETSTRING (args[n], XSYMBOL (args[n])->name);
7df74da6
RS
2582 if (STRING_MULTIBYTE (args[n]) && ! multibyte)
2583 {
2584 multibyte = 1;
2585 goto retry;
2586 }
35692fe0
JB
2587 goto string;
2588 }
ae683129 2589 else if (STRINGP (args[n]))
35692fe0
JB
2590 {
2591 string:
b22e7ecc 2592 if (*format != 's' && *format != 'S')
bf6ab66c 2593 error ("Format specifier doesn't match argument type");
1f24f4fd 2594 thissize = CONVERTED_BYTE_SIZE (multibyte, args[n]);
35692fe0
JB
2595 }
2596 /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */
ae683129 2597 else if (INTEGERP (args[n]) && *format != 's')
35692fe0 2598 {
4746118a 2599#ifdef LISP_FLOAT_TYPE
eb8c3be9 2600 /* The following loop assumes the Lisp type indicates
35692fe0
JB
2601 the proper way to pass the argument.
2602 So make sure we have a flonum if the argument should
2603 be a double. */
2604 if (*format == 'e' || *format == 'f' || *format == 'g')
2605 args[n] = Ffloat (args[n]);
4224cb62 2606 else
4746118a 2607#endif
4224cb62 2608 if (*format != 'd' && *format != 'o' && *format != 'x'
00d65216 2609 && *format != 'i' && *format != 'X' && *format != 'c')
4224cb62
KH
2610 error ("Invalid format operation %%%c", *format);
2611
f49a2d74 2612 thissize = 30;
25c9e7fb
RS
2613 if (*format == 'c'
2614 && (! SINGLE_BYTE_CHAR_P (XINT (args[n]))
2615 || XINT (args[n]) == 0))
f49a2d74
KH
2616 {
2617 if (! multibyte)
2618 {
2619 multibyte = 1;
2620 goto retry;
2621 }
2622 args[n] = Fchar_to_string (args[n]);
dc3620af 2623 thissize = STRING_BYTES (XSTRING (args[n]));
f49a2d74 2624 }
35692fe0 2625 }
4746118a 2626#ifdef LISP_FLOAT_TYPE
ae683129 2627 else if (FLOATP (args[n]) && *format != 's')
35692fe0
JB
2628 {
2629 if (! (*format == 'e' || *format == 'f' || *format == 'g'))
247422ce 2630 args[n] = Ftruncate (args[n], Qnil);
b413f895 2631 thissize = 200;
35692fe0 2632 }
4746118a 2633#endif
35692fe0
JB
2634 else
2635 {
2636 /* Anything but a string, convert to a string using princ. */
2637 register Lisp_Object tem;
2638 tem = Fprin1_to_string (args[n], Qt);
a4e91443 2639 if (STRING_MULTIBYTE (tem) & ! multibyte)
e781c49e
RS
2640 {
2641 multibyte = 1;
2642 goto retry;
2643 }
35692fe0
JB
2644 args[n] = tem;
2645 goto string;
2646 }
1f24f4fd
RS
2647
2648 if (thissize < minlen)
2649 thissize = minlen;
2650
2651 total += thissize + 4;
35692fe0
JB
2652 }
2653
e781c49e
RS
2654 /* Now we can no longer jump to retry.
2655 TOTAL and LONGEST_FORMAT are known for certain. */
2656
1f24f4fd 2657 this_format = (unsigned char *) alloca (longest_format + 1);
50aa2f90 2658
1f24f4fd
RS
2659 /* Allocate the space for the result.
2660 Note that TOTAL is an overestimate. */
2661 if (total < 1000)
3c6db9d5 2662 buf = (char *) alloca (total + 1);
1f24f4fd 2663 else
3c6db9d5 2664 buf = (char *) xmalloc (total + 1);
35692fe0 2665
1f24f4fd
RS
2666 p = buf;
2667 nchars = 0;
2668 n = 0;
35692fe0 2669
1f24f4fd
RS
2670 /* Scan the format and store result in BUF. */
2671 format = XSTRING (args[0])->data;
8f2917e4 2672 maybe_combine_byte = 0;
1f24f4fd
RS
2673 while (format != end)
2674 {
2675 if (*format == '%')
2676 {
2677 int minlen;
25c9e7fb 2678 int negative = 0;
1f24f4fd 2679 unsigned char *this_format_start = format;
35692fe0 2680
1f24f4fd 2681 format++;
fb893977 2682
1f24f4fd
RS
2683 /* Process a numeric arg and skip it. */
2684 minlen = atoi (format);
2685 if (minlen < 0)
25c9e7fb 2686 minlen = - minlen, negative = 1;
35692fe0 2687
1f24f4fd
RS
2688 while ((*format >= '0' && *format <= '9')
2689 || *format == '-' || *format == ' ' || *format == '.')
2690 format++;
35692fe0 2691
1f24f4fd
RS
2692 if (*format++ == '%')
2693 {
2694 *p++ = '%';
2695 nchars++;
2696 continue;
2697 }
2698
2699 ++n;
2700
2701 if (STRINGP (args[n]))
2702 {
25c9e7fb
RS
2703 int padding, nbytes;
2704 int width = strwidth (XSTRING (args[n])->data,
dc3620af 2705 STRING_BYTES (XSTRING (args[n])));
5e6d5493 2706 int start = nchars;
25c9e7fb
RS
2707
2708 /* If spec requires it, pad on right with spaces. */
2709 padding = minlen - width;
2710 if (! negative)
2711 while (padding-- > 0)
2712 {
2713 *p++ = ' ';
2714 nchars++;
2715 }
1f24f4fd 2716
8f2917e4
KH
2717 if (p > buf
2718 && multibyte
25aa5d64 2719 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
8f2917e4 2720 && STRING_MULTIBYTE (args[n])
25aa5d64 2721 && !CHAR_HEAD_P (XSTRING (args[n])->data[0]))
8f2917e4 2722 maybe_combine_byte = 1;
1f24f4fd 2723 nbytes = copy_text (XSTRING (args[n])->data, p,
dc3620af 2724 STRING_BYTES (XSTRING (args[n])),
1f24f4fd
RS
2725 STRING_MULTIBYTE (args[n]), multibyte);
2726 p += nbytes;
2727 nchars += XSTRING (args[n])->size;
2728
25c9e7fb
RS
2729 if (negative)
2730 while (padding-- > 0)
2731 {
2732 *p++ = ' ';
2733 nchars++;
2734 }
5e6d5493
GM
2735
2736 /* If this argument has text properties, record where
2737 in the result string it appears. */
2738 if (XSTRING (args[n])->intervals)
2739 {
2740 if (!info)
2741 {
2742 int nbytes = nargs * sizeof *info;
2743 info = (struct info *) alloca (nbytes);
2744 bzero (info, nbytes);
2745 }
2746
2747 info[n].start = start;
2748 info[n].end = nchars;
2749 }
1f24f4fd
RS
2750 }
2751 else if (INTEGERP (args[n]) || FLOATP (args[n]))
2752 {
2753 int this_nchars;
2754
2755 bcopy (this_format_start, this_format,
2756 format - this_format_start);
2757 this_format[format - this_format_start] = 0;
2758
d0183d38
RS
2759 if (INTEGERP (args[n]))
2760 sprintf (p, this_format, XINT (args[n]));
2761 else
2762 sprintf (p, this_format, XFLOAT (args[n])->data);
1f24f4fd 2763
8f2917e4
KH
2764 if (p > buf
2765 && multibyte
25aa5d64
KH
2766 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
2767 && !CHAR_HEAD_P (*((unsigned char *) p)))
8f2917e4 2768 maybe_combine_byte = 1;
1f24f4fd
RS
2769 this_nchars = strlen (p);
2770 p += this_nchars;
2771 nchars += this_nchars;
2772 }
2773 }
7df74da6
RS
2774 else if (STRING_MULTIBYTE (args[0]))
2775 {
2776 /* Copy a whole multibyte character. */
8f2917e4
KH
2777 if (p > buf
2778 && multibyte
25aa5d64
KH
2779 && !ASCII_BYTE_P (*((unsigned char *) p - 1))
2780 && !CHAR_HEAD_P (*format))
8f2917e4 2781 maybe_combine_byte = 1;
7df74da6
RS
2782 *p++ = *format++;
2783 while (! CHAR_HEAD_P (*format)) *p++ = *format++;
2784 nchars++;
2785 }
2786 else if (multibyte)
1f24f4fd
RS
2787 {
2788 /* Convert a single-byte character to multibyte. */
2789 int len = copy_text (format, p, 1, 0, 1);
2790
2791 p += len;
2792 format++;
2793 nchars++;
2794 }
2795 else
2796 *p++ = *format++, nchars++;
2797 }
2798
8f2917e4
KH
2799 if (maybe_combine_byte)
2800 nchars = multibyte_chars_in_text (buf, p - buf);
5f75e666 2801 val = make_specified_string (buf, nchars, p - buf, multibyte);
8d6179dc 2802
1f24f4fd
RS
2803 /* If we allocated BUF with malloc, free it too. */
2804 if (total >= 1000)
2805 xfree (buf);
35692fe0 2806
5e6d5493
GM
2807 /* If the format string has text properties, or any of the string
2808 arguments has text properties, set up text properties of the
2809 result string. */
2810
2811 if (XSTRING (args[0])->intervals || info)
2812 {
2813 Lisp_Object len, new_len, props;
2814 struct gcpro gcpro1;
2815
2816 /* Add text properties from the format string. */
2817 len = make_number (XSTRING (args[0])->size);
2818 props = text_property_list (args[0], make_number (0), len, Qnil);
2819 GCPRO1 (props);
2820
2821 if (CONSP (props))
2822 {
2823 new_len = make_number (XSTRING (val)->size);
2824 extend_property_ranges (props, len, new_len);
2825 add_text_properties_from_list (val, props, make_number (0));
2826 }
2827
2828 /* Add text properties from arguments. */
2829 if (info)
2830 for (n = 1; n < nargs; ++n)
2831 if (info[n].end)
2832 {
2833 len = make_number (XSTRING (args[n])->size);
2834 new_len = make_number (info[n].end - info[n].start);
2835 props = text_property_list (args[n], make_number (0), len, Qnil);
2836 extend_property_ranges (props, len, new_len);
2837 add_text_properties_from_list (val, props,
2838 make_number (info[n].start));
2839 }
2840
2841 UNGCPRO;
2842 }
2843
8d6179dc 2844 return val;
35692fe0
JB
2845}
2846
2847/* VARARGS 1 */
2848Lisp_Object
2849#ifdef NO_ARG_ARRAY
2850format1 (string1, arg0, arg1, arg2, arg3, arg4)
679e18b1 2851 EMACS_INT arg0, arg1, arg2, arg3, arg4;
35692fe0
JB
2852#else
2853format1 (string1)
2854#endif
2855 char *string1;
2856{
2857 char buf[100];
2858#ifdef NO_ARG_ARRAY
679e18b1 2859 EMACS_INT args[5];
35692fe0
JB
2860 args[0] = arg0;
2861 args[1] = arg1;
2862 args[2] = arg2;
2863 args[3] = arg3;
2864 args[4] = arg4;
e3670faa 2865 doprnt (buf, sizeof buf, string1, (char *)0, 5, (char **) args);
35692fe0 2866#else
ea4d2909 2867 doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1);
35692fe0
JB
2868#endif
2869 return build_string (buf);
2870}
2871\f
2872DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
2873 "Return t if two characters match, optionally ignoring case.\n\
2874Both arguments must be characters (i.e. integers).\n\
2875Case is ignored if `case-fold-search' is non-nil in the current buffer.")
2876 (c1, c2)
2877 register Lisp_Object c1, c2;
2878{
1b5d98bb 2879 int i1, i2;
35692fe0
JB
2880 CHECK_NUMBER (c1, 0);
2881 CHECK_NUMBER (c2, 1);
2882
1b5d98bb 2883 if (XINT (c1) == XINT (c2))
35692fe0 2884 return Qt;
1b5d98bb
RS
2885 if (NILP (current_buffer->case_fold_search))
2886 return Qnil;
2887
2888 /* Do these in separate statements,
2889 then compare the variables.
2890 because of the way DOWNCASE uses temp variables. */
2891 i1 = DOWNCASE (XFASTINT (c1));
2892 i2 = DOWNCASE (XFASTINT (c2));
2893 return (i1 == i2 ? Qt : Qnil);
35692fe0 2894}
b229b8d1
RS
2895\f
2896/* Transpose the markers in two regions of the current buffer, and
2897 adjust the ones between them if necessary (i.e.: if the regions
2898 differ in size).
2899
ec1c14f6
RS
2900 START1, END1 are the character positions of the first region.
2901 START1_BYTE, END1_BYTE are the byte positions.
2902 START2, END2 are the character positions of the second region.
2903 START2_BYTE, END2_BYTE are the byte positions.
2904
b229b8d1
RS
2905 Traverses the entire marker list of the buffer to do so, adding an
2906 appropriate amount to some, subtracting from some, and leaving the
2907 rest untouched. Most of this is copied from adjust_markers in insdel.c.
2908
ec1c14f6 2909 It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */
b229b8d1
RS
2910
2911void
ec1c14f6
RS
2912transpose_markers (start1, end1, start2, end2,
2913 start1_byte, end1_byte, start2_byte, end2_byte)
b229b8d1 2914 register int start1, end1, start2, end2;
ec1c14f6 2915 register int start1_byte, end1_byte, start2_byte, end2_byte;
b229b8d1 2916{
ec1c14f6 2917 register int amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
b229b8d1 2918 register Lisp_Object marker;
b229b8d1 2919
03240d11 2920 /* Update point as if it were a marker. */
8de1d5f0
KH
2921 if (PT < start1)
2922 ;
2923 else if (PT < end1)
ec1c14f6
RS
2924 TEMP_SET_PT_BOTH (PT + (end2 - end1),
2925 PT_BYTE + (end2_byte - end1_byte));
8de1d5f0 2926 else if (PT < start2)
ec1c14f6
RS
2927 TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1),
2928 (PT_BYTE + (end2_byte - start2_byte)
2929 - (end1_byte - start1_byte)));
8de1d5f0 2930 else if (PT < end2)
ec1c14f6
RS
2931 TEMP_SET_PT_BOTH (PT - (start2 - start1),
2932 PT_BYTE - (start2_byte - start1_byte));
8de1d5f0 2933
03240d11
KH
2934 /* We used to adjust the endpoints here to account for the gap, but that
2935 isn't good enough. Even if we assume the caller has tried to move the
2936 gap out of our way, it might still be at start1 exactly, for example;
2937 and that places it `inside' the interval, for our purposes. The amount
2938 of adjustment is nontrivial if there's a `denormalized' marker whose
2939 position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
2940 the dirty work to Fmarker_position, below. */
b229b8d1
RS
2941
2942 /* The difference between the region's lengths */
2943 diff = (end2 - start2) - (end1 - start1);
ec1c14f6 2944 diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte);
b229b8d1
RS
2945
2946 /* For shifting each marker in a region by the length of the other
ec1c14f6 2947 region plus the distance between the regions. */
b229b8d1
RS
2948 amt1 = (end2 - start2) + (start2 - end1);
2949 amt2 = (end1 - start1) + (start2 - end1);
ec1c14f6
RS
2950 amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte);
2951 amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte);
b229b8d1 2952
1e158d25 2953 for (marker = BUF_MARKERS (current_buffer); !NILP (marker);
03240d11 2954 marker = XMARKER (marker)->chain)
b229b8d1 2955 {
ec1c14f6
RS
2956 mpos = marker_byte_position (marker);
2957 if (mpos >= start1_byte && mpos < end2_byte)
2958 {
2959 if (mpos < end1_byte)
2960 mpos += amt1_byte;
2961 else if (mpos < start2_byte)
2962 mpos += diff_byte;
2963 else
2964 mpos -= amt2_byte;
f3e1f752 2965 XMARKER (marker)->bytepos = mpos;
ec1c14f6
RS
2966 }
2967 mpos = XMARKER (marker)->charpos;
03240d11
KH
2968 if (mpos >= start1 && mpos < end2)
2969 {
2970 if (mpos < end1)
2971 mpos += amt1;
2972 else if (mpos < start2)
2973 mpos += diff;
2974 else
2975 mpos -= amt2;
03240d11 2976 }
ec1c14f6 2977 XMARKER (marker)->charpos = mpos;
b229b8d1
RS
2978 }
2979}
2980
2981DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0,
2982 "Transpose region START1 to END1 with START2 to END2.\n\
2983The regions may not be overlapping, because the size of the buffer is\n\
2984never changed in a transposition.\n\
2985\n\
ec1c14f6 2986Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update\n\
b229b8d1
RS
2987any markers that happen to be located in the regions.\n\
2988\n\
2989Transposing beyond buffer boundaries is an error.")
2990 (startr1, endr1, startr2, endr2, leave_markers)
2991 Lisp_Object startr1, endr1, startr2, endr2, leave_markers;
2992{
ec1c14f6
RS
2993 register int start1, end1, start2, end2;
2994 int start1_byte, start2_byte, len1_byte, len2_byte;
2995 int gap, len1, len_mid, len2;
3c6bc7d0 2996 unsigned char *start1_addr, *start2_addr, *temp;
dc3620af
RS
2997 int combined_before_bytes_1, combined_after_bytes_1;
2998 int combined_before_bytes_2, combined_after_bytes_2;
2999 struct gcpro gcpro1, gcpro2;
b229b8d1
RS
3000
3001#ifdef USE_TEXT_PROPERTIES
3002 INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2;
1e158d25 3003 cur_intv = BUF_INTERVALS (current_buffer);
b229b8d1
RS
3004#endif /* USE_TEXT_PROPERTIES */
3005
3006 validate_region (&startr1, &endr1);
3007 validate_region (&startr2, &endr2);
3008
3009 start1 = XFASTINT (startr1);
3010 end1 = XFASTINT (endr1);
3011 start2 = XFASTINT (startr2);
3012 end2 = XFASTINT (endr2);
3013 gap = GPT;
3014
3015 /* Swap the regions if they're reversed. */
3016 if (start2 < end1)
3017 {
3018 register int glumph = start1;
3019 start1 = start2;
3020 start2 = glumph;
3021 glumph = end1;
3022 end1 = end2;
3023 end2 = glumph;
3024 }
3025
b229b8d1
RS
3026 len1 = end1 - start1;
3027 len2 = end2 - start2;
3028
3029 if (start2 < end1)
dc3620af 3030 error ("Transposed regions overlap");
b229b8d1 3031 else if (start1 == end1 || start2 == end2)
dc3620af 3032 error ("Transposed region has length 0");
b229b8d1
RS
3033
3034 /* The possibilities are:
3035 1. Adjacent (contiguous) regions, or separate but equal regions
3036 (no, really equal, in this case!), or
3037 2. Separate regions of unequal size.
3038
3039 The worst case is usually No. 2. It means that (aside from
3040 potential need for getting the gap out of the way), there also
3041 needs to be a shifting of the text between the two regions. So
3042 if they are spread far apart, we are that much slower... sigh. */
3043
3044 /* It must be pointed out that the really studly thing to do would
3045 be not to move the gap at all, but to leave it in place and work
3046 around it if necessary. This would be extremely efficient,
3047 especially considering that people are likely to do
3048 transpositions near where they are working interactively, which
3049 is exactly where the gap would be found. However, such code
3050 would be much harder to write and to read. So, if you are
3051 reading this comment and are feeling squirrely, by all means have
3052 a go! I just didn't feel like doing it, so I will simply move
3053 the gap the minimum distance to get it out of the way, and then
3054 deal with an unbroken array. */
3c6bc7d0
RS
3055
3056 /* Make sure the gap won't interfere, by moving it out of the text
3057 we will operate on. */
3058 if (start1 < gap && gap < end2)
3059 {
3060 if (gap - start1 < end2 - gap)
3061 move_gap (start1);
3062 else
3063 move_gap (end2);
3064 }
ec1c14f6
RS
3065
3066 start1_byte = CHAR_TO_BYTE (start1);
3067 start2_byte = CHAR_TO_BYTE (start2);
3068 len1_byte = CHAR_TO_BYTE (end1) - start1_byte;
3069 len2_byte = CHAR_TO_BYTE (end2) - start2_byte;
dc3620af
RS
3070
3071 if (end1 == start2)
3072 {
3073 combined_before_bytes_2
3074 = count_combining_before (BYTE_POS_ADDR (start2_byte),
3075 len2_byte, start1, start1_byte);
3076 combined_before_bytes_1
3077 = count_combining_before (BYTE_POS_ADDR (start1_byte),
3078 len1_byte, end2, start2_byte + len2_byte);
3079 combined_after_bytes_1
3080 = count_combining_after (BYTE_POS_ADDR (start1_byte),
3081 len1_byte, end2, start2_byte + len2_byte);
3082 combined_after_bytes_2 = 0;
3083 }
3084 else
3085 {
3086 combined_before_bytes_2
3087 = count_combining_before (BYTE_POS_ADDR (start2_byte),
3088 len2_byte, start1, start1_byte);
3089 combined_before_bytes_1
3090 = count_combining_before (BYTE_POS_ADDR (start1_byte),
3091 len1_byte, start2, start2_byte);
3092 combined_after_bytes_2
3093 = count_combining_after (BYTE_POS_ADDR (start2_byte),
3094 len2_byte, end1, start1_byte + len1_byte);
3095 combined_after_bytes_1
3096 = count_combining_after (BYTE_POS_ADDR (start1_byte),
3097 len1_byte, end2, start2_byte + len2_byte);
3098 }
3099
3100 /* If any combining is going to happen, do this the stupid way,
3101 because replace handles combining properly. */
3102 if (combined_before_bytes_1 || combined_before_bytes_2
3103 || combined_after_bytes_1 || combined_after_bytes_2)
3104 {
3105 Lisp_Object text1, text2;
3106
3107 text1 = text2 = Qnil;
3108 GCPRO2 (text1, text2);
3109
3110 text1 = make_buffer_string_both (start1, start1_byte,
3111 end1, start1_byte + len1_byte, 1);
3112 text2 = make_buffer_string_both (start2, start2_byte,
3113 end2, start2_byte + len2_byte, 1);
3114
3115 transpose_markers (start1, end1, start2, end2,
3116 start1_byte, start1_byte + len1_byte,
3117 start2_byte, start2_byte + len2_byte);
3118
c2b70590
RS
3119 replace_range (start2, end2, text1, 1, 0, 0);
3120 replace_range (start1, end1, text2, 1, 0, 0);
dc3620af
RS
3121
3122 UNGCPRO;
3123 return Qnil;
3124 }
b229b8d1
RS
3125
3126 /* Hmmm... how about checking to see if the gap is large
3127 enough to use as the temporary storage? That would avoid an
3128 allocation... interesting. Later, don't fool with it now. */
3129
3130 /* Working without memmove, for portability (sigh), so must be
3131 careful of overlapping subsections of the array... */
3132
3133 if (end1 == start2) /* adjacent regions */
3134 {
b229b8d1
RS
3135 modify_region (current_buffer, start1, end2);
3136 record_change (start1, len1 + len2);
3137
3138#ifdef USE_TEXT_PROPERTIES
3139 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3140 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
09dbcf71
RS
3141 Fset_text_properties (make_number (start1), make_number (end2),
3142 Qnil, Qnil);
b229b8d1
RS
3143#endif /* USE_TEXT_PROPERTIES */
3144
3145 /* First region smaller than second. */
ec1c14f6 3146 if (len1_byte < len2_byte)
b229b8d1 3147 {
3c6bc7d0
RS
3148 /* We use alloca only if it is small,
3149 because we want to avoid stack overflow. */
ec1c14f6
RS
3150 if (len2_byte > 20000)
3151 temp = (unsigned char *) xmalloc (len2_byte);
3c6bc7d0 3152 else
ec1c14f6 3153 temp = (unsigned char *) alloca (len2_byte);
03240d11
KH
3154
3155 /* Don't precompute these addresses. We have to compute them
3156 at the last minute, because the relocating allocator might
3157 have moved the buffer around during the xmalloc. */
23017390
KH
3158 start1_addr = BYTE_POS_ADDR (start1_byte);
3159 start2_addr = BYTE_POS_ADDR (start2_byte);
03240d11 3160
ec1c14f6
RS
3161 bcopy (start2_addr, temp, len2_byte);
3162 bcopy (start1_addr, start1_addr + len2_byte, len1_byte);
3163 bcopy (temp, start1_addr, len2_byte);
3164 if (len2_byte > 20000)
3c6bc7d0 3165 free (temp);
b229b8d1
RS
3166 }
3167 else
3168 /* First region not smaller than second. */
3169 {
ec1c14f6
RS
3170 if (len1_byte > 20000)
3171 temp = (unsigned char *) xmalloc (len1_byte);
3c6bc7d0 3172 else
ec1c14f6 3173 temp = (unsigned char *) alloca (len1_byte);
23017390
KH
3174 start1_addr = BYTE_POS_ADDR (start1_byte);
3175 start2_addr = BYTE_POS_ADDR (start2_byte);
ec1c14f6
RS
3176 bcopy (start1_addr, temp, len1_byte);
3177 bcopy (start2_addr, start1_addr, len2_byte);
3178 bcopy (temp, start1_addr + len2_byte, len1_byte);
3179 if (len1_byte > 20000)
3c6bc7d0 3180 free (temp);
b229b8d1
RS
3181 }
3182#ifdef USE_TEXT_PROPERTIES
3183 graft_intervals_into_buffer (tmp_interval1, start1 + len2,
3184 len1, current_buffer, 0);
3185 graft_intervals_into_buffer (tmp_interval2, start1,
3186 len2, current_buffer, 0);
3187#endif /* USE_TEXT_PROPERTIES */
3188 }
3189 /* Non-adjacent regions, because end1 != start2, bleagh... */
3190 else
3191 {
ec1c14f6
RS
3192 len_mid = start2_byte - (start1_byte + len1_byte);
3193
3194 if (len1_byte == len2_byte)
b229b8d1
RS
3195 /* Regions are same size, though, how nice. */
3196 {
3197 modify_region (current_buffer, start1, end1);
3198 modify_region (current_buffer, start2, end2);
3199 record_change (start1, len1);
3200 record_change (start2, len2);
3201#ifdef USE_TEXT_PROPERTIES
3202 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3203 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
09dbcf71
RS
3204 Fset_text_properties (make_number (start1), make_number (end1),
3205 Qnil, Qnil);
3206 Fset_text_properties (make_number (start2), make_number (end2),
3207 Qnil, Qnil);
b229b8d1
RS
3208#endif /* USE_TEXT_PROPERTIES */
3209
ec1c14f6
RS
3210 if (len1_byte > 20000)
3211 temp = (unsigned char *) xmalloc (len1_byte);
3c6bc7d0 3212 else
ec1c14f6 3213 temp = (unsigned char *) alloca (len1_byte);
23017390
KH
3214 start1_addr = BYTE_POS_ADDR (start1_byte);
3215 start2_addr = BYTE_POS_ADDR (start2_byte);
ec1c14f6
RS
3216 bcopy (start1_addr, temp, len1_byte);
3217 bcopy (start2_addr, start1_addr, len2_byte);
3218 bcopy (temp, start2_addr, len1_byte);
3219 if (len1_byte > 20000)
3c6bc7d0 3220 free (temp);
b229b8d1
RS
3221#ifdef USE_TEXT_PROPERTIES
3222 graft_intervals_into_buffer (tmp_interval1, start2,
3223 len1, current_buffer, 0);
3224 graft_intervals_into_buffer (tmp_interval2, start1,
3225 len2, current_buffer, 0);
3226#endif /* USE_TEXT_PROPERTIES */
3227 }
3228
ec1c14f6 3229 else if (len1_byte < len2_byte) /* Second region larger than first */
b229b8d1
RS
3230 /* Non-adjacent & unequal size, area between must also be shifted. */
3231 {
b229b8d1
RS
3232 modify_region (current_buffer, start1, end2);
3233 record_change (start1, (end2 - start1));
3234#ifdef USE_TEXT_PROPERTIES
3235 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3236 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
3237 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
09dbcf71
RS
3238 Fset_text_properties (make_number (start1), make_number (end2),
3239 Qnil, Qnil);
b229b8d1
RS
3240#endif /* USE_TEXT_PROPERTIES */
3241
3c6bc7d0 3242 /* holds region 2 */
ec1c14f6
RS
3243 if (len2_byte > 20000)
3244 temp = (unsigned char *) xmalloc (len2_byte);
3c6bc7d0 3245 else
ec1c14f6 3246 temp = (unsigned char *) alloca (len2_byte);
23017390
KH
3247 start1_addr = BYTE_POS_ADDR (start1_byte);
3248 start2_addr = BYTE_POS_ADDR (start2_byte);
ec1c14f6
RS
3249 bcopy (start2_addr, temp, len2_byte);
3250 bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte);
3251 safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
3252 bcopy (temp, start1_addr, len2_byte);
3253 if (len2_byte > 20000)
3c6bc7d0 3254 free (temp);
b229b8d1
RS
3255#ifdef USE_TEXT_PROPERTIES
3256 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
3257 len1, current_buffer, 0);
3258 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
3259 len_mid, current_buffer, 0);
3260 graft_intervals_into_buffer (tmp_interval2, start1,
3261 len2, current_buffer, 0);
3262#endif /* USE_TEXT_PROPERTIES */
3263 }
3264 else
3265 /* Second region smaller than first. */
3266 {
b229b8d1
RS
3267 record_change (start1, (end2 - start1));
3268 modify_region (current_buffer, start1, end2);
3269
3270#ifdef USE_TEXT_PROPERTIES
3271 tmp_interval1 = copy_intervals (cur_intv, start1, len1);
3272 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid);
3273 tmp_interval2 = copy_intervals (cur_intv, start2, len2);
09dbcf71
RS
3274 Fset_text_properties (make_number (start1), make_number (end2),
3275 Qnil, Qnil);
b229b8d1
RS
3276#endif /* USE_TEXT_PROPERTIES */
3277
3c6bc7d0 3278 /* holds region 1 */
ec1c14f6
RS
3279 if (len1_byte > 20000)
3280 temp = (unsigned char *) xmalloc (len1_byte);
3c6bc7d0 3281 else
ec1c14f6 3282 temp = (unsigned char *) alloca (len1_byte);
23017390
KH
3283 start1_addr = BYTE_POS_ADDR (start1_byte);
3284 start2_addr = BYTE_POS_ADDR (start2_byte);
ec1c14f6
RS
3285 bcopy (start1_addr, temp, len1_byte);
3286 bcopy (start2_addr, start1_addr, len2_byte);
3287 bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid);
3288 bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte);
3289 if (len1_byte > 20000)
3c6bc7d0 3290 free (temp);
b229b8d1
RS
3291#ifdef USE_TEXT_PROPERTIES
3292 graft_intervals_into_buffer (tmp_interval1, end2 - len1,
3293 len1, current_buffer, 0);
3294 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2,
3295 len_mid, current_buffer, 0);
3296 graft_intervals_into_buffer (tmp_interval2, start1,
3297 len2, current_buffer, 0);
3298#endif /* USE_TEXT_PROPERTIES */
3299 }
3300 }
3301
ec1c14f6
RS
3302 /* When doing multiple transpositions, it might be nice
3303 to optimize this. Perhaps the markers in any one buffer
3304 should be organized in some sorted data tree. */
b229b8d1 3305 if (NILP (leave_markers))
8de1d5f0 3306 {
ec1c14f6
RS
3307 transpose_markers (start1, end1, start2, end2,
3308 start1_byte, start1_byte + len1_byte,
3309 start2_byte, start2_byte + len2_byte);
8de1d5f0
KH
3310 fix_overlays_in_range (start1, end2);
3311 }
b229b8d1
RS
3312
3313 return Qnil;
3314}
35692fe0 3315
35692fe0
JB
3316\f
3317void
3318syms_of_editfns ()
3319{
260e2e2a
KH
3320 environbuf = 0;
3321
3322 Qbuffer_access_fontify_functions
3323 = intern ("buffer-access-fontify-functions");
3324 staticpro (&Qbuffer_access_fontify_functions);
3325
3326 DEFVAR_LISP ("buffer-access-fontify-functions",
3327 &Vbuffer_access_fontify_functions,
3328 "List of functions called by `buffer-substring' to fontify if necessary.\n\
3329Each function is called with two arguments which specify the range\n\
3330of the buffer being accessed.");
3331 Vbuffer_access_fontify_functions = Qnil;
3332
af209db8
RS
3333 {
3334 Lisp_Object obuf;
3335 extern Lisp_Object Vprin1_to_string_buffer;
3336 obuf = Fcurrent_buffer ();
3337 /* Do this here, because init_buffer_once is too early--it won't work. */
3338 Fset_buffer (Vprin1_to_string_buffer);
3339 /* Make sure buffer-access-fontify-functions is nil in this buffer. */
3340 Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")),
3341 Qnil);
3342 Fset_buffer (obuf);
3343 }
3344
0b6fd023 3345 DEFVAR_LISP ("buffer-access-fontified-property",
260e2e2a
KH
3346 &Vbuffer_access_fontified_property,
3347 "Property which (if non-nil) indicates text has been fontified.\n\
3348`buffer-substring' need not call the `buffer-access-fontify-functions'\n\
3349functions if all the text being accessed has this property.");
3350 Vbuffer_access_fontified_property = Qnil;
3351
f43754f6
KH
3352 DEFVAR_LISP ("system-name", &Vsystem_name,
3353 "The name of the machine Emacs is running on.");
3354
3355 DEFVAR_LISP ("user-full-name", &Vuser_full_name,
3356 "The full name of the user logged in.");
3357
35b34f72 3358 DEFVAR_LISP ("user-login-name", &Vuser_login_name,
f43754f6
KH
3359 "The user's name, taken from environment variables if possible.");
3360
35b34f72 3361 DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name,
f43754f6 3362 "The user's name, based upon the real uid only.");
35692fe0
JB
3363
3364 defsubr (&Schar_equal);
3365 defsubr (&Sgoto_char);
3366 defsubr (&Sstring_to_char);
3367 defsubr (&Schar_to_string);
3368 defsubr (&Sbuffer_substring);
260e2e2a 3369 defsubr (&Sbuffer_substring_no_properties);
35692fe0
JB
3370 defsubr (&Sbuffer_string);
3371
3372 defsubr (&Spoint_marker);
3373 defsubr (&Smark_marker);
3374 defsubr (&Spoint);
3375 defsubr (&Sregion_beginning);
3376 defsubr (&Sregion_end);
7df74da6
RS
3377
3378 defsubr (&Sline_beginning_position);
3379 defsubr (&Sline_end_position);
3380
35692fe0
JB
3381/* defsubr (&Smark); */
3382/* defsubr (&Sset_mark); */
3383 defsubr (&Ssave_excursion);
4bc8c7d2 3384 defsubr (&Ssave_current_buffer);
35692fe0
JB
3385
3386 defsubr (&Sbufsize);
3387 defsubr (&Spoint_max);
3388 defsubr (&Spoint_min);
3389 defsubr (&Spoint_min_marker);
3390 defsubr (&Spoint_max_marker);
c86212b9
RS
3391 defsubr (&Sgap_position);
3392 defsubr (&Sgap_size);
7df74da6 3393 defsubr (&Sposition_bytes);
3ab0732d 3394 defsubr (&Sbyte_to_position);
c9ed721d 3395
35692fe0
JB
3396 defsubr (&Sbobp);
3397 defsubr (&Seobp);
3398 defsubr (&Sbolp);
3399 defsubr (&Seolp);
850a8179
JB
3400 defsubr (&Sfollowing_char);
3401 defsubr (&Sprevious_char);
35692fe0 3402 defsubr (&Schar_after);
fb8106e8 3403 defsubr (&Schar_before);
35692fe0
JB
3404 defsubr (&Sinsert);
3405 defsubr (&Sinsert_before_markers);
be91036a
RS
3406 defsubr (&Sinsert_and_inherit);
3407 defsubr (&Sinsert_and_inherit_before_markers);
35692fe0
JB
3408 defsubr (&Sinsert_char);
3409
3410 defsubr (&Suser_login_name);
3411 defsubr (&Suser_real_login_name);
3412 defsubr (&Suser_uid);
3413 defsubr (&Suser_real_uid);
3414 defsubr (&Suser_full_name);
7fd233b3 3415 defsubr (&Semacs_pid);
d940e0e4 3416 defsubr (&Scurrent_time);
a82d387c 3417 defsubr (&Sformat_time_string);
4691c06d 3418 defsubr (&Sdecode_time);
cce7b8a0 3419 defsubr (&Sencode_time);
35692fe0 3420 defsubr (&Scurrent_time_string);
c2662aea 3421 defsubr (&Scurrent_time_zone);
143cb9a9 3422 defsubr (&Sset_time_zone_rule);
35692fe0 3423 defsubr (&Ssystem_name);
35692fe0 3424 defsubr (&Smessage);
cacc3e2c
RS
3425 defsubr (&Smessage_box);
3426 defsubr (&Smessage_or_box);
b14dda8a 3427 defsubr (&Scurrent_message);
35692fe0 3428 defsubr (&Sformat);
35692fe0
JB
3429
3430 defsubr (&Sinsert_buffer_substring);
e9cf2084 3431 defsubr (&Scompare_buffer_substrings);
35692fe0
JB
3432 defsubr (&Ssubst_char_in_region);
3433 defsubr (&Stranslate_region);
3434 defsubr (&Sdelete_region);
3435 defsubr (&Swiden);
3436 defsubr (&Snarrow_to_region);
3437 defsubr (&Ssave_restriction);
b229b8d1 3438 defsubr (&Stranspose_regions);
35692fe0 3439}