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"
18 #include "bufferlist.h"
19 #include "bufferparams.h"
20 #include "errorlist.h"
23 #include "paragraph.h"
26 #include "vc-backend.h"
28 #include "frontends/Alert.h"
30 #include "support/FileInfo.h"
31 #include "support/filetools.h"
32 #include "support/lyxlib.h"
35 extern BufferList bufferlist;
37 using namespace lyx::support;
42 bool readFile(Buffer * b, string const & s)
45 string e = OnlyPath(s);
47 // File information about normal file
50 if (!fileInfo.exist()) {
51 string const file = MakeDisplayPath(s, 50);
52 string text = bformat(_("The specified document\n%1$s"
53 "\ncould not be read."), file);
54 Alert::error(_("Could not read document"), text);
58 // Check if emergency save file exists and is newer.
59 e += OnlyFilename(s) + ".emergency";
60 FileInfo fileInfoE(e);
62 bool use_emergency = false;
64 if (fileInfoE.exist() && fileInfo.exist()) {
65 if (fileInfoE.getModificationTime()
66 > fileInfo.getModificationTime()) {
67 string const file = MakeDisplayPath(s, 20);
68 string text = bformat(_("An emergency save of the document %1$s exists.\n"
69 "\nRecover emergency save?"), file);
70 int const ret = Alert::prompt(_("Load emergency save?"),
71 text, 0, 1, _("&Recover"), _("&Load Original"));
75 // the file is not saved if we load the
84 // Now check if autosave file is newer.
88 FileInfo fileInfoA(a);
89 if (fileInfoA.exist() && fileInfo.exist()) {
90 if (fileInfoA.getModificationTime()
91 > fileInfo.getModificationTime()) {
92 string const file = MakeDisplayPath(s, 20);
93 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
94 "Load the backup instead?"), file);
95 int const ret = Alert::prompt(_("Load backup?"),
96 text, 0, 1, _("&Load backup"), _("Load &original"));
100 // the file is not saved if we load the
104 // Here, we should delete the autosave
110 return b->readFile(ts);
118 bool loadLyXFile(Buffer * b, string const & s)
120 switch (IsFileWriteable(s)) {
122 b->setReadonly(true);
125 if (readFile(b, s)) {
126 b->lyxvc().file_found_hook(s);
131 string const file = MakeDisplayPath(s, 20);
132 // Here we probably should run
133 if (LyXVC::file_not_found_hook(s)) {
134 string text = bformat(_("Do you want to retrieve the document"
135 " %1$s from version control?"), file);
136 int const ret = Alert::prompt(_("Retrieve from version control?"),
137 text, 0, 1, _("&Retrieve"), _("&Cancel"));
140 // How can we know _how_ to do the checkout?
141 // With the current VC support it has to be,
142 // a RCS file since CVS do not have special ,v files.
144 return loadLyXFile(b, s);
153 Buffer * newFile(string const & filename, string const & templatename,
157 Buffer * b = bufferlist.newBuffer(filename);
160 // use defaults.lyx as a default template if it exists.
161 if (templatename.empty())
162 tname = LibFileSearch("templates", "defaults.lyx");
164 tname = templatename;
166 if (!tname.empty()) {
167 if (!b->readFile(tname)) {
168 string const file = MakeDisplayPath(tname, 50);
169 string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
170 Alert::error(_("Could not read template"), text);
171 // no template, start with empty buffer
172 b->paragraphs().push_back(Paragraph());
173 b->paragraphs().begin()->layout(b->params().getLyXTextClass().defaultLayout());
175 } else { // start with empty buffer
176 b->paragraphs().push_back(Paragraph());
177 b->paragraphs().begin()->layout(b->params().getLyXTextClass().defaultLayout());
182 b->setFileName(filename);
185 b->setReadonly(false);
186 b->updateDocLang(b->params().language);
192 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
194 TeXErrors::Errors::const_iterator cit = terr.begin();
195 TeXErrors::Errors::const_iterator end = terr.end();
197 for (; cit != end; ++cit) {
200 int const errorrow = cit->error_in_line;
201 buf.texrow().getIdFromRow(errorrow, par_id, posstart);
203 buf.texrow().getIdFromRow(errorrow + 1, par_id, posend);
204 buf.error(ErrorItem(cit->error_desc,
206 par_id, posstart, posend));
211 void bufferErrors(Buffer const & buf, ErrorList const & el)
213 ErrorList::const_iterator it = el.begin();
214 ErrorList::const_iterator end = el.end();
216 for (; it != end; ++it)
221 string const BufferFormat(Buffer const & buffer)
223 if (buffer.isLinuxDoc())
225 else if (buffer.isDocBook())
227 else if (buffer.isLiterate())