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 "paragraph.h"
20 #include "vc-backend.h"
23 #include "frontends/Alert.h"
25 #include "support/filetools.h"
26 #include "support/FileInfo.h"
27 #include "support/lyxlib.h"
30 extern BufferList bufferlist;
32 using namespace lyx::support;
37 bool readFile(Buffer * b, string const & s)
40 string e = OnlyPath(s);
42 // File information about normal file
45 if (!fileInfo.exist()) {
46 string const file = MakeDisplayPath(s, 50);
47 string text = bformat(_("The specified document\n%1$s"
48 "\ncould not be read."), file);
49 Alert::error(_("Could not read document"), text);
53 // Check if emergency save file exists and is newer.
54 e += OnlyFilename(s) + ".emergency";
55 FileInfo fileInfoE(e);
57 bool use_emergency = false;
59 if (fileInfoE.exist() && fileInfo.exist()) {
60 if (fileInfoE.getModificationTime()
61 > fileInfo.getModificationTime()) {
62 string const file = MakeDisplayPath(s, 20);
63 string text = bformat(_("An emergency save of the document %1$s exists.\n"
64 "\nRecover emergency save?"), file);
65 int const ret = Alert::prompt(_("Load emergency save?"),
66 text, 0, 1, _("&Recover"), _("&Load Original"));
70 // the file is not saved if we load the
79 // Now check if autosave file is newer.
83 FileInfo fileInfoA(a);
84 if (fileInfoA.exist() && fileInfo.exist()) {
85 if (fileInfoA.getModificationTime()
86 > fileInfo.getModificationTime()) {
87 string const file = MakeDisplayPath(s, 20);
88 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
89 "Load the backup instead?"), file);
90 int const ret = Alert::prompt(_("Load backup?"),
91 text, 0, 1, _("&Load backup"), _("Load &original"));
95 // the file is not saved if we load the
99 // Here, we should delete the autosave
105 return b->readFile(ts);
113 bool loadLyXFile(Buffer * b, string const & s)
115 switch (IsFileWriteable(s)) {
117 b->setReadonly(true);
120 if (readFile(b, s)) {
121 b->lyxvc.file_found_hook(s);
126 string const file = MakeDisplayPath(s, 20);
127 // Here we probably should run
128 if (LyXVC::file_not_found_hook(s)) {
129 string text = bformat(_("Do you want to retrieve the document"
130 " %1$s from version control?"), file);
131 int const ret = Alert::prompt(_("Retrieve from version control?"),
132 text, 0, 1, _("&Retrieve"), _("&Cancel"));
135 // How can we know _how_ to do the checkout?
136 // With the current VC support it has to be,
137 // a RCS file since CVS do not have special ,v files.
139 return loadLyXFile(b, s);
148 Buffer * newFile(string const & filename, string const & templatename,
152 Buffer * b = bufferlist.newBuffer(filename);
155 // use defaults.lyx as a default template if it exists.
156 if (templatename.empty())
157 tname = LibFileSearch("templates", "defaults.lyx");
159 tname = templatename;
161 if (!tname.empty()) {
162 if (!b->readFile(tname)) {
163 string const file = MakeDisplayPath(tname, 50);
164 string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
165 Alert::error(_("Could not read template"), text);
166 // no template, start with empty buffer
167 b->paragraphs.push_back(Paragraph());
168 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
170 } else { // start with empty buffer
171 b->paragraphs.push_back(Paragraph());
172 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
177 b->setFileName(filename);
180 b->setReadonly(false);
181 b->updateDocLang(b->params.language);
187 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
189 TeXErrors::Errors::const_iterator cit = terr.begin();
190 TeXErrors::Errors::const_iterator end = terr.end();
192 for (; cit != end; ++cit) {
195 int const errorrow = cit->error_in_line;
196 buf.texrow.getIdFromRow(errorrow, par_id, posstart);
198 buf.texrow.getIdFromRow(errorrow + 1, par_id, posend);
199 buf.error(ErrorItem(cit->error_desc,
201 par_id, posstart, posend));
206 void bufferErrors(Buffer const & buf, ErrorList const & el)
208 ErrorList::const_iterator it = el.begin();
209 ErrorList::const_iterator end = el.end();
211 for (; it != end; ++it)
216 string const BufferFormat(Buffer const & buffer)
218 if (buffer.isLinuxDoc())
220 else if (buffer.isDocBook())
222 else if (buffer.isLiterate())