(advance_to_char_boundary): Handle the case the code
[bpt/emacs.git] / src / buffer.c
index b52c990..9fcae96 100644 (file)
@@ -1,5 +1,5 @@
 /* Buffer manipulation primitives for GNU Emacs.
-   Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995
+   Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995, 1997, 1998
        Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -23,6 +23,9 @@ Boston, MA 02111-1307, USA.  */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/param.h>
+#include <errno.h>
+
+extern int errno;
 
 #ifndef MAXPATHLEN
 /* in 4.1, param.h fails to define this. */
@@ -35,9 +38,11 @@ Boston, MA 02111-1307, USA.  */
 #include "window.h"
 #include "commands.h"
 #include "buffer.h"
+#include "charset.h"
 #include "region-cache.h"
 #include "indent.h"
 #include "blockinput.h"
+#include "frame.h"
 
 struct buffer *current_buffer;         /* the current buffer */
 
@@ -95,8 +100,11 @@ static Lisp_Object Vbuffer_local_symbols;
    buffer-local slots.  If a slot contains Qnil, then the
    corresponding buffer slot may contain a value of any type.  If a
    slot contains an integer, then prospective values' tags must be
-   equal to that integer.  When a tag does not match, the function
-   buffer_slot_type_mismatch will signal an error.  */
+   equal to that integer (except nil is always allowed).
+   When a tag does not match, the function
+   buffer_slot_type_mismatch will signal an error.
+
+   If a slot here contains -1, the corresponding variable is read-only.  */
 struct buffer buffer_local_types;
 
 /* Flags indicating which built-in buffer-local variables
@@ -160,6 +168,7 @@ Lisp_Object Qinsert_behind_hooks;
 /* For debugging; temporary.  See set_buffer_internal.  */
 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
 
+void
 nsberror (spec)
      Lisp_Object spec;
 {
@@ -178,11 +187,34 @@ Value is nil if OBJECT is not a buffer or if it has been killed.")
          ? Qt : Qnil);
 }
 
-DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 0, 0,
-  "Return a list of all existing live buffers.")
-  ()
+DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 1, 0,
+  "Return a list of all existing live buffers.\n\
+If the optional arg FRAME is a frame, we return that frame's buffer list.")
+  (frame)
+     Lisp_Object frame;
 {
-  return Fmapcar (Qcdr, Vbuffer_alist);
+  Lisp_Object framelist, general;
+  general = Fmapcar (Qcdr, Vbuffer_alist);
+
+  if (FRAMEP (frame))
+    {
+      Lisp_Object tail;
+
+      CHECK_FRAME (frame, 1);
+
+      framelist = Fcopy_sequence (XFRAME (frame)->buffer_list);
+
+      /* Remove from GENERAL any buffer that duplicates one in FRAMELIST.  */
+      tail = framelist;
+      while (! NILP (tail))
+       {
+         general = Fdelq (XCONS (tail)->car, general);
+         tail = XCONS (tail)->cdr;
+       }
+      return nconc2 (framelist, general);
+    }
+
+  return general;
 }
 
 /* Like Fassoc, but use Fstring_equal to compare
@@ -302,7 +334,9 @@ The value is never nil.")
 
   BUF_GAP_SIZE (b) = 20;
   BLOCK_INPUT;
-  BUFFER_ALLOC (BUF_BEG_ADDR (b), BUF_GAP_SIZE (b));
+  /* We allocate extra 1-byte at the tail and keep it always '\0' for
+     anchoring a search.  */
+  BUFFER_ALLOC (BUF_BEG_ADDR (b), (BUF_GAP_SIZE (b) + 1));
   UNBLOCK_INPUT;
   if (! BUF_BEG_ADDR (b))
     buffer_memory_full ();
@@ -312,10 +346,16 @@ The value is never nil.")
   BUF_BEGV (b) = 1;
   BUF_ZV (b) = 1;
   BUF_Z (b) = 1;
+  BUF_PT_BYTE (b) = 1;
+  BUF_GPT_BYTE (b) = 1;
+  BUF_BEGV_BYTE (b) = 1;
+  BUF_ZV_BYTE (b) = 1;
+  BUF_Z_BYTE (b) = 1;
   BUF_MODIFF (b) = 1;
   BUF_OVERLAY_MODIFF (b) = 1;
   BUF_SAVE_MODIFF (b) = 1;
   BUF_INTERVALS (b) = 0;
+  *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'.  */
 
   b->newline_cache = 0;
   b->width_run_cache = 0;
@@ -391,6 +431,9 @@ NAME should be a string which is not the name of an existing buffer.")
   BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
   BUF_ZV (b) = BUF_ZV (b->base_buffer);
   BUF_PT (b) = BUF_PT (b->base_buffer);
+  BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
+  BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
+  BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
 
   b->newline_cache = 0;
   b->width_run_cache = 0;
@@ -418,31 +461,33 @@ NAME should be a string which is not the name of an existing buffer.")
   if (NILP (b->base_buffer->pt_marker))
     {
       b->base_buffer->pt_marker = Fmake_marker ();
-      Fset_marker (b->base_buffer->pt_marker,
-                  make_number (BUF_PT (b->base_buffer)), base_buffer);
+      set_marker_both (b->base_buffer->pt_marker, base_buffer,
+                      BUF_PT (b->base_buffer),
+                      BUF_PT_BYTE (b->base_buffer));
     }
   if (NILP (b->base_buffer->begv_marker))
     {
       b->base_buffer->begv_marker = Fmake_marker ();
-      Fset_marker (b->base_buffer->begv_marker,
-                  make_number (BUF_BEGV (b->base_buffer)), base_buffer);
+      set_marker_both (b->base_buffer->begv_marker, base_buffer,
+                      BUF_BEGV (b->base_buffer),
+                      BUF_BEGV_BYTE (b->base_buffer));
     }
   if (NILP (b->base_buffer->zv_marker))
     {
       b->base_buffer->zv_marker = Fmake_marker ();
-      Fset_marker (b->base_buffer->zv_marker,
-                  make_number (BUF_ZV (b->base_buffer)), base_buffer);
+      set_marker_both (b->base_buffer->zv_marker, base_buffer,
+                      BUF_ZV (b->base_buffer),
+                      BUF_ZV_BYTE (b->base_buffer));
       XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
     }
 
   /* Give the indirect buffer markers for its narrowing.  */
   b->pt_marker = Fmake_marker ();
-  Fset_marker (b->pt_marker, make_number (BUF_PT (b)), buf);
+  set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
   b->begv_marker = Fmake_marker ();
-  Fset_marker (b->begv_marker, make_number (BUF_BEGV (b)), buf);
+  set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
   b->zv_marker = Fmake_marker ();
-  Fset_marker (b->zv_marker, make_number (BUF_ZV (b)), buf);
-
+  set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
   XMARKER (b->zv_marker)->insertion_type = 1;
 
   return buf;
@@ -461,6 +506,8 @@ reset_buffer (b)
   b->modtime = 0;
   XSETFASTINT (b->save_length, 0);
   b->last_window_start = 1;
+  /* It is more conservative to start out "changed" than "unchanged".  */
+  b->clip_changed = 1;
   b->backed_up = Qnil;
   b->auto_save_modified = 0;
   b->auto_save_failure_time = -1;
@@ -473,8 +520,10 @@ reset_buffer (b)
   b->point_before_scroll = Qnil;
   b->file_format = Qnil;
   b->last_selected_window = Qnil;
+  XSETINT (b->display_count, 0);
   b->extra2 = Qnil;
   b->extra3 = Qnil;
+  b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
 }
 
 /* Reset buffer B's local variables info.
@@ -844,10 +893,26 @@ If BUFFER is omitted or nil, some interesting buffer is returned.")
      register Lisp_Object buffer, visible_ok;
 {
   Lisp_Object Fset_buffer_major_mode ();
-  register Lisp_Object tail, buf, notsogood, tem;
+  register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
   notsogood = Qnil;
 
-  for (tail = Vbuffer_alist; !NILP (tail); tail = Fcdr (tail))
+  tail = Vbuffer_alist;
+  pred = frame_buffer_predicate ();
+
+  /* Consider buffers that have been seen in the selected frame
+     before other buffers.  */
+    
+  tem = frame_buffer_list ();
+  add_ons = Qnil;
+  while (CONSP (tem))
+    {
+      if (BUFFERP (XCONS (tem)->car))
+       add_ons = Fcons (Fcons (Qnil, XCONS (tem)->car), add_ons);
+      tem = XCONS (tem)->cdr;
+    }
+  tail = nconc2 (Fnreverse (add_ons), tail);
+
+  for (; !NILP (tail); tail = Fcdr (tail))
     {
       buf = Fcdr (Fcar (tail));
       if (EQ (buf, buffer))
@@ -856,10 +921,9 @@ If BUFFER is omitted or nil, some interesting buffer is returned.")
        continue;
       /* If the selected frame has a buffer_predicate,
         disregard buffers that don't fit the predicate.  */
-      tem = frame_buffer_predicate ();
-      if (!NILP (tem))
+      if (!NILP (pred))
        {
-         tem = call1 (tem, buf);
+         tem = call1 (pred, buf);
          if (NILP (tem))
            continue;
        }
@@ -1059,12 +1123,13 @@ with SIGHUP.")
   Vinhibit_quit = Qt;
   replace_buffer_in_all_windows (buf);
   Vbuffer_alist = Fdelq (Frassq (buf, Vbuffer_alist), Vbuffer_alist);
+  frames_discard_buffer (buf);
   Vinhibit_quit = tem;
 
   /* Delete any auto-save file, if we saved it in this session.  */
   if (STRINGP (b->auto_save_file_name)
       && b->auto_save_modified != 0
-      && SAVE_MODIFF < b->auto_save_modified)
+      && BUF_SAVE_MODIFF (b) < b->auto_save_modified)
     {
       Lisp_Object tem;
       tem = Fsymbol_value (intern ("delete-auto-save-files"));
@@ -1142,6 +1207,7 @@ with SIGHUP.")
    selected buffers are always closer to the front of the list.  This
    means that other_buffer is more likely to choose a relevant buffer.  */
 
+void
 record_buffer (buf)
      Lisp_Object buf;
 {
@@ -1163,8 +1229,33 @@ record_buffer (buf)
   else
     XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
        
-  XCONS(link)->cdr = Vbuffer_alist;
+  XCONS (link)->cdr = Vbuffer_alist;
   Vbuffer_alist = link;
+
+  /* Now move this buffer to the front of frame_buffer_list also.  */
+
+  prev = Qnil;
+  for (link = frame_buffer_list (); CONSP (link); link = XCONS (link)->cdr)
+    {
+      if (EQ (XCONS (link)->car, buf))
+       break;
+      prev = link;
+    }
+
+  /* Effectively do delq.  */
+
+  if (CONSP (link))
+    {
+      if (NILP (prev))
+       set_frame_buffer_list (XCONS (frame_buffer_list ())->cdr);
+      else
+       XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
+       
+      XCONS (link)->cdr = frame_buffer_list ();
+      set_frame_buffer_list (link);
+    }
+  else
+    set_frame_buffer_list (Fcons (buf, frame_buffer_list ()));
 }
 
 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
@@ -1238,10 +1329,10 @@ the window-buffer correspondences.")
                      : selected_window,
                      buf);
 
-  3eturn buf;
+  return buf;
 }
 
-DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 2, 0,
+DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
   "Select buffer BUFFER in some window, preferably a different one.\n\
 If BUFFER is nil, then some other buffer is chosen.\n\
 If `pop-up-windows' is non-nil, windows can be split to do this.\n\
@@ -1270,7 +1361,7 @@ do not put this buffer at the front of the list of recently selected ones.")
   Fset_buffer (buf);
   if (NILP (norecord))
     record_buffer (buf);
-  Fselect_window (Fdisplay_buffer (buf, other_window));
+  Fselect_window (Fdisplay_buffer (buf, other_window, Qnil));
   return buf;
 }
 
@@ -1331,19 +1422,22 @@ set_buffer_internal_1 (b)
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
+         set_marker_both (old_buf->pt_marker, obuf,
+                          BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
        }
       if (! NILP (old_buf->begv_marker))
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
+         set_marker_both (old_buf->begv_marker, obuf,
+                          BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
        }
       if (! NILP (old_buf->zv_marker))
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
+         set_marker_both (old_buf->zv_marker, obuf,
+                          BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
        }
     }
 
@@ -1355,11 +1449,20 @@ set_buffer_internal_1 (b)
   /* If the new current buffer has markers to record PT, BEGV and ZV
      when it is not current, fetch them now.  */
   if (! NILP (b->pt_marker))
-    BUF_PT (b) = marker_position (b->pt_marker);
+    {
+      BUF_PT (b) = marker_position (b->pt_marker);
+      BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
+    }
   if (! NILP (b->begv_marker))
-    BUF_BEGV (b) = marker_position (b->begv_marker);
+    {
+      BUF_BEGV (b) = marker_position (b->begv_marker);
+      BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
+    }
   if (! NILP (b->zv_marker))
-    BUF_ZV (b) = marker_position (b->zv_marker);
+    {
+      BUF_ZV (b) = marker_position (b->zv_marker);
+      BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
+    }
 
   /* Look down buffer's list of local Lisp variables
      to find and update any that forward into C variables. */
@@ -1415,30 +1518,42 @@ set_buffer_temp (b)
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
+         set_marker_both (old_buf->pt_marker, obuf,
+                          BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
        }
       if (! NILP (old_buf->begv_marker))
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
+         set_marker_both (old_buf->begv_marker, obuf,
+                          BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
        }
       if (! NILP (old_buf->zv_marker))
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
+         set_marker_both (old_buf->zv_marker, obuf,
+                          BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
        }
     }
 
   /* If the new current buffer has markers to record PT, BEGV and ZV
      when it is not current, fetch them now.  */
   if (! NILP (b->pt_marker))
-    BUF_PT (b) = marker_position (b->pt_marker);
+    {
+      BUF_PT (b) = marker_position (b->pt_marker);
+      BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
+    }
   if (! NILP (b->begv_marker))
-    BUF_BEGV (b) = marker_position (b->begv_marker);
+    {
+      BUF_BEGV (b) = marker_position (b->begv_marker);
+      BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
+    }
   if (! NILP (b->zv_marker))
-    BUF_ZV (b) = marker_position (b->zv_marker);
+    {
+      BUF_ZV (b) = marker_position (b->zv_marker);
+      BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
+    }
 }
 
 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
@@ -1460,6 +1575,17 @@ Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
   set_buffer_internal (XBUFFER (buf));
   return buf;
 }
+
+/* Set the current buffer to BUFFER provided it is alive.  */
+
+Lisp_Object
+set_buffer_if_live (buffer)
+     Lisp_Object buffer;
+{
+  if (! NILP (XBUFFER (buffer)->name))
+    Fset_buffer (buffer);
+  return Qnil;
+}
 \f
 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
                                   Sbarf_if_buffer_read_only, 0, 0, 0,
@@ -1511,6 +1637,8 @@ selected window if it is displayed there.")
     Vbuffer_alist = nconc2 (Vbuffer_alist, link);
   }
 
+  frames_bury_buffer (buffer);
+
   return Qnil;
 }
 \f
@@ -1530,6 +1658,7 @@ so the buffer is truly empty after this.")
   return Qnil;
 }
 
+void
 validate_region (b, e)
      register Lisp_Object *b, *e;
 {
@@ -1547,6 +1676,148 @@ validate_region (b, e)
     args_out_of_range (*b, *e);
 }
 \f
+/* Advance BYTE_POS up to a character boundary
+   and return the adjusted position.  */
+
+static int
+advance_to_char_boundary (byte_pos)
+     int byte_pos;
+{
+  int c;
+
+  if (byte_pos == BEG)
+    /* Beginning of buffer is always a character boundary.  */
+    return 1;
+
+  c = FETCH_BYTE (byte_pos);
+  if (! CHAR_HEAD_P (c))
+    {
+      /* We should advance BYTE_POS only when C is a constituen of a
+         multibyte sequence.  */
+      DEC_POS (byte_pos);
+      INC_POS (byte_pos);
+      /* If C is a constituent of a multibyte sequence, BYTE_POS was
+         surely advance to the correct character boundary.  If C is
+         not, BYTE_POS was unchanged.  */
+    }
+
+  return byte_pos;
+}
+
+DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
+       1, 1, 0,
+  "Set the multibyte flag of the current buffer to FLAG.\n\
+If FLAG is t, this makes the buffer a multibyte buffer.\n\
+If FLAG is nil, this makes the buffer a single-byte buffer.\n\
+The buffer contents remain unchanged as a sequence of bytes\n\
+but the contents viewed as characters do change.")
+  (flag)
+     Lisp_Object flag;
+{
+  Lisp_Object tail, markers;
+
+  /* It would be better to update the list,
+     but this is good enough for now.  */
+  if (! EQ (current_buffer->undo_list, Qt))
+    current_buffer->undo_list = Qnil;
+
+  /* If the cached position is for this buffer, clear it out.  */
+  clear_charpos_cache (current_buffer);
+
+  if (NILP (flag))
+    {
+      /* Do this first, so it can use CHAR_TO_BYTE
+        to calculate the old correspondences.  */
+      set_intervals_multibyte (0);
+
+      current_buffer->enable_multibyte_characters = Qnil;
+
+      Z = Z_BYTE;
+      BEGV = BEGV_BYTE;
+      ZV = ZV_BYTE;
+      GPT = GPT_BYTE;
+      TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
+
+      tail = BUF_MARKERS (current_buffer);
+      while (XSYMBOL (tail) != XSYMBOL (Qnil))
+       {
+         XMARKER (tail)->charpos = XMARKER (tail)->bytepos;
+         tail = XMARKER (tail)->chain;
+       }
+    }
+  else
+    {
+      /* Be sure not to have a multibyte sequence striding over the GAP.
+        Ex: We change this: "...abc\201\241\241 _GAP_ \241\241\241..."
+            to: "...abc _GAP_ \201\241\241\241\241\241..."  */
+
+      if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
+         && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
+       {
+         unsigned char *p = GPT_ADDR - 1;
+
+         while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
+         if (BASE_LEADING_CODE_P (*p))
+           {
+             int new_gpt = GPT_BYTE - (GPT_ADDR - p);
+
+             move_gap_both (new_gpt, new_gpt);
+           }
+       }
+
+      /* Do this first, so that chars_in_text asks the right question.
+        set_intervals_multibyte needs it too.  */
+      current_buffer->enable_multibyte_characters = Qt;
+
+      GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
+      GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
+
+      Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
+
+      BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
+      if (BEGV_BYTE > GPT_BYTE)
+       BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
+      else
+       BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
+
+      ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
+      if (ZV_BYTE > GPT_BYTE)
+       ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
+      else
+       ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
+
+      {
+       int pt_byte = advance_to_char_boundary (PT_BYTE);
+       int pt;
+
+       if (pt_byte > GPT_BYTE)
+         pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
+       else
+         pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
+       TEMP_SET_PT_BOTH (pt, pt_byte);
+      }
+
+      tail = markers = BUF_MARKERS (current_buffer);
+      BUF_MARKERS (current_buffer) = Qnil;
+
+      while (XSYMBOL (tail) != XSYMBOL (Qnil))
+       {
+         XMARKER (tail)->bytepos
+           = advance_to_char_boundary (XMARKER (tail)->bytepos);
+         XMARKER (tail)->charpos = BYTE_TO_CHAR (XMARKER (tail)->bytepos);
+
+         tail = XMARKER (tail)->chain;
+       }
+      BUF_MARKERS (current_buffer) = markers;
+
+      /* Do this last, so it can calculate the new correspondences
+        between chars and bytes.  */
+      set_intervals_multibyte (1);
+    }
+
+  return flag;
+}
+\f
 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
   0, 0, 0,
   "Switch to Fundamental mode by killing current buffer's local variables.\n\
@@ -2047,7 +2318,7 @@ struct sortstrlist
    it's more efficient to hold onto the memory instead of repeatedly
    allocating and freeing it.  */
 static struct sortstrlist overlay_heads, overlay_tails;
-static char *overlay_str_buf;
+static unsigned char *overlay_str_buf;
 
 /* Allocated length of overlay_str_buf.  */
 static int overlay_str_len;
@@ -2072,6 +2343,8 @@ record_overlay_string (ssl, str, str2, pri, size)
      Lisp_Object str, str2, pri;
      int size;
 {
+  int nbytes;
+
   if (ssl->used == ssl->size)
     {
       if (ssl->buf)
@@ -2086,9 +2359,29 @@ record_overlay_string (ssl, str, str2, pri, size)
   ssl->buf[ssl->used].size = size;
   ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
   ssl->used++;
-  ssl->bytes += XSTRING (str)->size;
+
+  if (NILP (current_buffer->enable_multibyte_characters))
+    nbytes = XSTRING (str)->size;
+  else if (! STRING_MULTIBYTE (str))
+    nbytes = count_size_as_multibyte (XSTRING (str)->data,
+                                     XSTRING (str)->size_byte);
+  else
+    nbytes = XSTRING (str)->size_byte;
+
+  ssl->bytes += nbytes;
+
   if (STRINGP (str2))
-    ssl->bytes += XSTRING (str2)->size;
+    {
+      if (NILP (current_buffer->enable_multibyte_characters))
+       nbytes = XSTRING (str2)->size;
+      else if (! STRING_MULTIBYTE (str2))
+       nbytes = count_size_as_multibyte (XSTRING (str2)->data,
+                                         XSTRING (str2)->size_byte);
+      else
+       nbytes = XSTRING (str2)->size_byte;
+
+      ssl->bytes += nbytes;
+    }
 }
 
 /* Return the concatenation of the strings associated with overlays that
@@ -2111,6 +2404,7 @@ overlay_strings (pos, w, pstr)
 {
   Lisp_Object ov, overlay, window, str;
   int startpos, endpos;
+  int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
 
   overlay_heads.used = overlay_heads.bytes = 0;
   overlay_tails.used = overlay_tails.bytes = 0;
@@ -2182,29 +2476,38 @@ overlay_strings (pos, w, pstr)
     {
       Lisp_Object tem;
       int i;
-      char *p;
+      unsigned char *p;
       int total = overlay_heads.bytes + overlay_tails.bytes;
 
       if (total > overlay_str_len)
-       overlay_str_buf = (char *)xrealloc (overlay_str_buf,
-                                           overlay_str_len = total);
+       {
+         overlay_str_len = total;
+         overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
+                                                      total);
+       }
       p = overlay_str_buf;
       for (i = overlay_tails.used; --i >= 0;)
        {
+         int nbytes;
          tem = overlay_tails.buf[i].string;
-         bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
-         p += XSTRING (tem)->size;
+         nbytes = copy_text (XSTRING (tem)->data, p, XSTRING (tem)->size_byte,
+                             STRING_MULTIBYTE (tem), multibyte);
+         p += nbytes;
        }
       for (i = 0; i < overlay_heads.used; ++i)
        {
+         int nbytes;
          tem = overlay_heads.buf[i].string;
-         bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
-         p += XSTRING (tem)->size;
+         nbytes = copy_text (XSTRING (tem)->data, p, XSTRING (tem)->size_byte,
+                             STRING_MULTIBYTE (tem), multibyte);
+         p += nbytes;
          tem = overlay_heads.buf[i].string2;
          if (STRINGP (tem))
            {
-             bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
-             p += XSTRING (tem)->size;
+             nbytes = copy_text (XSTRING (tem)->data, p,
+                                 XSTRING (tem)->size_byte,
+                                 STRING_MULTIBYTE (tem), multibyte);
+             p += nbytes;
            }
        }
       if (p != overlay_str_buf + total)
@@ -2448,8 +2751,10 @@ fix_overlays_in_range (start, end)
          if (startpos > endpos)
            {
              int tem;
-             Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
-             Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
+             Fset_marker (OVERLAY_START (overlay), make_number (endpos),
+                          Qnil);
+             Fset_marker (OVERLAY_END (overlay), make_number (startpos),
+                          Qnil);
              tem = startpos; startpos = endpos; endpos = tem;
            }
          /* Add it to the end of the wrong list.  Later on,
@@ -2482,8 +2787,10 @@ fix_overlays_in_range (start, end)
          if (startpos > endpos)
            {
              int tem;
-             Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
-             Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
+             Fset_marker (OVERLAY_START (overlay), make_number (endpos),
+                          Qnil);
+             Fset_marker (OVERLAY_END (overlay), make_number (startpos),
+                          Qnil);
              tem = startpos; startpos = endpos; endpos = tem;
            }
          if (endpos < XINT (current_buffer->overlay_center))
@@ -2514,6 +2821,82 @@ fix_overlays_in_range (start, end)
   recenter_overlay_lists (current_buffer,
                          XINT (current_buffer->overlay_center));
 }
+
+/* We have two types of overlay: the one whose ending marker is
+   after-insertion-marker (this is the usual case) and the one whose
+   ending marker is before-insertion-marker.  When `overlays_before'
+   contains overlays of the latter type and the former type in this
+   order and both overlays end at inserting position, inserting a text
+   increases only the ending marker of the latter type, which results
+   in incorrect ordering of `overlays_before'.
+
+   This function fixes ordering of overlays in the slot
+   `overlays_before' of the buffer *BP.  Before the insertion, `point'
+   was at PREV, and now is at POS.  */
+
+void
+fix_overlays_before (bp, prev, pos)
+     struct buffer *bp;
+     int prev, pos;
+{
+  Lisp_Object *tailp = &bp->overlays_before;
+  Lisp_Object *right_place;
+  int end;
+
+  /* After the insertion, the several overlays may be in incorrect
+     order.  The possibility is that, in the list `overlays_before',
+     an overlay which ends at POS appears after an overlay which ends
+     at PREV.  Since POS is greater than PREV, we must fix the
+     ordering of these overlays, by moving overlays ends at POS before
+     the overlays ends at PREV.  */
+
+  /* At first, find a place where disordered overlays should be linked
+     in.  It is where an overlay which end before POS exists. (i.e. an
+     overlay whose ending marker is after-insertion-marker if disorder
+     exists).  */
+  while (!NILP (*tailp)
+        && ((end = OVERLAY_POSITION (OVERLAY_END (XCONS (*tailp)->car)))
+            >= pos))
+    tailp = &XCONS (*tailp)->cdr;
+
+  /* If we don't find such an overlay,
+     or the found one ends before PREV,
+     or the found one is the last one in the list,
+     we don't have to fix anything.  */
+  if (NILP (*tailp)
+      || end < prev
+      || NILP (XCONS (*tailp)->cdr))
+    return;
+
+  right_place = tailp;
+  tailp = &XCONS (*tailp)->cdr;
+
+  /* Now, end position of overlays in the list *TAILP should be before
+     or equal to PREV.  In the loop, an overlay which ends at POS is
+     moved ahead to the place pointed by RIGHT_PLACE.  If we found an
+     overlay which ends before PREV, the remaining overlays are in
+     correct order.  */
+  while (!NILP (*tailp))
+    {
+      end = OVERLAY_POSITION (OVERLAY_END (XCONS (*tailp)->car));
+
+      if (end == pos)
+       {                       /* This overlay is disordered. */
+         Lisp_Object found = *tailp;
+
+         /* Unlink the found overlay.  */
+         *tailp = XCONS (found)->cdr;
+         /* Move an overlay at RIGHT_PLACE to the next of the found one.  */
+         XCONS (found)->cdr = *right_place;
+         /* Link it into the right place.  */
+         *right_place = found;
+       }
+      else if (end == prev)
+       tailp = &XCONS (*tailp)->cdr;
+      else                     /* No more disordered overlay. */
+       break;
+    }
+}
 \f
 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
   "Return t if OBJECT is an overlay.")
@@ -2633,10 +3016,12 @@ modify_overlay (buf, start, end)
        }
     }
 
-  ++OVERLAY_MODIFF;
+  ++BUF_OVERLAY_MODIFF (buf);
 }
 
 \f\f
+Lisp_Object Fdelete_overlay ();
+
 DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
   "Set the endpoints of OVERLAY to BEG and END in BUFFER.\n\
 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.\n\
@@ -2687,15 +3072,13 @@ buffer.")
       /* Redisplay where the overlay was.  */
       if (!NILP (obuffer))
        {
-         Lisp_Object o_beg;
-         Lisp_Object o_end;
+         int o_beg;
+         int o_end;
 
-         o_beg = OVERLAY_START (overlay);
-         o_end = OVERLAY_END   (overlay);
-         o_beg = OVERLAY_POSITION (o_beg);
-         o_end = OVERLAY_POSITION (o_end);
+         o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
+         o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
 
-         modify_overlay (ob, XINT (o_beg), XINT (o_end));
+         modify_overlay (ob, o_beg, o_end);
        }
 
       /* Redisplay where the overlay is going to be.  */
@@ -2704,24 +3087,21 @@ buffer.")
   else
     /* Redisplay the area the overlay has just left, or just enclosed.  */
     {
-      Lisp_Object o_beg;
-      Lisp_Object o_end;
+      int o_beg, o_end;
       int change_beg, change_end;
 
-      o_beg = OVERLAY_START (overlay);
-      o_end = OVERLAY_END   (overlay);
-      o_beg = OVERLAY_POSITION (o_beg);
-      o_end = OVERLAY_POSITION (o_end);
+      o_beg = OVERLAY_POSITION (OVERLAY_START (overlay));
+      o_end = OVERLAY_POSITION (OVERLAY_END (overlay));
 
-      if (XINT (o_beg) == XINT (beg))
-       modify_overlay (b, XINT (o_end), XINT (end));
-      else if (XINT (o_end) == XINT (end))
-       modify_overlay (b, XINT (o_beg), XINT (beg));
+      if (o_beg == XINT (beg))
+       modify_overlay (b, o_end, XINT (end));
+      else if (o_end == XINT (end))
+       modify_overlay (b, o_beg, XINT (beg));
       else
        {
-         if (XINT (beg) < XINT (o_beg)) o_beg = beg;
-         if (XINT (end) > XINT (o_end)) o_end = end;
-         modify_overlay (b, XINT (o_beg), XINT (o_end));
+         if (XINT (beg) < o_beg) o_beg = XINT (beg);
+         if (XINT (end) > o_end) o_end = XINT (end);
+         modify_overlay (b, o_beg, o_end);
        }
     }
 
@@ -3364,7 +3744,8 @@ evaporate_overlays (pos)
 }
 \f
 /* Somebody has tried to store a value with an unacceptable type
-   into the buffer-local slot with offset OFFSET.  */
+   in the slot with offset OFFSET.  */
+
 void
 buffer_slot_type_mismatch (offset)
      int offset;
@@ -3377,11 +3758,12 @@ buffer_slot_type_mismatch (offset)
     case Lisp_Int:     type_name = "integers";  break;
     case Lisp_String:  type_name = "strings";   break;
     case Lisp_Symbol:  type_name = "symbols";   break;
+
     default:
       abort ();
     }
 
-  error ("only %s should be stored in the buffer-local variable %s",
+  error ("Only %s should be stored in the buffer-local variable %s",
         type_name, XSYMBOL (sym)->name->data);
 }
 \f
@@ -3432,14 +3814,18 @@ init_buffer_once ()
   XSETFASTINT (buffer_defaults.tab_width, 8);
   buffer_defaults.truncate_lines = Qnil;
   buffer_defaults.ctl_arrow = Qt;
+  buffer_defaults.direction_reversed = Qnil;
 
 #ifdef DOS_NT
   buffer_defaults.buffer_file_type = Qnil; /* TEXT */
 #endif
+  buffer_defaults.enable_multibyte_characters = Qt;
+  buffer_defaults.buffer_file_coding_system = Qnil;
   XSETFASTINT (buffer_defaults.fill_column, 70);
   XSETFASTINT (buffer_defaults.left_margin, 0);
   buffer_defaults.cache_long_line_scans = Qnil;
   buffer_defaults.file_truename = Qnil;
+  XSETFASTINT (buffer_defaults.display_count, 0);
 
   /* Assign the local-flags to the slots that have default values.
      The local flag is a bit that is used in the buffer
@@ -3465,6 +3851,8 @@ init_buffer_once ()
   XSETINT (buffer_local_flags.file_truename, -1);
   XSETINT (buffer_local_flags.invisibility_spec, -1);
   XSETINT (buffer_local_flags.file_format, -1);
+  XSETINT (buffer_local_flags.display_count, -1);
+  XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
 
   XSETFASTINT (buffer_local_flags.mode_line_format, 1);
   XSETFASTINT (buffer_local_flags.abbrev_mode, 2);
@@ -3489,7 +3877,12 @@ init_buffer_once ()
 #endif
   XSETFASTINT (buffer_local_flags.syntax_table, 0x8000);
   XSETFASTINT (buffer_local_flags.cache_long_line_scans, 0x10000);
-
+  XSETFASTINT (buffer_local_flags.category_table, 0x20000);
+  XSETFASTINT (buffer_local_flags.direction_reversed, 0x40000);
+  XSETFASTINT (buffer_local_flags.buffer_file_coding_system, 0x80000);
+  /* Make this one a permanent local.  */
+  buffer_permanent_local_flags |= 0x80000;
+  
   Vbuffer_alist = Qnil;
   current_buffer = 0;
   all_buffers = 0;
@@ -3534,8 +3927,13 @@ init_buffer ()
       && dotstat.st_dev == pwdstat.st_dev
       && strlen (pwd) < MAXPATHLEN)
     strcpy (buf, pwd);
+#ifdef HAVE_GETCWD
+  else if (getcwd (buf, MAXPATHLEN+1) == 0)
+    fatal ("`getcwd' failed: %s\n", strerror (errno));
+#else
   else if (getwd (buf) == 0)
     fatal ("`getwd' failed: %s\n", buf);
+#endif
 
 #ifndef VMS
   /* Maybe this should really use some standard subroutine
@@ -3553,7 +3951,12 @@ init_buffer ()
   /* Add /: to the front of the name
      if it would otherwise be treated as magic.  */
   temp = Ffind_file_name_handler (current_buffer->directory, Qt);
-  if (! NILP (temp))
+  if (! NILP (temp)
+      /* If the default dir is just /, TEMP is non-nil
+        because of the ange-ftp completion handler.
+        However, it is not necessary to turn / into /:/.
+        So avoid doing that.  */
+      && strcmp ("/", XSTRING (current_buffer->directory)->data))
     current_buffer->directory
       = concat2 (build_string ("/:"), current_buffer->directory);
 
@@ -3629,6 +4032,21 @@ This is the same as (default-value 'abbrev-mode).");
     "Default value of `ctl-arrow' for buffers that do not override it.\n\
 This is the same as (default-value 'ctl-arrow).");
 
+   DEFVAR_LISP_NOPRO ("default-direction-reversed",
+             &buffer_defaults.direction_reversed,
+     "Default value of `direction_reversed' for buffers that do not override it.\n\
+ This is the same as (default-value 'direction-reversed).");
+   DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
+             &buffer_defaults.enable_multibyte_characters,
+     "Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
+ This is the same as (default-value 'enable-multibyte-characters).");
+   DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
+             &buffer_defaults.buffer_file_coding_system,
+     "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
+ This is the same as (default-value 'buffer-file-coding-system).");
   DEFVAR_LISP_NOPRO ("default-truncate-lines",
              &buffer_defaults.truncate_lines,
     "Default value of `truncate-lines' for buffers that do not override it.\n\
@@ -3687,11 +4105,14 @@ A string is printed verbatim in the mode line except for %-constructs:\n\
   (%-constructs are allowed when the string is the entire mode-line-format\n\
    or when it is found in a cons-cell or a list)\n\
   %b -- print buffer name.      %f -- print visited file name.\n\
+  %F -- print frame name.\n\
   %* -- print %, * or hyphen.   %+ -- print *, % or hyphen.\n\
        % means buffer is read-only and * means it is modified.\n\
        For a modified read-only buffer, %* gives % and %+ gives *.\n\
   %s -- print process status.   %l -- print the current line number.\n\
   %c -- print the current column number (this makes editing slower).\n\
+        To make the column number update correctly in all cases,\n\
+       `column-number-mode' must be non-nil.\n\
   %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
   %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
         or print Bottom or All.\n\
@@ -3741,11 +4162,32 @@ Automatically becomes buffer-local when set in any fashion.");
 
   DEFVAR_PER_BUFFER ("ctl-arrow", &current_buffer->ctl_arrow, Qnil,
     "*Non-nil means display control chars with uparrow.\n\
-Nil means use backslash and octal digits.\n\
+A value of nil means use backslash and octal digits.\n\
 Automatically becomes buffer-local when set in any fashion.\n\
 This variable does not apply to characters whose display is specified\n\
 in the current display table (if there is one).");
 
+  DEFVAR_PER_BUFFER ("enable-multibyte-characters",
+                    &current_buffer->enable_multibyte_characters,
+                    make_number (-1),
+    "*Non-nil means the buffer contents are regarded as multi-byte form\n\
+of characters, not a binary code.  This affects the display, file I/O,\n\
+and behaviors of various editing commands.");
+
+  DEFVAR_PER_BUFFER ("buffer-file-coding-system",
+                    &current_buffer->buffer_file_coding_system, Qnil,
+    "Coding system to be used for encoding the buffer contents on saving.\n\
+If it is nil, the buffer is saved without any code conversion unless\n\
+some coding system is specified in file-coding-system-alist\n\
+for the buffer file.\n\
+\n\
+This variable is never applied to a way of decoding\n\
+a file while reading it.");
+
+  DEFVAR_PER_BUFFER ("direction-reversed", &current_buffer->direction_reversed,
+                    Qnil,
+    "*Non-nil means lines in the buffer are displayed right to left.");
+
   DEFVAR_PER_BUFFER ("truncate-lines", &current_buffer->truncate_lines, Qnil,
     "*Non-nil means do not display continuation lines;\n\
 give each line of text one screen line.\n\
@@ -3891,7 +4333,7 @@ Three arguments are passed to the function: the positions of\n\
 the beginning and end of the range of changed text,\n\
 and the length of the pre-change text replaced by that range.\n\
 \(For an insertion, the pre-change length is zero;\n\
-for a deletion, that length is the number of characters deleted,\n\
+for a deletion, that length is the number of bytes deleted,\n\
 and the post-change beginning and end are at the same place.)\n\
 \n\
 Buffer changes made while executing the `after-change-function'\n\
@@ -3922,9 +4364,9 @@ accomplishing an equivalent result by using other variables.");
               "List of function to call after each text change.\n\
 Three arguments are passed to each function: the positions of\n\
 the beginning and end of the range of changed text,\n\
-and the length of the pre-change text replaced by that range.\n\
+and the length in bytes of the pre-change text replaced by that range.\n\
 \(For an insertion, the pre-change length is zero;\n\
-for a deletion, that length is the number of characters deleted,\n\
+for a deletion, that length is the number of bytes deleted,\n\
 and the post-change beginning and end are at the same place.)\n\
 \n\
 Buffer changes made while executing the `after-change-functions'\n\
@@ -4031,6 +4473,10 @@ If an element is a cons cell of the form (PROP . ELLIPSIS),\n\
 then characters with property value PROP are invisible,\n\
 and they have an ellipsis as well if ELLIPSIS is non-nil.");
 
+  DEFVAR_PER_BUFFER ("buffer-display-count",
+                    &current_buffer->display_count, Qnil,
+  "A number incremented each time the buffer is displayed in a window.");
+
   DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
     "*Non-nil means deactivate the mark when the buffer contents change.\n\
 Non-nil also enables highlighting of the region whenever the mark is active.\n\
@@ -4070,7 +4516,6 @@ is a member of the list.");
   defsubr (&Sbuffer_disable_undo);
   defsubr (&Sbuffer_enable_undo);
   defsubr (&Skill_buffer);
-  defsubr (&Serase_buffer);
   defsubr (&Sset_buffer_major_mode);
   defsubr (&Sswitch_to_buffer);
   defsubr (&Spop_to_buffer);
@@ -4078,6 +4523,8 @@ is a member of the list.");
   defsubr (&Sset_buffer);
   defsubr (&Sbarf_if_buffer_read_only);
   defsubr (&Sbury_buffer);
+  defsubr (&Serase_buffer);
+  defsubr (&Sset_buffer_multibyte);
   defsubr (&Skill_all_local_variables);
 
   defsubr (&Soverlayp);