changeset 6036:7e9984c9a36e

(Finsert_file_contents): If REPLACE, always do lseek. Exit main loop if INSERTED reaches TOTAL.
author Richard M. Stallman <rms@gnu.org>
date Wed, 23 Feb 1994 00:33:13 +0000
parents a96a9f6cb250
children 324bb3410cfb
files src/fileio.c
diffstat 1 files changed, 29 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/fileio.c	Wed Feb 23 00:29:43 1994 +0000
+++ b/src/fileio.c	Wed Feb 23 00:33:13 1994 +0000
@@ -154,6 +154,15 @@
 {
   close (XFASTINT (fd));
 }
+
+/* Restore point, having saved it as a marker.  */
+
+restore_point_unwind (location)
+     Lisp_Object location; 
+{
+  SET_PT (marker_position (location));
+  Fset_marker (location, Qnil, Qnil);
+}
 
 Lisp_Object Qexpand_file_name;
 Lisp_Object Qdirectory_file_name;
@@ -2551,6 +2560,10 @@
       goto notfound;
     }
 
+  /* Replacement should preserve point as it preserves markers.  */
+  if (!NILP (replace))
+    record_unwind_protect (restore_point_unwind, Fpoint_marker ());
+
   record_unwind_protect (close_file_unwind, make_number (fd));
 
 #ifdef S_IFSOCK
@@ -2623,7 +2636,7 @@
       if (same_at_start == ZV)
 	{
 	  close (fd);
-	  specpdl_ptr = specpdl + count;
+	  specpdl_ptr--;
 	  goto handled;
 	}
       immediate_quit = 1;
@@ -2670,6 +2683,8 @@
       XFASTINT (end) = st.st_size - (ZV - same_at_end);
       /* Delete the nonmatching middle part of the buffer.  */
       Fdelete_region (make_number (same_at_start), make_number (same_at_end));
+      /* Insert from the file at the proper position.  */
+      SET_PT (same_at_start);
     }
 
   total = XINT (end) - XINT (beg);
@@ -2690,13 +2705,14 @@
   if (GAP_SIZE < total)
     make_gap (total - GAP_SIZE);
 
-  if (XINT (beg) != 0)
+  if (XINT (beg) != 0 || !NILP (replace))
     {
       if (lseek (fd, XINT (beg), 0) < 0)
 	report_file_error ("Setting file position", Fcons (filename, Qnil));
     }
 
-  while (1)
+  how_much = 0;
+  while (inserted < total)
     {
       int try = min (total - inserted, 64 << 10);
       int this;
@@ -2734,8 +2750,8 @@
       XFASTINT (current_buffer->buffer_file_type) = XFASTINT (code);
     if (XFASTINT (current_buffer->buffer_file_type) == 0)
       {
-	int reduced_size = 
-	  inserted - crlf_to_lf (inserted, &FETCH_CHAR (point - 1) + 1);
+	int reduced_size
+	  = inserted - crlf_to_lf (inserted, &FETCH_CHAR (point - 1) + 1);
 	ZV -= reduced_size;
 	Z -= reduced_size;
 	GPT -= reduced_size;
@@ -2756,8 +2772,8 @@
 
   close (fd);
 
-  /* Discard the unwind protect */
-  specpdl_ptr = specpdl + count;
+  /* Discard the unwind protect for closing the file.  */
+  specpdl_ptr--;
 
   if (how_much < 0)
     error ("IO error reading %s: %s",
@@ -2814,11 +2830,12 @@
 	}
     }
 
-  if (!NILP (val))
-    RETURN_UNGCPRO (val);
-  RETURN_UNGCPRO (Fcons (filename,
-			 Fcons (make_number (inserted),
-				Qnil)));
+  if (NILP (val))
+    val = Fcons (filename,
+		 Fcons (make_number (inserted),
+			Qnil));
+
+  RETURN_UNGCPRO (unbind_to (count, val));
 }
 
 static Lisp_Object build_annotations ();