(Fsubstitute_in_file_name): Adjusted for the change of
[bpt/emacs.git] / src / fileio.c
index eafb1e6..17be8ac 100644 (file)
@@ -235,9 +235,10 @@ Lisp_Object Qfile_name_history;
 
 Lisp_Object Qcar_less_than_car;
 
-static int a_write P_ ((int, char *, int, int,
+static int a_write P_ ((int, Lisp_Object, int, int,
                        Lisp_Object *, struct coding_system *));
-static int e_write P_ ((int, char *, int, struct coding_system *));
+static int e_write P_ ((int, Lisp_Object, int, int, struct coding_system *));
+
 \f
 void
 report_file_error (string, data)
@@ -2090,21 +2091,10 @@ duplicates what `expand-file-name' does.")
          {
            /* If the original string is multibyte,
               convert what we substitute into multibyte.  */
-           unsigned char workbuf[4], *str;
-           int len;
-
            while (*o)
              {
-               int c = *o++;
-               c = unibyte_char_to_multibyte (c);
-               if (! SINGLE_BYTE_CHAR_P (c))
-                 {
-                   len = CHAR_STRING (c, workbuf, str);
-                   bcopy (str, x, len);
-                   x += len;
-                 }
-               else
-                 *x++ = c;
+               int c = unibyte_char_to_multibyte (*o++);
+               x += CHAR_STRING (c, x);
              }
          }
        else
@@ -3288,22 +3278,44 @@ Lisp_Object Qfind_buffer_file_type;
 #define READ_BUF_SIZE (64 << 10)
 #endif
 
-/* This function is called when a function bound to
-   Vset_auto_coding_function causes some error.  At that time, a text
-   of a file has already been inserted in the current buffer, but,
-   markers has not yet been adjusted.  Thus we must adjust markers
-   here.  We are sure that the buffer was empty before the text of the
-   file was inserted.  */
+extern void adjust_markers_for_delete P_ ((int, int, int, int));
+
+/* This function is called after Lisp functions to decide a coding
+   system are called, or when they cause an error.  Before they are
+   called, the current buffer is set unibyte and it contains only a
+   newly inserted text (thus the buffer was empty before the
+   insertion).
+
+   The functions may set markers, overlays, text properties, or even
+   alter the buffer contents, change the current buffer.
+
+   Here, we reset all those changes by:
+       o set back the current buffer.
+       o move all markers and overlays to BEG.
+       o remove all text properties.
+       o set back the buffer multibyteness.  */
 
 static Lisp_Object
-set_auto_coding_unwind (multibyte)
-     Lisp_Object multibyte;
+decide_coding_unwind (unwind_data)
+     Lisp_Object unwind_data;
 {
-  int inserted = Z_BYTE - BEG_BYTE;
+  Lisp_Object multibyte, undo_list, buffer;
+
+  multibyte = XCAR (unwind_data);
+  unwind_data = XCDR (unwind_data);
+  undo_list = XCAR (unwind_data);
+  buffer = XCDR (unwind_data);
 
-  if (!NILP (multibyte))
-    inserted = multibyte_chars_in_text (GPT_ADDR - inserted, inserted);
-  adjust_after_insert (PT, PT_BYTE, Z, Z_BYTE, inserted);
+  if (current_buffer != XBUFFER (buffer))
+    set_buffer_internal (XBUFFER (buffer));
+  adjust_markers_for_delete (BEG, BEG_BYTE, Z, Z_BYTE);
+  adjust_overlays_for_delete (BEG, Z - BEG);
+  BUF_INTERVALS (current_buffer) = 0;
+  TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
+
+  /* Now we are safe to change the buffer's multibyteness directly.  */
+  current_buffer->enable_multibyte_characters = multibyte;
+  current_buffer->undo_list = undo_list;
 
   return Qnil;
 }
@@ -3666,7 +3678,7 @@ actually used.")
          emacs_close (fd);
          specpdl_ptr--;
          /* Truncate the buffer to the size of the file.  */
-         del_range_1 (same_at_start, same_at_end, 0);
+         del_range_1 (same_at_start, same_at_end, 0, 0);
          goto handled;
        }
       immediate_quit = 1;
@@ -4061,27 +4073,26 @@ actually used.")
        val = Vcoding_system_for_read;
       else
        {
-         if (inserted > 0 && ! NILP (Vset_auto_coding_function))
-           {
-             /* Since we are sure that the current buffer was
-                empty before the insertion, we can toggle
-                enable-multibyte-characters directly here without
-                taking care of marker adjustment and byte
-                combining problem.  */
-             Lisp_Object prev_multibyte;
+         /* Since we are sure that the current buffer was empty
+            before the insertion, we can toggle
+            enable-multibyte-characters directly here without taking
+            care of marker adjustment and byte combining problem.  By
+            this way, we can run Lisp program safely before decoding
+            the inserted text.  */
+         Lisp_Object unwind_data;
              int count = specpdl_ptr - specpdl;
 
-             prev_multibyte = current_buffer->enable_multibyte_characters;
+         unwind_data = Fcons (current_buffer->enable_multibyte_characters,
+                              Fcons (current_buffer->undo_list,
+                                     Fcurrent_buffer ()));
              current_buffer->enable_multibyte_characters = Qnil;
-             record_unwind_protect (set_auto_coding_unwind,
-                                    prev_multibyte);
+         current_buffer->undo_list = Qt;
+         record_unwind_protect (decide_coding_unwind, unwind_data);
+
+         if (inserted > 0 && ! NILP (Vset_auto_coding_function))
+           {
              val = call2 (Vset_auto_coding_function,
                           filename, make_number (inserted));
-             /* Discard the unwind protect for recovering the
-                error of Vset_auto_coding_function.  */
-             specpdl_ptr--;
-             current_buffer->enable_multibyte_characters = prev_multibyte;
-             TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
            }
 
          if (NILP (val))
@@ -4096,6 +4107,9 @@ actually used.")
              if (CONSP (coding_systems))
                val = XCAR (coding_systems);
            }
+
+         unbind_to (count, Qnil);
+         inserted = Z_BYTE - BEG_BYTE;
        }
 
       /* The following kludgy code is to avoid some compiler bug.
@@ -4204,15 +4218,18 @@ actually used.")
       inserted = XFASTINT (insval);
     }
 
+  if (set_coding_system)
+    Vlast_coding_system_used = coding.symbol;
+
   /* Call after-change hooks for the inserted text, aside from the case
      of normal visiting (not with REPLACE), which is done in a new buffer
      "before" the buffer is changed.  */
   if (inserted > 0 && total > 0
       && (NILP (visit) || !NILP (replace)))
-    signal_after_change (PT, 0, inserted);
-
-  if (set_coding_system)
-    Vlast_coding_system_used = coding.symbol;
+    {
+      signal_after_change (PT, 0, inserted);
+      update_compositions (PT, PT, CHECK_BORDER);
+    }
 
   if (inserted > 0)
     {
@@ -4628,9 +4645,8 @@ This does code conversion according to the value of\n\
 
   if (STRINGP (start))
     {
-      failure = 0 > a_write (desc, XSTRING (start)->data,
-                            STRING_BYTES (XSTRING (start)), 0, &annotations,
-                            &coding);
+      failure = 0 > a_write (desc, start, 0, XSTRING (start)->size,
+                            &annotations, &coding);
       save_errno = errno;
     }
   else if (XINT (start) != XINT (end))
@@ -4641,17 +4657,17 @@ This does code conversion according to the value of\n\
 
       if (XINT (start) < GPT)
        {
-         failure = 0 > a_write (desc, BYTE_POS_ADDR (tem),
-                                min (GPT_BYTE, end1) - tem, tem, &annotations,
-                                &coding);
+         failure = 0 > a_write (desc, Qnil, XINT (start),
+                                min (GPT, XINT (end)) - XINT (start),
+                                &annotations, &coding);
          save_errno = errno;
        }
 
       if (XINT (end) > GPT && !failure)
        {
-         tem = max (tem, GPT_BYTE);
-         failure = 0 > a_write (desc, BYTE_POS_ADDR (tem), end1 - tem,
-                                tem, &annotations, &coding);
+         tem = max (XINT (start), GPT);
+         failure = 0 > a_write (desc, Qnil, tem , XINT (end) - tem,
+                                &annotations, &coding);
          save_errno = errno;
        }
     }
@@ -4659,7 +4675,7 @@ This does code conversion according to the value of\n\
     {
       /* If file was empty, still need to write the annotations */
       coding.mode |= CODING_MODE_LAST_BLOCK;
-      failure = 0 > a_write (desc, "", 0, XINT (start), &annotations, &coding);
+      failure = 0 > a_write (desc, Qnil, XINT (end), 0, &annotations, &coding);
       save_errno = errno;
     }
 
@@ -4669,7 +4685,7 @@ This does code conversion according to the value of\n\
     {
       /* We have to flush out a data. */
       coding.mode |= CODING_MODE_LAST_BLOCK;
-      failure = 0 > e_write (desc, "", 0, &coding);
+      failure = 0 > e_write (desc, Qnil, 0, 0, &coding);
       save_errno = errno;
     }
 
@@ -4852,10 +4868,10 @@ build_annotations (start, end, pre_write_conversion)
   return annotations;
 }
 \f
-/* Write to descriptor DESC the NBYTES bytes starting at ADDR,
-   assuming they start at byte position BYTEPOS in the buffer.
+/* Write to descriptor DESC the NCHARS chars starting at POS of STRING.
+   If STRING is nil, POS is the character position in the current buffer.
    Intersperse with them the annotations from *ANNOT
-   which fall within the range of byte positions BYTEPOS to BYTEPOS + NBYTES,
+   which fall within the range of POS to POS + NCHARS,
    each at its appropriate position.
 
    We modify *ANNOT by discarding elements as we use them up.
@@ -4863,44 +4879,42 @@ build_annotations (start, end, pre_write_conversion)
    The return value is negative in case of system call failure.  */
 
 static int
-a_write (desc, addr, nbytes, bytepos, annot, coding)
+a_write (desc, string, pos, nchars, annot, coding)
      int desc;
-     register char *addr;
-     register int nbytes;
-     int bytepos;
+     Lisp_Object string;
+     register int nchars;
+     int pos;
      Lisp_Object *annot;
      struct coding_system *coding;
 {
   Lisp_Object tem;
   int nextpos;
-  int lastpos = bytepos + nbytes;
+  int lastpos = pos + nchars;
 
   while (NILP (*annot) || CONSP (*annot))
     {
       tem = Fcar_safe (Fcar (*annot));
-      nextpos = bytepos - 1;
+      nextpos = pos - 1;
       if (INTEGERP (tem))
-       nextpos = CHAR_TO_BYTE (XFASTINT (tem));
+       nextpos = XFASTINT (tem);
 
       /* If there are no more annotations in this range,
         output the rest of the range all at once.  */
-      if (! (nextpos >= bytepos && nextpos <= lastpos))
-       return e_write (desc, addr, lastpos - bytepos, coding);
+      if (! (nextpos >= pos && nextpos <= lastpos))
+       return e_write (desc, string, pos, lastpos, coding);
 
       /* Output buffer text up to the next annotation's position.  */
-      if (nextpos > bytepos)
+      if (nextpos > pos)
        {
-         if (0 > e_write (desc, addr, nextpos - bytepos, coding))
+         if (0 > e_write (desc, string, pos, nextpos, coding));
            return -1;
-         addr += nextpos - bytepos;
-         bytepos = nextpos;
+         pos = nextpos;
        }
       /* Output the annotation.  */
       tem = Fcdr (Fcar (*annot));
       if (STRINGP (tem))
        {
-         if (0 > e_write (desc, XSTRING (tem)->data, STRING_BYTES (XSTRING (tem)),
-                          coding))
+         if (0 > e_write (desc, tem, 0, XSTRING (tem)->size, coding));
            return -1;
        }
       *annot = Fcdr (*annot);
@@ -4912,17 +4926,45 @@ a_write (desc, addr, nbytes, bytepos, annot, coding)
 #define WRITE_BUF_SIZE (16 * 1024)
 #endif
 
-/* Write NBYTES bytes starting at ADDR into descriptor DESC,
-   encoding them with coding system CODING.  */
+/* Write text in the range START and END into descriptor DESC,
+   encoding them with coding system CODING.  If STRING is nil, START
+   and END are character positions of the current buffer, else they
+   are indexes to the string STRING.  */
 
 static int
-e_write (desc, addr, nbytes, coding)
+e_write (desc, string, start, end, coding)
      int desc;
-     register char *addr;
-     register int nbytes;
+     Lisp_Object string;
+     int start, end;
      struct coding_system *coding;
 {
+  register char *addr;
+  register int nbytes;
   char buf[WRITE_BUF_SIZE];
+  int composing = coding->composing;
+  int return_val = 0;
+
+  if (start >= end)
+    coding->composing = COMPOSITION_DISABLED;
+  if (coding->composing != COMPOSITION_DISABLED)
+    coding_save_composition (coding, start, end, string);
+
+  if (STRINGP (string))
+    {
+      addr = XSTRING (string)->data;
+      nbytes = STRING_BYTES (XSTRING (string));
+    }
+  else if (start < end)
+    {
+      /* It is assured that the gap is not in the range START and END-1.  */
+      addr = CHAR_POS_ADDR (start);
+      nbytes = CHAR_TO_BYTE (end) - CHAR_TO_BYTE (start);
+    }
+  else
+    {
+      addr = "";
+      nbytes = 0;
+    }
 
   /* We used to have a code for handling selective display here.  But,
      now it is handled within encode_coding.  */
@@ -4931,21 +4973,33 @@ e_write (desc, addr, nbytes, coding)
       int result;
 
       result = encode_coding (coding, addr, buf, nbytes, WRITE_BUF_SIZE);
-      nbytes -= coding->consumed, addr += coding->consumed;
       if (coding->produced > 0)
        {
          coding->produced -= emacs_write (desc, buf, coding->produced);
-         if (coding->produced) return -1;
+         if (coding->produced)
+           {
+             return_val = -1;
+             break;
+           }
        }
       if (result == CODING_FINISH_INSUFFICIENT_SRC)
        {
          /* The source text ends by an incomplete multibyte form.
              There's no way other than write it out as is.  */
          nbytes -= emacs_write (desc, addr, nbytes);
-         if (nbytes) return -1;
+         if (nbytes)
+           {
+             return_val = -1;
+             break;
+           }
        }
       if (nbytes <= 0)
        break;
+      nbytes -= coding->consumed;
+      addr += coding->consumed;
+      start += coding->consumed_char;
+      if (coding->cmp_data)
+       coding_adjust_composition_offset (coding, start);
     }
   return 0;
 }