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 "dociterator.h"
21 #include "errorlist.h"
24 #include "paragraph.h"
27 #include "vc-backend.h"
29 #include "frontends/Alert.h"
31 #include "support/FileInfo.h"
32 #include "support/filetools.h"
33 #include "support/lyxlib.h"
35 #include <boost/bind.hpp>
37 using lyx::support::bformat;
38 using lyx::support::FileInfo;
39 using lyx::support::IsFileWriteable;
40 using lyx::support::LibFileSearch;
41 using lyx::support::MakeDisplayPath;
42 using lyx::support::OnlyFilename;
43 using lyx::support::OnlyPath;
44 using lyx::support::unlink;
49 extern BufferList bufferlist;
53 bool readFile(Buffer * b, string const & s)
55 // File information about normal file
58 string const file = MakeDisplayPath(s, 50);
59 string text = bformat(_("The specified document\n%1$s"
60 "\ncould not be read."), file);
61 Alert::error(_("Could not read document"), text);
65 // Check if emergency save file exists and is newer.
66 string const e = OnlyPath(s) + OnlyFilename(s) + ".emergency";
69 if (fileE.exist() && fileN.exist()
70 && fileE.getModificationTime() > fileN.getModificationTime())
72 string const file = MakeDisplayPath(s, 20);
73 string text = bformat(_("An emergency save of the document "
75 "Recover emergency save?"), file);
76 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
77 _("&Recover"), _("&Load Original"),
81 // the file is not saved if we load the emergency file.
83 return b->readFile(e);
91 // Now check if autosave file is newer.
92 string const a = OnlyPath(s) + '#' + OnlyFilename(s) + '#';
95 if (fileA.exist() && fileN.exist()
96 && fileA.getModificationTime() > fileN.getModificationTime())
98 string const file = MakeDisplayPath(s, 20);
99 string text = bformat(_("The backup of the document "
100 "%1$s is newer.\n\nLoad the "
101 "backup instead?"), file);
102 switch (Alert::prompt(_("Load backup?"), text, 0, 2,
103 _("&Load backup"), _("Load &original"),
107 // the file is not saved if we load the autosave file.
109 return b->readFile(a);
111 // Here we delete the autosave
118 return b->readFile(s);
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 for_each(el.begin(), el.end(), bind(ref(buf.error), _1));
221 string const BufferFormat(Buffer const & buffer)
223 if (buffer.isLinuxDoc())
225 else if (buffer.isDocBook())
227 else if (buffer.isLiterate())
234 int countWords(DocIterator const & from, DocIterator const & to)
238 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
239 // Copied and adapted from isLetter() in ControlSpellChecker
241 && dit.pos() != dit.lastpos()
242 && dit.paragraph().isLetter(dit.pos())
243 && !isDeletedText(dit.paragraph(), dit.pos())) {