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 if (!Alert::prompt(_("Reload saved document?"),
77 text, 0, 1, _("&Reload"), _("&Keep Changes"))) {
78 // reload the document
79 if (checkBuffer->reload() != Buffer::ReadSuccess)
85 bool const exists = filename.exists();
86 bool const tryVC = exists ? false : LyXVC::fileInVC(filename);
87 if (exists || tryVC) {
88 if (exists && !filename.isReadableFile()) {
89 docstring text = bformat(_("The file %1$s exists but is not "
90 "readable by the current user."),
91 from_utf8(filename.absFileName()));
92 Alert::error(_("File not readable!"), text);
95 Buffer * b = theBufferList().newBuffer(filename.absFileName());
97 // Buffer creation is not possible.
100 if (b->loadLyXFile() != Buffer::ReadSuccess) {
101 // do not save an emergency file when releasing the buffer
103 theBufferList().release(b);
109 docstring text = bformat(_("The document %1$s does not yet "
110 "exist.\n\nDo you want to create a new document?"),
111 from_utf8(filename.absFileName()));
112 if (!Alert::prompt(_("Create new document?"),
113 text, 0, 1, _("&Create"), _("Cancel")))
114 return newFile(filename.absFileName(), string(), true);
120 // FIXME newFile() should probably be a member method of Application...
121 Buffer * newFile(string const & filename, string const & templatename,
125 Buffer * b = theBufferList().newBuffer(filename);
127 // Buffer creation is not possible.
131 // use defaults.lyx as a default template if it exists.
132 if (templatename.empty())
133 tname = libFileSearch("templates", "defaults.lyx");
135 tname = makeAbsPath(templatename);
137 if (!tname.empty()) {
138 if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
139 docstring const file = makeDisplayPath(tname.absFileName(), 50);
140 docstring const text = bformat(
141 _("The specified document template\n%1$s\ncould not be read."),
143 Alert::error(_("Could not read template"), text);
144 theBufferList().release(b);
150 // in this case, the user chose the filename, so we
151 // assume that she really does want this file.
156 b->setReadonly(false);
157 b->setFullyLoaded(true);
163 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
164 string const & templatename)
166 static map<string, int> file_number;
172 prefix + convert<string>(++file_number[prefix]) + ".lyx");
174 while (theBufferList().exists(filename) || filename.isReadableFile());
176 return newFile(filename.absFileName(), templatename, false);
180 Buffer * loadIfNeeded(FileName const & fname)
182 Buffer * buffer = theBufferList().getBuffer(fname);
184 if (!fname.exists() && !LyXVC::fileInVC(fname))
187 buffer = theBufferList().newBuffer(fname.absFileName());
189 // Buffer creation is not possible.
192 if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
193 //close the buffer we just opened
194 theBufferList().release(buffer);