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)
52 // File information about normal file
55 string const file = MakeDisplayPath(s, 50);
56 string text = bformat(_("The specified document\n%1$s"
57 "\ncould not be read."), file);
58 Alert::error(_("Could not read document"), text);
62 // Check if emergency save file exists and is newer.
63 string const e = OnlyPath(s) + OnlyFilename(s) + ".emergency";
66 if (fileE.exist() && fileN.exist()
67 && fileE.getModificationTime() > fileN.getModificationTime())
69 string const file = MakeDisplayPath(s, 20);
70 string text = bformat(_("An emergency save of the document "
72 "Recover emergency save?"), file);
73 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
74 _("&Recover"), _("&Load Original"),
78 // the file is not saved if we load the emergency file.
80 return b->readFile(e);
88 // Now check if autosave file is newer.
89 string const a = OnlyPath(s) + '#' + OnlyFilename(s) + '#';
92 if (fileA.exist() && fileN.exist()
93 && fileA.getModificationTime() > fileN.getModificationTime())
95 string const file = MakeDisplayPath(s, 20);
96 string text = bformat(_("The backup of the document "
97 "%1$s is newer.\n\nLoad the "
98 "backup instead?"), file);
99 switch (Alert::prompt(_("Load backup?"), text, 0, 2,
100 _("&Load backup"), _("Load &original"),
104 // the file is not saved if we load the autosave file.
106 return b->readFile(a);
108 // Here we delete the autosave
115 return b->readFile(s);
123 bool loadLyXFile(Buffer * b, string const & s)
125 switch (IsFileWriteable(s)) {
127 b->setReadonly(true);
130 if (readFile(b, s)) {
131 b->lyxvc().file_found_hook(s);
136 string const file = MakeDisplayPath(s, 20);
137 // Here we probably should run
138 if (LyXVC::file_not_found_hook(s)) {
139 string text = bformat(_("Do you want to retrieve the document"
140 " %1$s from version control?"), file);
141 int const ret = Alert::prompt(_("Retrieve from version control?"),
142 text, 0, 1, _("&Retrieve"), _("&Cancel"));
145 // How can we know _how_ to do the checkout?
146 // With the current VC support it has to be,
147 // a RCS file since CVS do not have special ,v files.
149 return loadLyXFile(b, s);
158 Buffer * newFile(string const & filename, string const & templatename,
162 Buffer * b = bufferlist.newBuffer(filename);
165 // use defaults.lyx as a default template if it exists.
166 if (templatename.empty())
167 tname = LibFileSearch("templates", "defaults.lyx");
169 tname = templatename;
171 if (!tname.empty()) {
172 if (!b->readFile(tname)) {
173 string const file = MakeDisplayPath(tname, 50);
174 string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
175 Alert::error(_("Could not read template"), text);
176 // no template, start with empty buffer
182 b->setFileName(filename);
185 b->setReadonly(false);
186 b->fully_loaded(true);
187 b->updateDocLang(b->params().language);
193 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
195 TeXErrors::Errors::const_iterator cit = terr.begin();
196 TeXErrors::Errors::const_iterator end = terr.end();
198 for (; cit != end; ++cit) {
201 int const errorrow = cit->error_in_line;
202 buf.texrow().getIdFromRow(errorrow, par_id, posstart);
204 buf.texrow().getIdFromRow(errorrow + 1, par_id, posend);
205 buf.error(ErrorItem(cit->error_desc,
207 par_id, posstart, posend));
212 void bufferErrors(Buffer const & buf, ErrorList const & el)
214 ErrorList::const_iterator it = el.begin();
215 ErrorList::const_iterator end = el.end();
217 for (; it != end; ++it)
222 string const BufferFormat(Buffer const & buffer)
224 if (buffer.isLinuxDoc())
226 else if (buffer.isDocBook())
228 else if (buffer.isLiterate())