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 * const b, string const & s)
57 // File information about normal file
60 string const file = MakeDisplayPath(s, 50);
61 string text = bformat(_("The specified document\n%1$s"
62 "\ncould not be read."), file);
63 Alert::error(_("Could not read document"), text);
67 // Check if emergency save file exists and is newer.
68 string const e = OnlyPath(s) + OnlyFilename(s) + ".emergency";
71 if (fileE.exist() && fileN.exist()
72 && fileE.getModificationTime() > fileN.getModificationTime())
74 string const file = MakeDisplayPath(s, 20);
76 bformat(_("An emergency save of the document "
78 "Recover emergency save?"), file);
79 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
80 _("&Recover"), _("&Load Original"),
84 // the file is not saved if we load the emergency file.
86 return b->readFile(e);
94 // Now check if autosave file is newer.
95 string const a = OnlyPath(s) + '#' + OnlyFilename(s) + '#';
98 if (fileA.exist() && fileN.exist()
99 && fileA.getModificationTime() > fileN.getModificationTime())
101 string const file = MakeDisplayPath(s, 20);
103 bformat(_("The backup of the document "
104 "%1$s is newer.\n\nLoad the "
105 "backup instead?"), file);
106 switch (Alert::prompt(_("Load backup?"), text, 0, 2,
107 _("&Load backup"), _("Load &original"),
111 // the file is not saved if we load the autosave file.
113 return b->readFile(a);
115 // Here we delete the autosave
122 return b->readFile(s);
130 bool loadLyXFile(Buffer * b, string const & s)
134 switch (IsFileWriteable(s)) {
136 b->setReadonly(true);
139 if (readFile(b, s)) {
140 b->lyxvc().file_found_hook(s);
145 string const file = MakeDisplayPath(s, 20);
146 // Here we probably should run
147 if (LyXVC::file_not_found_hook(s)) {
149 bformat(_("Do you want to retrieve the document"
150 " %1$s from version control?"), file);
151 int const ret = Alert::prompt(_("Retrieve from version control?"),
152 text, 0, 1, _("&Retrieve"), _("&Cancel"));
155 // How can we know _how_ to do the checkout?
156 // With the current VC support it has to be,
157 // a RCS file since CVS do not have special ,v files.
159 return loadLyXFile(b, s);
168 Buffer * newFile(string const & filename, string const & templatename,
172 Buffer * b = bufferlist.newBuffer(filename);
176 // use defaults.lyx as a default template if it exists.
177 if (templatename.empty())
178 tname = LibFileSearch("templates", "defaults.lyx");
180 tname = templatename;
182 if (!tname.empty()) {
183 if (!b->readFile(tname)) {
184 string const file = MakeDisplayPath(tname, 50);
185 string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
186 Alert::error(_("Could not read template"), text);
187 // no template, start with empty buffer
193 b->setFileName(filename);
196 b->setReadonly(false);
197 b->fully_loaded(true);
198 b->updateDocLang(b->params().language);
204 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
206 TeXErrors::Errors::const_iterator cit = terr.begin();
207 TeXErrors::Errors::const_iterator end = terr.end();
209 for (; cit != end; ++cit) {
212 int const errorrow = cit->error_in_line;
213 buf.texrow().getIdFromRow(errorrow, par_id, posstart);
215 buf.texrow().getIdFromRow(errorrow + 1, par_id, posend);
216 buf.error(ErrorItem(cit->error_desc,
218 par_id, posstart, posend));
223 void bufferErrors(Buffer const & buf, ErrorList const & el)
225 for_each(el.begin(), el.end(), bind(ref(buf.error), _1));
229 string const BufferFormat(Buffer const & buffer)
231 if (buffer.isLinuxDoc())
233 else if (buffer.isDocBook())
235 else if (buffer.isLiterate())
242 int countWords(DocIterator const & from, DocIterator const & to)
246 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
247 // Copied and adapted from isLetter() in ControlSpellChecker
249 && dit.pos() != dit.lastpos()
250 && dit.paragraph().isLetter(dit.pos())
251 && !isDeletedText(dit.paragraph(), dit.pos())) {