2 * \file buffer_funcs.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Alfredo Braunstein
9 * Full author contact details are available in file CREDITS.
15 #include "buffer_funcs.h"
17 #include "BufferList.h"
20 #include "frontends/alert.h"
22 #include "support/lassert.h"
23 #include "support/convert.h"
24 #include "support/debug.h"
25 #include "support/FileName.h"
26 #include "support/filetools.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29 #include "support/mutex.h"
32 using namespace lyx::support;
36 namespace Alert = frontend::Alert;
39 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
42 Buffer * checkBuffer = theBufferList().getBuffer(filename);
44 // Sometimes (when setting the master buffer from a child)
45 // we accept a dirty buffer right away (otherwise we'd get
46 // an infinite loop (bug 5514).
47 // We also accept a dirty buffer when the document has not
48 // yet been saved to disk.
49 if (checkBuffer->isClean() || acceptDirty || !filename.exists())
51 docstring const file = makeDisplayPath(filename.absFileName(), 20);
52 docstring const text = bformat(_(
53 "The document %1$s is already loaded and has unsaved changes.\n"
54 "Do you want to abandon your changes and reload the version on disk?"), file);
55 int res = Alert::prompt(_("Reload saved document?"),
56 text, 2, 2, _("Yes, &Reload"), _("No, &Keep Changes"), _("&Cancel"));
59 // reload the document
60 if (checkBuffer->reload() != Buffer::ReadSuccess)
73 bool const exists = filename.exists();
74 bool const tryVC = exists ? false : LyXVC::fileInVC(filename);
75 if (exists || tryVC) {
77 if (!filename.isReadableFile()) {
78 docstring text = bformat(_("The file %1$s exists but is not "
79 "readable by the current user."),
80 from_utf8(filename.absFileName()));
81 Alert::error(_("File not readable!"), text);
84 if (filename.extension() == "lyx" && filename.isFileEmpty()) {
85 // Makes it possible to open an empty (0 bytes) .lyx file
86 return newFile(filename.absFileName(), "", true);
89 Buffer * b = theBufferList().newBuffer(filename.absFileName());
91 // Buffer creation is not possible.
94 if (b->loadLyXFile() != Buffer::ReadSuccess) {
95 // do not save an emergency file when releasing the buffer
97 theBufferList().release(b);
103 docstring text = bformat(_("The document %1$s does not yet "
104 "exist.\n\nDo you want to create a new document?"),
105 from_utf8(filename.absFileName()));
106 if (!Alert::prompt(_("Create new document?"),
107 text, 0, 1, _("&Yes, Create New Document"), _("&No, Do Not Create")))
108 return newFile(filename.absFileName(), string(), true);
114 // FIXME newFile() should probably be a member method of Application...
115 Buffer * newFile(string const & filename, string const & templatename,
119 Buffer * b = theBufferList().newBuffer(filename);
121 // Buffer creation is not possible.
125 // use defaults.lyx as a default template if it exists.
126 if (templatename.empty())
127 tname = libFileSearch("templates", "defaults.lyx");
129 tname = makeAbsPath(templatename);
131 if (!tname.empty()) {
132 if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
133 docstring const file = makeDisplayPath(tname.absFileName(), 50);
134 docstring const text = bformat(
135 _("The specified document template\n%1$s\ncould not be read."),
137 Alert::error(_("Could not read template"), text);
138 theBufferList().release(b);
144 // in this case, the user chose the filename, so we
145 // assume that she really does want this file.
150 b->setReadonly(false);
151 b->setFullyLoaded(true);
157 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
158 string const & templatename)
160 static map<string, int> file_number;
163 Mutex::Locker locker(&mutex);
168 prefix + convert<string>(++file_number[prefix]) + ".lyx");
170 while (theBufferList().exists(filename) || filename.isReadableFile());
172 return newFile(filename.absFileName(), templatename, false);
176 Buffer * loadIfNeeded(FileName const & fname)
178 Buffer * buffer = theBufferList().getBuffer(fname);
180 if (!fname.exists() && !LyXVC::fileInVC(fname))
183 buffer = theBufferList().newBuffer(fname.absFileName());
185 // Buffer creation is not possible.
188 if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
189 //close the buffer we just opened
190 theBufferList().release(buffer);