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