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 #include <boost/bind.hpp>
36 using lyx::support::bformat;
37 using lyx::support::FileInfo;
38 using lyx::support::IsFileWriteable;
39 using lyx::support::LibFileSearch;
40 using lyx::support::MakeDisplayPath;
41 using lyx::support::OnlyFilename;
42 using lyx::support::OnlyPath;
43 using lyx::support::unlink;
48 extern BufferList bufferlist;
52 bool readFile(Buffer * b, string const & s)
54 // File information about normal file
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 string const e = OnlyPath(s) + OnlyFilename(s) + ".emergency";
68 if (fileE.exist() && fileN.exist()
69 && fileE.getModificationTime() > fileN.getModificationTime())
71 string const file = MakeDisplayPath(s, 20);
72 string text = bformat(_("An emergency save of the document "
74 "Recover emergency save?"), file);
75 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
76 _("&Recover"), _("&Load Original"),
80 // the file is not saved if we load the emergency file.
82 return b->readFile(e);
90 // Now check if autosave file is newer.
91 string const a = OnlyPath(s) + '#' + OnlyFilename(s) + '#';
94 if (fileA.exist() && fileN.exist()
95 && fileA.getModificationTime() > fileN.getModificationTime())
97 string const file = MakeDisplayPath(s, 20);
98 string text = bformat(_("The backup of the document "
99 "%1$s is newer.\n\nLoad the "
100 "backup instead?"), file);
101 switch (Alert::prompt(_("Load backup?"), text, 0, 2,
102 _("&Load backup"), _("Load &original"),
106 // the file is not saved if we load the autosave file.
108 return b->readFile(a);
110 // Here we delete the autosave
117 return b->readFile(s);
125 bool loadLyXFile(Buffer * b, string const & s)
127 switch (IsFileWriteable(s)) {
129 b->setReadonly(true);
132 if (readFile(b, s)) {
133 b->lyxvc().file_found_hook(s);
138 string const file = MakeDisplayPath(s, 20);
139 // Here we probably should run
140 if (LyXVC::file_not_found_hook(s)) {
141 string text = bformat(_("Do you want to retrieve the document"
142 " %1$s from version control?"), file);
143 int const ret = Alert::prompt(_("Retrieve from version control?"),
144 text, 0, 1, _("&Retrieve"), _("&Cancel"));
147 // How can we know _how_ to do the checkout?
148 // With the current VC support it has to be,
149 // a RCS file since CVS do not have special ,v files.
151 return loadLyXFile(b, s);
160 Buffer * newFile(string const & filename, string const & templatename,
164 Buffer * b = bufferlist.newBuffer(filename);
167 // use defaults.lyx as a default template if it exists.
168 if (templatename.empty())
169 tname = LibFileSearch("templates", "defaults.lyx");
171 tname = templatename;
173 if (!tname.empty()) {
174 if (!b->readFile(tname)) {
175 string const file = MakeDisplayPath(tname, 50);
176 string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
177 Alert::error(_("Could not read template"), text);
178 // no template, start with empty buffer
184 b->setFileName(filename);
187 b->setReadonly(false);
188 b->fully_loaded(true);
189 b->updateDocLang(b->params().language);
195 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
197 TeXErrors::Errors::const_iterator cit = terr.begin();
198 TeXErrors::Errors::const_iterator end = terr.end();
200 for (; cit != end; ++cit) {
203 int const errorrow = cit->error_in_line;
204 buf.texrow().getIdFromRow(errorrow, par_id, posstart);
206 buf.texrow().getIdFromRow(errorrow + 1, par_id, posend);
207 buf.error(ErrorItem(cit->error_desc,
209 par_id, posstart, posend));
214 void bufferErrors(Buffer const & buf, ErrorList const & el)
216 for_each(el.begin(), el.end(), bind(ref(buf.error), _1));
220 string const BufferFormat(Buffer const & buffer)
222 if (buffer.isLinuxDoc())
224 else if (buffer.isDocBook())
226 else if (buffer.isLiterate())