]> git.lyx.org Git - features.git/blobdiff - src/buffer_funcs.cpp
Accept dirty buffer if the document has not been saved to disk (part of bug 6645).
[features.git] / src / buffer_funcs.cpp
index 3115f4a356527fec91fbcb0c5fe30dc90e7d80bf..dc51c93835c394dd24fa8d272b2450cf2b8afe45 100644 (file)
@@ -61,23 +61,24 @@ Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
        // File already open?
        Buffer * checkBuffer = theBufferList().getBuffer(filename);
        if (checkBuffer) {
-               // sometimes (when setting the master buffer from a child)
+               // Sometimes (when setting the master buffer from a child)
                // we accept a dirty buffer right away (otherwise we'd get
-               // an infinite loop (bug 5514)
-               if (checkBuffer->isClean() || acceptDirty)
+               // an infinite loop (bug 5514).
+               // We also accept a dirty buffer when the document has not
+               // yet been saved to disk.
+               if (checkBuffer->isClean() || acceptDirty || !filename.exists())
                        return checkBuffer;
                docstring const file = makeDisplayPath(filename.absFilename(), 20);
-               docstring text = bformat(_(
+               docstring const text = bformat(_(
                                "The document %1$s is already loaded and has unsaved changes.\n"
                                "Do you want to abandon your changes and reload the version on disk?"), file);
-               if (Alert::prompt(_("Reload saved document?"),
-                               text, 0, 1,  _("&Reload"), _("&Keep Changes")))
-                       return checkBuffer;
-
-               // FIXME: should be LFUN_REVERT
-               theBufferList().release(checkBuffer);
-               // Load it again.
-               return checkAndLoadLyXFile(filename);
+               if (!Alert::prompt(_("Reload saved document?"),
+                         text, 0, 1,  _("&Reload"), _("&Keep Changes"))) {
+                       // reload the document
+                       if (!checkBuffer->reload())
+                               return 0;
+               }
+               return checkBuffer;
        }
 
        if (filename.exists()) {
@@ -94,6 +95,8 @@ Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
                        return 0;
                }
                if (!b->loadLyXFile(filename)) {
+                       // do not save an emergency file when releasing the buffer
+                       b->markClean();
                        theBufferList().release(b);
                        return 0;
                }
@@ -165,7 +168,7 @@ Buffer * newUnnamedFile(FileName const & path, string const & prefix,
        }
        while (theBufferList().exists(filename) || filename.isReadableFile());
                
-       return newFile(filename.absFilename(), "", false);
+       return newFile(filename.absFilename(), templatename, false);
 }
 
 
@@ -249,4 +252,27 @@ int countChars(DocIterator const & from, DocIterator const & to,
        return chars + blanks;
 }
 
+
+Buffer * loadIfNeeded(FileName const & fname)
+{
+       Buffer * buffer = theBufferList().getBuffer(fname);
+       if (!buffer) {
+               if (!fname.exists())
+                       return 0;
+
+               buffer = theBufferList().newBuffer(fname.absFilename());
+               if (!buffer)
+                       // Buffer creation is not possible.
+                       return 0;
+
+               if (!buffer->loadLyXFile(fname)) {
+                       //close the buffer we just opened
+                       theBufferList().release(buffer);
+                       return 0;
+               }
+       }
+       return buffer;
+}
+
+
 } // namespace lyx