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/textutils.h"
53 using namespace lyx::support;
57 namespace Alert = frontend::Alert;
60 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
63 Buffer * checkBuffer = theBufferList().getBuffer(filename);
65 // Sometimes (when setting the master buffer from a child)
66 // we accept a dirty buffer right away (otherwise we'd get
67 // an infinite loop (bug 5514).
68 // We also accept a dirty buffer when the document has not
69 // yet been saved to disk.
70 if (checkBuffer->isClean() || acceptDirty || !filename.exists())
72 docstring const file = makeDisplayPath(filename.absFileName(), 20);
73 docstring const text = bformat(_(
74 "The document %1$s is already loaded and has unsaved changes.\n"
75 "Do you want to abandon your changes and reload the version on disk?"), file);
76 int res = Alert::prompt(_("Reload saved document?"),
77 text, 2, 2, _("Yes, &Reload"), _("No, &Keep Changes"), _("&Cancel"));
80 // reload the document
81 if (checkBuffer->reload() != Buffer::ReadSuccess)
94 bool const exists = filename.exists();
95 bool const tryVC = exists ? false : LyXVC::fileInVC(filename);
96 if (exists || tryVC) {
97 if (exists && !filename.isReadableFile()) {
98 docstring text = bformat(_("The file %1$s exists but is not "
99 "readable by the current user."),
100 from_utf8(filename.absFileName()));
101 Alert::error(_("File not readable!"), text);
104 Buffer * b = theBufferList().newBuffer(filename.absFileName());
106 // Buffer creation is not possible.
109 if (b->loadLyXFile() != Buffer::ReadSuccess) {
110 // do not save an emergency file when releasing the buffer
112 theBufferList().release(b);
118 docstring text = bformat(_("The document %1$s does not yet "
119 "exist.\n\nDo you want to create a new document?"),
120 from_utf8(filename.absFileName()));
121 if (!Alert::prompt(_("Create new document?"),
122 text, 0, 1, _("&Create"), _("Cancel")))
123 return newFile(filename.absFileName(), string(), true);
129 // FIXME newFile() should probably be a member method of Application...
130 Buffer * newFile(string const & filename, string const & templatename,
134 Buffer * b = theBufferList().newBuffer(filename);
136 // Buffer creation is not possible.
140 // use defaults.lyx as a default template if it exists.
141 if (templatename.empty())
142 tname = libFileSearch("templates", "defaults.lyx");
144 tname = makeAbsPath(templatename);
146 if (!tname.empty()) {
147 if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
148 docstring const file = makeDisplayPath(tname.absFileName(), 50);
149 docstring const text = bformat(
150 _("The specified document template\n%1$s\ncould not be read."),
152 Alert::error(_("Could not read template"), text);
153 theBufferList().release(b);
159 // in this case, the user chose the filename, so we
160 // assume that she really does want this file.
165 b->setReadonly(false);
166 b->setFullyLoaded(true);
172 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
173 string const & templatename)
175 static map<string, int> file_number;
181 prefix + convert<string>(++file_number[prefix]) + ".lyx");
183 while (theBufferList().exists(filename) || filename.isReadableFile());
185 return newFile(filename.absFileName(), templatename, false);
189 Buffer * loadIfNeeded(FileName const & fname)
191 Buffer * buffer = theBufferList().getBuffer(fname);
193 if (!fname.exists() && !LyXVC::fileInVC(fname))
196 buffer = theBufferList().newBuffer(fname.absFileName());
198 // Buffer creation is not possible.
201 if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
202 //close the buffer we just opened
203 theBufferList().release(buffer);