Index: gedit/gedit/gedit-document.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.c,v
retrieving revision 1.69
diff -u -p -d -b -r1.69 gedit-document.c
--- gedit/gedit/gedit-document.c	3 Jun 2003 13:27:37 -0000	1.69
+++ gedit/gedit/gedit-document.c	6 Jun 2003 20:45:13 -0000
@@ -1168,6 +1168,7 @@ gedit_document_save_as_real (GeditDocume
 	GeditSaveEncodingSetting encoding_setting;
 	gboolean res;
 	gboolean add_cr;
+	gboolean create_temp_file;
 	
 	gedit_debug (DEBUG_DOCUMENT, "");
 
@@ -1176,6 +1177,7 @@ gedit_document_save_as_real (GeditDocume
 	g_return_val_if_fail (uri != NULL, FALSE);
 
 	retval = FALSE;
+	create_temp_file = TRUE;
 
 	filename = NULL;
 	real_filename = NULL;
@@ -1202,8 +1204,10 @@ gedit_document_save_as_real (GeditDocume
 			g_free (scheme_string);
 		}
 		else
+		{
 			error_message = g_strdup_printf (
 				_("gedit cannot handle this kind of location in write mode."));
+		}
 
 		g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, EROFS, error_message);
 		g_free (error_message);
@@ -1221,14 +1225,12 @@ gedit_document_save_as_real (GeditDocume
 	}
 
 	/* Get the real filename and file permissions */
-
 	real_filename = follow_symlinks (filename, error);
 
 	if (!real_filename)
 		goto out;
 
 	/* Get the directory in which the real filename lives */
-
 	slashpos = strrchr (real_filename, G_DIR_SEPARATOR);
 
 	if (slashpos)
@@ -1237,7 +1239,9 @@ gedit_document_save_as_real (GeditDocume
 		dirname[slashpos - real_filename + 1] = '\0';
 	}
 	else
+	{
 		dirname = g_strdup (".");
+	}
 
 	/* If there is not an existing file with that name, compute the
 	 * permissions and uid/gid that we will use for the newly-created file.
@@ -1276,14 +1280,32 @@ gedit_document_save_as_real (GeditDocume
 	fd = g_mkstemp (temp_filename); /* this modifies temp_filename to the used name */
 	umask (saved_umask);
 
-	if (fd == -1)
+
+	/* If we can't open the temporary file then we also won't be able
+	 * create a backup file. We instead open and truncate original file
+	 * and overwrite it with revised data.  This follows the behavior
+	 * of nedit. We anticipate offering greater control in a future
+	 * release, following the precedent set by vim.
+	 * CHECK ME - does above umask screw up file permissions?
+	 */
+
+	if (fd == -1) /* open original file and truncate it to zero length */
 	{
-		g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, errno, " ");
-		goto out;
+		fd = open (real_filename,
+				O_WRONLY | O_TRUNC,
+				S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+		gedit_debug (DEBUG_DOCUMENT,
+			"Opened and truncated file to zero length: %s",
+			real_filename);
+
+		create_temp_file = FALSE;
 	}
 
-	chars = gedit_document_get_chars (doc, 0, -1);
+	gedit_debug (DEBUG_DOCUMENT, "create_temp_file = %s",
+		     (create_temp_file == TRUE) ? "TRUE" : "FALSE");
 
+	chars = gedit_document_get_chars (doc, 0, -1);
 	encoding_setting = gedit_prefs_manager_get_save_encoding ();
 
 	if (encoding_setting == GEDIT_SAVE_CURRENT_LOCALE_IF_POSSIBLE)
@@ -1386,8 +1408,7 @@ gedit_document_save_as_real (GeditDocume
 	}
 
 	/* Move the original file to a backup */
-
-	if (create_backup_copy)
+	if (create_backup_copy && create_temp_file)
 	{
 		gchar *bak_ext;
 		gint result;
@@ -1411,8 +1432,9 @@ gedit_document_save_as_real (GeditDocume
 		}
 	}
 
+	if (create_temp_file)
+	{
 	/* Move the temp file to the original file */
-
 	if (rename (temp_filename, real_filename) != 0)
 	{
 		gint saved_errno;
@@ -1428,6 +1450,7 @@ gedit_document_save_as_real (GeditDocume
 
 		goto out;
 	}
+	}
 
 	/* Restore permissions.  There is not much error checking we can do
 	 * here, I'm afraid.  The final data is saved anyways.
