}
-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
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