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"
16 #include "bufferlist.h"
18 #include "errorlist.h"
20 #include "vc-backend.h"
22 #include "ParagraphList.h"
23 #include "paragraph.h"
25 #include "frontends/Alert.h"
27 #include "support/filetools.h"
28 #include "support/FileInfo.h"
29 #include "support/lyxlib.h"
32 extern BufferList bufferlist;
34 using namespace lyx::support;
39 bool readFile(Buffer * b, string const & s)
42 string e = OnlyPath(s);
44 // File information about normal file
47 if (!fileInfo.exist()) {
48 string const file = MakeDisplayPath(s, 50);
49 string text = bformat(_("The specified document\n%1$s"
50 "\ncould not be read."), file);
51 Alert::error(_("Could not read document"), text);
55 // Check if emergency save file exists and is newer.
56 e += OnlyFilename(s) + ".emergency";
57 FileInfo fileInfoE(e);
59 bool use_emergency = false;
61 if (fileInfoE.exist() && fileInfo.exist()) {
62 if (fileInfoE.getModificationTime()
63 > fileInfo.getModificationTime()) {
64 string const file = MakeDisplayPath(s, 20);
65 string text = bformat(_("An emergency save of the document %1$s exists.\n"
66 "\nRecover emergency save?"), file);
67 int const ret = Alert::prompt(_("Load emergency save?"),
68 text, 0, 1, _("&Recover"), _("&Load Original"));
72 // the file is not saved if we load the
81 // Now check if autosave file is newer.
85 FileInfo fileInfoA(a);
86 if (fileInfoA.exist() && fileInfo.exist()) {
87 if (fileInfoA.getModificationTime()
88 > fileInfo.getModificationTime()) {
89 string const file = MakeDisplayPath(s, 20);
90 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
91 "Load the backup instead?"), file);
92 int const ret = Alert::prompt(_("Load backup?"),
93 text, 0, 1, _("&Load backup"), _("Load &original"));
97 // the file is not saved if we load the
101 // Here, we should delete the autosave
107 return b->readFile(ts);
115 bool loadLyXFile(Buffer * b, string const & s)
117 switch (IsFileWriteable(s)) {
119 b->setReadonly(true);
122 if (readFile(b, s)) {
123 b->lyxvc.file_found_hook(s);
128 string const file = MakeDisplayPath(s, 20);
129 // Here we probably should run
130 if (LyXVC::file_not_found_hook(s)) {
131 string text = bformat(_("Do you want to retrieve the document"
132 " %1$s from version control?"), file);
133 int const ret = Alert::prompt(_("Retrieve from version control?"),
134 text, 0, 1, _("&Retrieve"), _("&Cancel"));
137 // How can we know _how_ to do the checkout?
138 // With the current VC support it has to be,
139 // a RCS file since CVS do not have special ,v files.
141 return loadLyXFile(b, s);
150 Buffer * newFile(string const & filename, string const & templatename,
154 Buffer * b = bufferlist.newBuffer(filename);
157 // use defaults.lyx as a default template if it exists.
158 if (templatename.empty())
159 tname = LibFileSearch("templates", "defaults.lyx");
161 tname = templatename;
163 if (!tname.empty()) {
164 if (!b->readFile(tname)) {
165 string const file = MakeDisplayPath(tname, 50);
166 string const text = bformat(_("The specified document template\n%1$s\n"
167 "could not be read."), file);
168 Alert::error(_("Could not read template"), text);
169 // no template, start with empty buffer
170 b->paragraphs.push_back(Paragraph());
171 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
173 } else { // start with empty buffer
174 b->paragraphs.push_back(Paragraph());
175 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
180 b->setFileName(filename);
183 b->setReadonly(false);
184 b->updateDocLang(b->params.language);
190 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
192 TeXErrors::Errors::const_iterator cit = terr.begin();
193 TeXErrors::Errors::const_iterator end = terr.end();
195 for (; cit != end; ++cit) {
198 int const errorrow = cit->error_in_line;
199 buf.texrow.getIdFromRow(errorrow, par_id, posstart);
201 buf.texrow.getIdFromRow(errorrow + 1, par_id, posend);
202 buf.error(ErrorItem(cit->error_desc,
204 par_id, posstart, posend));
209 void bufferErrors(Buffer const & buf, ErrorList const & el)
211 ErrorList::const_iterator it = el.begin();
212 ErrorList::const_iterator end = el.end();
214 for (; it != end; ++it)
219 string const BufferFormat(Buffer const & buffer)
221 if (buffer.isLinuxDoc())
223 else if (buffer.isDocBook())
225 else if (buffer.isLiterate())