]> git.lyx.org Git - lyx.git/blobdiff - src/buffer_funcs.cpp
Correct the mess introduced in r33250.
[lyx.git] / src / buffer_funcs.cpp
index 7d23306d2ba35b0b11ae01f948d33fcc4ec67703..8224241ee1db2a4a871648da302dc2cb653ca8bb 100644 (file)
@@ -28,7 +28,6 @@
 #include "LyX.h"
 #include "TextClass.h"
 #include "Paragraph.h"
-#include "paragraph_funcs.h"
 #include "ParagraphList.h"
 #include "ParagraphParameters.h"
 #include "ParIterator.h"
@@ -76,6 +75,7 @@ Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
                        return checkBuffer;
 
                // FIXME: should be LFUN_REVERT
+               checkBuffer->markClean();
                theBufferList().release(checkBuffer);
                // Load it again.
                return checkAndLoadLyXFile(filename);
@@ -153,21 +153,23 @@ Buffer * newFile(string const & filename, string const & templatename,
 }
 
 
-Buffer * newUnnamedFile(string const & templatename, FileName const & path)
+Buffer * newUnnamedFile(FileName const & path, string const & prefix,
+                                               string const & templatename)
 {
-       static int newfile_number;
-
-       FileName filename(path, 
-               "newfile" + convert<string>(++newfile_number) + ".lyx");
-       while (theBufferList().exists(filename)
-               || filename.isReadableFile()) {
-               ++newfile_number;
-               filename.set(path,
-                       "newfile" +     convert<string>(newfile_number) + ".lyx");
+       static map<string, int> file_number;
+
+       FileName filename;
+
+       do {
+               filename.set(path, 
+                       prefix + convert<string>(++file_number[prefix]) + ".lyx");
        }
+       while (theBufferList().exists(filename) || filename.isReadableFile());
+               
        return newFile(filename.absFilename(), templatename, false);
 }
 
+
 /* 
  * FIXME : merge with countChars. The structures of the two functions
  * are similar but, unfortunately, they seem to have a different
@@ -248,4 +250,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