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;
38 bool readFile(Buffer * b, string const & s)
41 string e = OnlyPath(s);
43 // File information about normal file
46 if (!fileInfo.exist()) {
47 string const file = MakeDisplayPath(s, 50);
48 string text = bformat(_("The specified document\n%1$s"
49 "\ncould not be read."), file);
50 Alert::error(_("Could not read document"), text);
54 // Check if emergency save file exists and is newer.
55 e += OnlyFilename(s) + ".emergency";
56 FileInfo fileInfoE(e);
58 bool use_emergency = false;
60 if (fileInfoE.exist() && fileInfo.exist()) {
61 if (fileInfoE.getModificationTime()
62 > fileInfo.getModificationTime()) {
63 string const file = MakeDisplayPath(s, 20);
64 string text = bformat(_("An emergency save of the document %1$s exists.\n"
65 "\nRecover emergency save?"), file);
66 int const ret = Alert::prompt(_("Load emergency save?"),
67 text, 0, 1, _("&Recover"), _("&Load Original"));
71 // the file is not saved if we load the
80 // Now check if autosave file is newer.
84 FileInfo fileInfoA(a);
85 if (fileInfoA.exist() && fileInfo.exist()) {
86 if (fileInfoA.getModificationTime()
87 > fileInfo.getModificationTime()) {
88 string const file = MakeDisplayPath(s, 20);
89 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
90 "Load the backup instead?"), file);
91 int const ret = Alert::prompt(_("Load backup?"),
92 text, 0, 1, _("&Load backup"), _("Load &original"));
96 // the file is not saved if we load the
100 // Here, we should delete the autosave
106 // not sure if this is the correct place to begin LyXLex
110 return b->readFile(lex, 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 bool templateok = false;
171 if (b->readFile(lex, tname)) {
176 string const file = MakeDisplayPath(tname, 50);
177 string text = bformat(_("The specified document template\n%1$s\n"
178 "could not be read."), file);
179 Alert::error(_("Could not read template"), text);
180 // no template, start with empty buffer
181 b->paragraphs.push_back(Paragraph());
182 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
184 } else { // start with empty buffer
185 b->paragraphs.push_back(Paragraph());
186 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
191 b->setFileName(filename);
194 b->setReadonly(false);
195 b->updateDocLang(b->params.language);
201 void parseErrors(Buffer const & buf, TeXErrors const & terr)
203 TeXErrors::Errors::const_iterator cit = terr.begin();
204 TeXErrors::Errors::const_iterator end = terr.end();
206 for (; cit != end; ++cit) {
209 int const errorrow = cit->error_in_line;
210 buf.texrow.getIdFromRow(errorrow, par_id, posstart);
212 buf.texrow.getIdFromRow(errorrow + 1, par_id, posend);
213 buf.parseError(ErrorItem(cit->error_desc,
215 par_id, posstart, posend));
220 void parseErrors(Buffer const & buf, ErrorList const & el)
222 ErrorList::const_iterator it = el.begin();
223 ErrorList::const_iterator end = el.end();
225 for (; it != end; ++it)
230 string const BufferFormat(Buffer const & buffer)
232 if (buffer.isLinuxDoc())
234 else if (buffer.isDocBook())
236 else if (buffer.isLiterate())