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
8 * Full author contact details are available in file CREDITS.
13 #include "BufferList.h"
17 #include "BufferParams.h"
20 #include "output_latex.h"
21 #include "ParagraphList.h"
23 #include "frontends/alert.h"
25 #include "support/ExceptionMessage.h"
26 #include "support/debug.h"
27 #include "support/FileName.h"
28 #include "support/FileNameList.h"
29 #include "support/filetools.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/Package.h"
34 #include "support/lassert.h"
35 #include <boost/bind.hpp>
43 using namespace lyx::support;
47 namespace Alert = lyx::frontend::Alert;
50 BufferList::BufferList()
54 BufferList::~BufferList()
56 BufferStorage::iterator it = binternal.begin();
57 BufferStorage::iterator end = binternal.end();
58 for (; it != end; ++it)
63 bool BufferList::empty() const
65 return bstore.empty();
69 BufferList::iterator BufferList::begin()
71 return bstore.begin();
75 BufferList::const_iterator BufferList::begin() const
77 return bstore.begin();
81 BufferList::iterator BufferList::end()
87 BufferList::const_iterator BufferList::end() const
93 void BufferList::release(Buffer * buf)
96 BufferStorage::iterator const it =
97 find(bstore.begin(), bstore.end(), buf);
98 if (it != bstore.end()) {
107 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
109 auto_ptr<Buffer> tmpbuf;
111 tmpbuf.reset(new Buffer(s, ronly));
112 } catch (ExceptionMessage const & message) {
113 if (message.type_ == ErrorException) {
114 Alert::error(message.title_, message.details_);
116 } else if (message.type_ == WarningException) {
117 Alert::warning(message.title_, message.details_);
121 tmpbuf->params().useClassDefaults();
122 if (tmpbuf->fileName().extension() == "internal") {
123 binternal.push_back(tmpbuf.get());
125 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
126 bstore.push_back(tmpbuf.get());
128 return tmpbuf.release();
132 void BufferList::closeAll()
134 while (!bstore.empty())
135 release(bstore.front());
139 FileNameList const & BufferList::fileNames() const
141 static FileNameList nvec;
143 transform(bstore.begin(), bstore.end(),
145 boost::bind(&Buffer::fileName, _1));
150 Buffer * BufferList::first()
154 return bstore.front();
158 Buffer * BufferList::last()
162 return bstore.back();
166 Buffer * BufferList::getBuffer(unsigned int choice)
168 if (choice >= bstore.size())
170 return bstore[choice];
174 Buffer * BufferList::next(Buffer const * buf) const
180 BufferStorage::const_iterator it = find(bstore.begin(),
182 LASSERT(it != bstore.end(), /**/);
184 Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
189 Buffer * BufferList::previous(Buffer const * buf) const
195 BufferStorage::const_iterator it = find(bstore.begin(),
197 LASSERT(it != bstore.end(), /**/);
199 Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
204 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
205 OutputParams const & runparams)
207 BufferStorage::iterator it = bstore.begin();
208 BufferStorage::iterator end = bstore.end();
209 for (; it != end; ++it) {
210 if (!(*it)->isDepClean(masterTmpDir)) {
211 string writefile = addName(masterTmpDir, (*it)->latexName());
212 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
214 (*it)->markDepClean(masterTmpDir);
220 void BufferList::emergencyWriteAll()
222 for_each(bstore.begin(), bstore.end(),
223 bind(&BufferList::emergencyWrite, this, _1));
227 docstring BufferList::emergencyWrite(Buffer * buf)
229 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
230 // compare with 0 to avoid pointer/interger comparison
231 // ::assert(buf != 0);
233 return _("No file open!");
235 // No need to save if the buffer has not changed.
239 string const doc = buf->isUnnamed()
240 ? onlyFilename(buf->absFileName()) : buf->absFileName();
242 docstring user_message = bformat(
243 _("LyX: Attempting to save document %1$s\n"), from_utf8(doc));
245 // We try to save three places:
246 // 1) Same place as document. Unless it is an unnamed doc.
247 if (!buf->isUnnamed()) {
248 string s = buf->absFileName();
250 lyxerr << " " << s << endl;
251 if (buf->writeFile(FileName(s))) {
253 user_message += _(" Save seems successful. Phew.\n");
256 user_message += _(" Save failed! Trying...\n");
260 // 2) In HOME directory.
261 string s = addName(package().home_dir().absFilename(), buf->absFileName());
263 lyxerr << ' ' << s << endl;
264 if (buf->writeFile(FileName(s))) {
266 user_message += _(" Save seems successful. Phew.\n");
270 user_message += _(" Save failed! Trying...\n");
272 // 3) In "/tmp" directory.
273 // MakeAbsPath to prepend the current
274 // drive letter on OS/2
275 s = addName(package().temp_dir().absFilename(), buf->absFileName());
277 lyxerr << ' ' << s << endl;
278 if (buf->writeFile(FileName(s))) {
280 user_message += _(" Save seems successful. Phew.\n");
284 user_message += _(" Save failed! Bummer. Document is lost.");
289 bool BufferList::exists(FileName const & fname) const
291 return getBuffer(fname) != 0;
295 bool BufferList::isLoaded(Buffer const * b) const
297 BufferStorage::const_iterator cit =
298 find(bstore.begin(), bstore.end(), b);
299 return cit != bstore.end();
304 struct equivalent_to : public binary_function<FileName, FileName, bool>
306 bool operator()(FileName const & x, FileName const & y) const
307 { return equivalent(x, y); }
312 Buffer * BufferList::getBuffer(support::FileName const & fname) const
314 // 1) cheap test, using string comparison of file names
315 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
316 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
317 if (it != bstore.end())
319 // 2) possibly expensive test, using quivalence test of file names
320 it = find_if(bstore.begin(), bstore.end(),
321 bind(equivalent_to(), bind(&Buffer::fileName, _1), fname));
322 return it != bstore.end() ? (*it) : 0;
326 Buffer * BufferList::getBufferFromTmp(string const & s)
328 BufferStorage::iterator it = bstore.begin();
329 BufferStorage::iterator end = bstore.end();
330 for (; it < end; ++it)
331 if (prefixIs(s, (*it)->temppath()))
337 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
339 BufferStorage::iterator it = bstore.begin();
340 BufferStorage::iterator end = bstore.end();
341 for (; it != end; ++it)
342 (*it)->params().authors().record(0, Author(name, email));
346 int BufferList::bufferNum(FileName const & fname) const
348 FileNameList const & buffers = fileNames();
349 FileNameList::const_iterator cit =
350 find(buffers.begin(), buffers.end(), fname);
351 if (cit == buffers.end())
353 return int(cit - buffers.begin());
357 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
359 LASSERT(parent, return false);
360 LASSERT(child, return false);
361 LASSERT(parent->isChild(child), return false);
363 // Child document has a different parent, don't close it.
364 if (child->parent() != parent)
367 BufferStorage::iterator it = bstore.begin();
368 BufferStorage::iterator end = bstore.end();
369 for (; it != end; ++it) {
371 if (buf != parent && buf->isChild(child)) {