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"
23 #include "ParagraphList.h"
24 #include "paragraph.h"
26 #include "frontends/Alert.h"
28 #include "support/filetools.h"
29 #include "support/FileInfo.h"
30 #include "support/lyxlib.h"
33 extern BufferList bufferlist;
35 using namespace lyx::support;
40 bool readFile(Buffer * b, string const & s)
43 string e = OnlyPath(s);
45 // File information about normal file
48 if (!fileInfo.exist()) {
49 string const file = MakeDisplayPath(s, 50);
50 string text = bformat(_("The specified document\n%1$s"
51 "\ncould not be read."), file);
52 Alert::error(_("Could not read document"), text);
56 // Check if emergency save file exists and is newer.
57 e += OnlyFilename(s) + ".emergency";
58 FileInfo fileInfoE(e);
60 bool use_emergency = false;
62 if (fileInfoE.exist() && fileInfo.exist()) {
63 if (fileInfoE.getModificationTime()
64 > fileInfo.getModificationTime()) {
65 string const file = MakeDisplayPath(s, 20);
66 string text = bformat(_("An emergency save of the document %1$s exists.\n"
67 "\nRecover emergency save?"), file);
68 int const ret = Alert::prompt(_("Load emergency save?"),
69 text, 0, 1, _("&Recover"), _("&Load Original"));
73 // the file is not saved if we load the
82 // Now check if autosave file is newer.
86 FileInfo fileInfoA(a);
87 if (fileInfoA.exist() && fileInfo.exist()) {
88 if (fileInfoA.getModificationTime()
89 > fileInfo.getModificationTime()) {
90 string const file = MakeDisplayPath(s, 20);
91 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
92 "Load the backup instead?"), file);
93 int const ret = Alert::prompt(_("Load backup?"),
94 text, 0, 1, _("&Load backup"), _("Load &original"));
98 // the file is not saved if we load the
102 // Here, we should delete the autosave
108 // not sure if this is the correct place to begin LyXLex
112 return b->readFile(lex, ts);
120 bool loadLyXFile(Buffer * b, string const & s)
122 switch (IsFileWriteable(s)) {
124 b->setReadonly(true);
127 if (readFile(b, s)) {
128 b->lyxvc.file_found_hook(s);
133 string const file = MakeDisplayPath(s, 20);
134 // Here we probably should run
135 if (LyXVC::file_not_found_hook(s)) {
136 string text = bformat(_("Do you want to retrieve the document"
137 " %1$s from version control?"), file);
138 int const ret = Alert::prompt(_("Retrieve from version control?"),
139 text, 0, 1, _("&Retrieve"), _("&Cancel"));
142 // How can we know _how_ to do the checkout?
143 // With the current VC support it has to be,
144 // a RCS file since CVS do not have special ,v files.
146 return loadLyXFile(b, s);
155 Buffer * newFile(string const & filename, string const & templatename,
159 Buffer * b = bufferlist.newBuffer(filename);
162 // use defaults.lyx as a default template if it exists.
163 if (templatename.empty())
164 tname = LibFileSearch("templates", "defaults.lyx");
166 tname = templatename;
168 if (!tname.empty()) {
169 bool templateok = false;
173 if (b->readFile(lex, tname)) {
178 string const file = MakeDisplayPath(tname, 50);
179 string text = bformat(_("The specified document template\n%1$s\n"
180 "could not be read."), file);
181 Alert::error(_("Could not read template"), text);
182 // no template, start with empty buffer
183 b->paragraphs.push_back(Paragraph());
184 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
186 } else { // start with empty buffer
187 b->paragraphs.push_back(Paragraph());
188 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
193 b->setFileName(filename);
196 b->setReadonly(false);
197 b->updateDocLang(b->params.language);
203 void parseErrors(Buffer const & buf, TeXErrors const & terr)
205 TeXErrors::Errors::const_iterator cit = terr.begin();
206 TeXErrors::Errors::const_iterator end = terr.end();
208 for (; cit != end; ++cit) {
211 int const errorrow = cit->error_in_line;
212 buf.texrow.getIdFromRow(errorrow, par_id, posstart);
214 buf.texrow.getIdFromRow(errorrow + 1, par_id, posend);
215 buf.parseError(ErrorItem(cit->error_desc,
217 par_id, posstart, posend));
222 void parseErrors(Buffer const & buf, ErrorList const & el)
224 ErrorList::const_iterator it = el.begin();
225 ErrorList::const_iterator end = el.end();
227 for (; it != end; ++it)
232 string const BufferFormat(Buffer const & buffer)
234 if (buffer.isLinuxDoc())
236 else if (buffer.isDocBook())
238 else if (buffer.isLiterate())