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;
46 extern BufferList bufferlist;
50 bool readFile(Buffer * b, string const & s)
53 string e = OnlyPath(s);
55 // File information about normal file
58 if (!fileInfo.exist()) {
59 string const file = MakeDisplayPath(s, 50);
60 string text = bformat(_("The specified document\n%1$s"
61 "\ncould not be read."), file);
62 Alert::error(_("Could not read document"), text);
66 // Check if emergency save file exists and is newer.
67 e += OnlyFilename(s) + ".emergency";
68 FileInfo fileInfoE(e);
70 bool use_emergency = false;
72 if (fileInfoE.exist() && fileInfo.exist()) {
73 if (fileInfoE.getModificationTime()
74 > fileInfo.getModificationTime()) {
75 string const file = MakeDisplayPath(s, 20);
76 string text = bformat(_("An emergency save of the document %1$s exists.\n"
77 "\nRecover emergency save?"), file);
78 int const ret = Alert::prompt(_("Load emergency save?"),
79 text, 0, 1, _("&Recover"), _("&Load Original"));
83 // the file is not saved if we load the
92 // Now check if autosave file is newer.
96 FileInfo fileInfoA(a);
97 if (fileInfoA.exist() && fileInfo.exist()) {
98 if (fileInfoA.getModificationTime()
99 > fileInfo.getModificationTime()) {
100 string const file = MakeDisplayPath(s, 20);
101 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
102 "Load the backup instead?"), file);
103 int const ret = Alert::prompt(_("Load backup?"),
104 text, 0, 1, _("&Load backup"), _("Load &original"));
108 // the file is not saved if we load the
112 // Here, we should delete the autosave
118 return b->readFile(ts);
126 bool loadLyXFile(Buffer * b, string const & s)
128 switch (IsFileWriteable(s)) {
130 b->setReadonly(true);
133 if (readFile(b, s)) {
134 b->lyxvc().file_found_hook(s);
139 string const file = MakeDisplayPath(s, 20);
140 // Here we probably should run
141 if (LyXVC::file_not_found_hook(s)) {
142 string text = bformat(_("Do you want to retrieve the document"
143 " %1$s from version control?"), file);
144 int const ret = Alert::prompt(_("Retrieve from version control?"),
145 text, 0, 1, _("&Retrieve"), _("&Cancel"));
148 // How can we know _how_ to do the checkout?
149 // With the current VC support it has to be,
150 // a RCS file since CVS do not have special ,v files.
152 return loadLyXFile(b, s);
161 Buffer * newFile(string const & filename, string const & templatename,
165 Buffer * b = bufferlist.newBuffer(filename);
168 // use defaults.lyx as a default template if it exists.
169 if (templatename.empty())
170 tname = LibFileSearch("templates", "defaults.lyx");
172 tname = templatename;
174 if (!tname.empty()) {
175 if (!b->readFile(tname)) {
176 string const file = MakeDisplayPath(tname, 50);
177 string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
178 Alert::error(_("Could not read template"), text);
179 // no template, start with empty buffer
185 b->setFileName(filename);
188 b->setReadonly(false);
189 b->fully_loaded(true);
190 b->updateDocLang(b->params().language);
196 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
198 TeXErrors::Errors::const_iterator cit = terr.begin();
199 TeXErrors::Errors::const_iterator end = terr.end();
201 for (; cit != end; ++cit) {
204 int const errorrow = cit->error_in_line;
205 buf.texrow().getIdFromRow(errorrow, par_id, posstart);
207 buf.texrow().getIdFromRow(errorrow + 1, par_id, posend);
208 buf.error(ErrorItem(cit->error_desc,
210 par_id, posstart, posend));
215 void bufferErrors(Buffer const & buf, ErrorList const & el)
217 ErrorList::const_iterator it = el.begin();
218 ErrorList::const_iterator end = el.end();
220 for (; it != end; ++it)
225 string const BufferFormat(Buffer const & buffer)
227 if (buffer.isLinuxDoc())
229 else if (buffer.isDocBook())
231 else if (buffer.isLiterate())