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"
19 #include "vc-backend.h"
22 #include "frontends/Alert.h"
24 #include "support/filetools.h"
25 #include "support/FileInfo.h"
26 #include "support/lyxlib.h"
29 extern BufferList bufferlist;
31 using namespace lyx::support;
36 bool readFile(Buffer * b, string const & s)
39 string e = OnlyPath(s);
41 // File information about normal file
44 if (!fileInfo.exist()) {
45 string const file = MakeDisplayPath(s, 50);
46 string text = bformat(_("The specified document\n%1$s"
47 "\ncould not be read."), file);
48 Alert::error(_("Could not read document"), text);
52 // Check if emergency save file exists and is newer.
53 e += OnlyFilename(s) + ".emergency";
54 FileInfo fileInfoE(e);
56 bool use_emergency = false;
58 if (fileInfoE.exist() && fileInfo.exist()) {
59 if (fileInfoE.getModificationTime()
60 > fileInfo.getModificationTime()) {
61 string const file = MakeDisplayPath(s, 20);
62 string text = bformat(_("An emergency save of the document %1$s exists.\n"
63 "\nRecover emergency save?"), file);
64 int const ret = Alert::prompt(_("Load emergency save?"),
65 text, 0, 1, _("&Recover"), _("&Load Original"));
69 // the file is not saved if we load the
78 // Now check if autosave file is newer.
82 FileInfo fileInfoA(a);
83 if (fileInfoA.exist() && fileInfo.exist()) {
84 if (fileInfoA.getModificationTime()
85 > fileInfo.getModificationTime()) {
86 string const file = MakeDisplayPath(s, 20);
87 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
88 "Load the backup instead?"), file);
89 int const ret = Alert::prompt(_("Load backup?"),
90 text, 0, 1, _("&Load backup"), _("Load &original"));
94 // the file is not saved if we load the
98 // Here, we should delete the autosave
104 return b->readFile(ts);
112 bool loadLyXFile(Buffer * b, string const & s)
114 switch (IsFileWriteable(s)) {
116 b->setReadonly(true);
119 if (readFile(b, s)) {
120 b->lyxvc.file_found_hook(s);
125 string const file = MakeDisplayPath(s, 20);
126 // Here we probably should run
127 if (LyXVC::file_not_found_hook(s)) {
128 string text = bformat(_("Do you want to retrieve the document"
129 " %1$s from version control?"), file);
130 int const ret = Alert::prompt(_("Retrieve from version control?"),
131 text, 0, 1, _("&Retrieve"), _("&Cancel"));
134 // How can we know _how_ to do the checkout?
135 // With the current VC support it has to be,
136 // a RCS file since CVS do not have special ,v files.
138 return loadLyXFile(b, s);
147 Buffer * newFile(string const & filename, string const & templatename,
151 Buffer * b = bufferlist.newBuffer(filename);
154 // use defaults.lyx as a default template if it exists.
155 if (templatename.empty())
156 tname = LibFileSearch("templates", "defaults.lyx");
158 tname = templatename;
160 if (!tname.empty()) {
161 if (!b->readFile(tname)) {
162 string const file = MakeDisplayPath(tname, 50);
163 string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
164 Alert::error(_("Could not read template"), text);
165 // no template, start with empty buffer
166 b->paragraphs.push_back(Paragraph());
167 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
169 } else { // start with empty buffer
170 b->paragraphs.push_back(Paragraph());
171 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
176 b->setFileName(filename);
179 b->setReadonly(false);
180 b->updateDocLang(b->params.language);
186 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
188 TeXErrors::Errors::const_iterator cit = terr.begin();
189 TeXErrors::Errors::const_iterator end = terr.end();
191 for (; cit != end; ++cit) {
194 int const errorrow = cit->error_in_line;
195 buf.texrow.getIdFromRow(errorrow, par_id, posstart);
197 buf.texrow.getIdFromRow(errorrow + 1, par_id, posend);
198 buf.error(ErrorItem(cit->error_desc,
200 par_id, posstart, posend));
205 void bufferErrors(Buffer const & buf, ErrorList const & el)
207 ErrorList::const_iterator it = el.begin();
208 ErrorList::const_iterator end = el.end();
210 for (; it != end; ++it)
215 string const BufferFormat(Buffer const & buffer)
217 if (buffer.isLinuxDoc())
219 else if (buffer.isDocBook())
221 else if (buffer.isLiterate())