home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / editfns.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  102KB  |  3,270 lines

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