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"
18 #include "BufferParams.h"
19 #include "DocIterator.h"
21 #include "ErrorList.h"
23 #include "FloatList.h"
24 #include "InsetList.h"
30 #include "TextClass.h"
31 #include "Paragraph.h"
32 #include "ParagraphList.h"
33 #include "ParagraphParameters.h"
34 #include "ParIterator.h"
37 #include "TocBackend.h"
39 #include "frontends/alert.h"
41 #include "insets/InsetBibitem.h"
42 #include "insets/InsetInclude.h"
44 #include "support/lassert.h"
45 #include "support/convert.h"
46 #include "support/debug.h"
47 #include "support/filetools.h"
48 #include "support/gettext.h"
49 #include "support/lstrings.h"
50 #include "support/mutex.h"
51 #include "support/textutils.h"
54 using namespace lyx::support;
58 namespace Alert = frontend::Alert;
61 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
64 Buffer * checkBuffer = theBufferList().getBuffer(filename);
66 // Sometimes (when setting the master buffer from a child)
67 // we accept a dirty buffer right away (otherwise we'd get
68 // an infinite loop (bug 5514).
69 // We also accept a dirty buffer when the document has not
70 // yet been saved to disk.
71 if (checkBuffer->isClean() || acceptDirty || !filename.exists())
73 docstring const file = makeDisplayPath(filename.absFileName(), 20);
74 docstring const text = bformat(_(
75 "The document %1$s is already loaded and has unsaved changes.\n"
76 "Do you want to abandon your changes and reload the version on disk?"), file);
77 int res = Alert::prompt(_("Reload saved document?"),
78 text, 2, 2, _("Yes, &Reload"), _("No, &Keep Changes"), _("&Cancel"));
81 // reload the document
82 if (checkBuffer->reload() != Buffer::ReadSuccess)
95 bool const exists = filename.exists();
96 bool const tryVC = exists ? false : LyXVC::fileInVC(filename);
97 if (exists || tryVC) {
99 if (!filename.isReadableFile()) {
100 docstring text = bformat(_("The file %1$s exists but is not "
101 "readable by the current user."),
102 from_utf8(filename.absFileName()));
103 Alert::error(_("File not readable!"), text);
106 if (filename.extension() == "lyx" && filename.isFileEmpty()) {
107 // Makes it possible to open an empty (0 bytes) .lyx file
108 return newFile(filename.absFileName(), "", true);
111 Buffer * b = theBufferList().newBuffer(filename.absFileName());
113 // Buffer creation is not possible.
116 if (b->loadLyXFile() != Buffer::ReadSuccess) {
117 // do not save an emergency file when releasing the buffer
119 theBufferList().release(b);
125 docstring text = bformat(_("The document %1$s does not yet "
126 "exist.\n\nDo you want to create a new document?"),
127 from_utf8(filename.absFileName()));
128 if (!Alert::prompt(_("Create new document?"),
129 text, 0, 1, _("&Yes, Create New Document"), _("&No, Do Not Create")))
130 return newFile(filename.absFileName(), string(), true);
136 // FIXME newFile() should probably be a member method of Application...
137 Buffer * newFile(string const & filename, string const & templatename,
141 Buffer * b = theBufferList().newBuffer(filename);
143 // Buffer creation is not possible.
147 // use defaults.lyx as a default template if it exists.
148 if (templatename.empty())
149 tname = libFileSearch("templates", "defaults.lyx");
151 tname = makeAbsPath(templatename);
153 if (!tname.empty()) {
154 if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
155 docstring const file = makeDisplayPath(tname.absFileName(), 50);
156 docstring const text = bformat(
157 _("The specified document template\n%1$s\ncould not be read."),
159 Alert::error(_("Could not read template"), text);
160 theBufferList().release(b);
166 // in this case, the user chose the filename, so we
167 // assume that she really does want this file.
172 b->setReadonly(false);
173 b->setFullyLoaded(true);
179 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
180 string const & templatename)
182 static map<string, int> file_number;
185 Mutex::Locker locker(&mutex);
190 prefix + convert<string>(++file_number[prefix]) + ".lyx");
192 while (theBufferList().exists(filename) || filename.isReadableFile());
194 return newFile(filename.absFileName(), templatename, false);
198 Buffer * loadIfNeeded(FileName const & fname)
200 Buffer * buffer = theBufferList().getBuffer(fname);
202 if (!fname.exists() && !LyXVC::fileInVC(fname))
205 buffer = theBufferList().newBuffer(fname.absFileName());
207 // Buffer creation is not possible.
210 if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
211 //close the buffer we just opened
212 theBufferList().release(buffer);