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"
34 using lyx::support::bformat;
35 using lyx::support::FileInfo;
36 using lyx::support::IsFileWriteable;
37 using lyx::support::LibFileSearch;
38 using lyx::support::MakeDisplayPath;
39 using lyx::support::OnlyFilename;
40 using lyx::support::OnlyPath;
41 using lyx::support::unlink;
44 extern BufferList bufferlist;
48 bool readFile(Buffer * b, string const & s)
51 string e = OnlyPath(s);
53 // File information about normal file
56 if (!fileInfo.exist()) {
57 string const file = MakeDisplayPath(s, 50);
58 string text = bformat(_("The specified document\n%1$s"
59 "\ncould not be read."), file);
60 Alert::error(_("Could not read document"), text);
64 // Check if emergency save file exists and is newer.
65 e += OnlyFilename(s) + ".emergency";
66 FileInfo fileInfoE(e);
68 bool use_emergency = false;
70 if (fileInfoE.exist() && fileInfo.exist()) {
71 if (fileInfoE.getModificationTime()
72 > fileInfo.getModificationTime()) {
73 string const file = MakeDisplayPath(s, 20);
74 string text = bformat(_("An emergency save of the document %1$s exists.\n"
75 "\nRecover emergency save?"), file);
76 int const ret = Alert::prompt(_("Load emergency save?"),
77 text, 0, 1, _("&Recover"), _("&Load Original"));
81 // the file is not saved if we load the
90 // Now check if autosave file is newer.
94 FileInfo fileInfoA(a);
95 if (fileInfoA.exist() && fileInfo.exist()) {
96 if (fileInfoA.getModificationTime()
97 > fileInfo.getModificationTime()) {
98 string const file = MakeDisplayPath(s, 20);
99 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
100 "Load the backup instead?"), file);
101 int const ret = Alert::prompt(_("Load backup?"),
102 text, 0, 1, _("&Load backup"), _("Load &original"));
106 // the file is not saved if we load the
110 // Here, we should delete the autosave
116 return b->readFile(ts);
124 bool loadLyXFile(Buffer * b, string const & s)
126 switch (IsFileWriteable(s)) {
128 b->setReadonly(true);
131 if (readFile(b, s)) {
132 b->lyxvc().file_found_hook(s);
137 string const file = MakeDisplayPath(s, 20);
138 // Here we probably should run
139 if (LyXVC::file_not_found_hook(s)) {
140 string text = bformat(_("Do you want to retrieve the document"
141 " %1$s from version control?"), file);
142 int const ret = Alert::prompt(_("Retrieve from version control?"),
143 text, 0, 1, _("&Retrieve"), _("&Cancel"));
146 // How can we know _how_ to do the checkout?
147 // With the current VC support it has to be,
148 // a RCS file since CVS do not have special ,v files.
150 return loadLyXFile(b, s);
159 Buffer * newFile(string const & filename, string const & templatename,
163 Buffer * b = bufferlist.newBuffer(filename);
166 // use defaults.lyx as a default template if it exists.
167 if (templatename.empty())
168 tname = LibFileSearch("templates", "defaults.lyx");
170 tname = templatename;
172 if (!tname.empty()) {
173 if (!b->readFile(tname)) {
174 string const file = MakeDisplayPath(tname, 50);
175 string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
176 Alert::error(_("Could not read template"), text);
177 // no template, start with empty buffer
178 b->paragraphs().push_back(Paragraph());
179 b->paragraphs().begin()->layout(b->params().getLyXTextClass().defaultLayout());
181 } else { // start with empty buffer
182 b->paragraphs().push_back(Paragraph());
183 b->paragraphs().begin()->layout(b->params().getLyXTextClass().defaultLayout());
188 b->setFileName(filename);
191 b->setReadonly(false);
192 b->updateDocLang(b->params().language);
198 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
200 TeXErrors::Errors::const_iterator cit = terr.begin();
201 TeXErrors::Errors::const_iterator end = terr.end();
203 for (; cit != end; ++cit) {
206 int const errorrow = cit->error_in_line;
207 buf.texrow().getIdFromRow(errorrow, par_id, posstart);
209 buf.texrow().getIdFromRow(errorrow + 1, par_id, posend);
210 buf.error(ErrorItem(cit->error_desc,
212 par_id, posstart, posend));
217 void bufferErrors(Buffer const & buf, ErrorList const & el)
219 ErrorList::const_iterator it = el.begin();
220 ErrorList::const_iterator end = el.end();
222 for (; it != end; ++it)
227 string const BufferFormat(Buffer const & buffer)
229 if (buffer.isLinuxDoc())
231 else if (buffer.isDocBook())
233 else if (buffer.isLiterate())