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/filetools.h"
32 #include "support/fs_extras.h"
33 #include "support/lyxlib.h"
35 #include <boost/bind.hpp>
36 #include <boost/filesystem/operations.hpp>
38 using lyx::support::bformat;
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;
47 namespace fs = boost::filesystem;
49 extern BufferList bufferlist;
53 bool readFile(Buffer * const b, string const & s)
57 // File information about normal file
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 string const e = OnlyPath(s) + OnlyFilename(s) + ".emergency";
69 if (fs::exists(e) && fs::exists(s)
70 && fs::last_write_time(e) > fs::last_write_time(s))
72 string const file = MakeDisplayPath(s, 20);
74 bformat(_("An emergency save of the document "
76 "Recover emergency save?"), file);
77 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
78 _("&Recover"), _("&Load Original"),
82 // the file is not saved if we load the emergency file.
84 return b->readFile(e);
92 // Now check if autosave file is newer.
93 string const a = OnlyPath(s) + '#' + OnlyFilename(s) + '#';
95 if (fs::exists(a) && fs::exists(s)
96 && fs::last_write_time(a) > fs::last_write_time(s))
98 string const file = MakeDisplayPath(s, 20);
100 bformat(_("The backup of the document "
101 "%1$s is newer.\n\nLoad the "
102 "backup instead?"), file);
103 switch (Alert::prompt(_("Load backup?"), text, 0, 2,
104 _("&Load backup"), _("Load &original"),
108 // the file is not saved if we load the autosave file.
110 return b->readFile(a);
112 // Here we delete the autosave
119 return b->readFile(s);
127 bool loadLyXFile(Buffer * b, string const & s)
131 if (fs::is_readable(s)) {
132 if (readFile(b, s)) {
133 b->lyxvc().file_found_hook(s);
134 if (!fs::is_writable(s))
135 b->setReadonly(true);
139 string const file = MakeDisplayPath(s, 20);
140 // Here we probably should run
141 if (LyXVC::file_not_found_hook(s)) {
143 bformat(_("Do you want to retrieve the document"
144 " %1$s from version control?"), file);
145 int const ret = Alert::prompt(_("Retrieve from version control?"),
146 text, 0, 1, _("&Retrieve"), _("&Cancel"));
149 // How can we know _how_ to do the checkout?
150 // With the current VC support it has to be,
151 // a RCS file since CVS do not have special ,v files.
153 return loadLyXFile(b, s);
161 Buffer * newFile(string const & filename, string const & templatename,
165 Buffer * b = bufferlist.newBuffer(filename);
169 // use defaults.lyx as a default template if it exists.
170 if (templatename.empty())
171 tname = LibFileSearch("templates", "defaults.lyx");
173 tname = templatename;
175 if (!tname.empty()) {
176 if (!b->readFile(tname)) {
177 string const file = MakeDisplayPath(tname, 50);
178 string const text = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
179 Alert::error(_("Could not read template"), text);
180 // no template, start with empty buffer
186 b->setFileName(filename);
189 b->setReadonly(false);
190 b->fully_loaded(true);
191 b->updateDocLang(b->params().language);
197 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
199 TeXErrors::Errors::const_iterator cit = terr.begin();
200 TeXErrors::Errors::const_iterator end = terr.end();
202 for (; cit != end; ++cit) {
205 int const errorrow = cit->error_in_line;
206 buf.texrow().getIdFromRow(errorrow, par_id, posstart);
208 buf.texrow().getIdFromRow(errorrow + 1, par_id, posend);
209 buf.error(ErrorItem(cit->error_desc,
211 par_id, posstart, posend));
216 void bufferErrors(Buffer const & buf, ErrorList const & el)
218 for_each(el.begin(), el.end(), bind(ref(buf.error), _1));
222 string const BufferFormat(Buffer const & buffer)
224 if (buffer.isLinuxDoc())
226 else if (buffer.isDocBook())
228 else if (buffer.isLiterate())
235 int countWords(DocIterator const & from, DocIterator const & to)
239 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
240 // Copied and adapted from isLetter() in ControlSpellChecker
242 && dit.pos() != dit.lastpos()
243 && dit.paragraph().isLetter(dit.pos())
244 && !isDeletedText(dit.paragraph(), dit.pos())) {