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/os.h"
33 #include "support/Package.h"
35 #include "support/lassert.h"
36 #include <boost/bind.hpp>
44 using namespace lyx::support;
48 namespace Alert = lyx::frontend::Alert;
51 BufferList::BufferList()
55 BufferList::~BufferList()
57 BufferStorage::iterator it = binternal.begin();
58 BufferStorage::iterator end = binternal.end();
59 for (; it != end; ++it)
64 bool BufferList::empty() const
66 return bstore.empty();
70 BufferList::iterator BufferList::begin()
72 return bstore.begin();
76 BufferList::const_iterator BufferList::begin() const
78 return bstore.begin();
82 BufferList::iterator BufferList::end()
88 BufferList::const_iterator BufferList::end() const
94 void BufferList::release(Buffer * buf)
97 BufferStorage::iterator const it =
98 find(bstore.begin(), bstore.end(), buf);
99 if (it != bstore.end()) {
100 Buffer * tmp = (*it);
108 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
110 auto_ptr<Buffer> tmpbuf;
112 tmpbuf.reset(new Buffer(s, ronly));
113 } catch (ExceptionMessage const & message) {
114 if (message.type_ == ErrorException) {
115 Alert::error(message.title_, message.details_);
117 } else if (message.type_ == WarningException) {
118 Alert::warning(message.title_, message.details_);
122 tmpbuf->params().useClassDefaults();
123 if (tmpbuf->fileName().extension() == "internal") {
124 binternal.push_back(tmpbuf.get());
126 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
127 bstore.push_back(tmpbuf.get());
129 return tmpbuf.release();
133 void BufferList::closeAll()
135 while (!bstore.empty())
136 release(bstore.front());
140 FileNameList const & BufferList::fileNames() const
142 static FileNameList nvec;
144 transform(bstore.begin(), bstore.end(),
146 boost::bind(&Buffer::fileName, _1));
151 Buffer * BufferList::first()
155 return bstore.front();
159 Buffer * BufferList::last()
163 return bstore.back();
167 Buffer * BufferList::getBuffer(unsigned int choice)
169 if (choice >= bstore.size())
171 return bstore[choice];
175 Buffer * BufferList::next(Buffer const * buf) const
181 BufferStorage::const_iterator it = find(bstore.begin(),
183 LASSERT(it != bstore.end(), /**/);
185 Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
190 Buffer * BufferList::previous(Buffer const * buf) const
196 BufferStorage::const_iterator it = find(bstore.begin(),
198 LASSERT(it != bstore.end(), /**/);
200 Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
205 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
206 OutputParams const & runparams)
208 BufferStorage::iterator it = bstore.begin();
209 BufferStorage::iterator end = bstore.end();
210 for (; it != end; ++it) {
211 if (!(*it)->isDepClean(masterTmpDir)) {
212 string writefile = addName(masterTmpDir, (*it)->latexName());
213 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
215 (*it)->markDepClean(masterTmpDir);
221 void BufferList::emergencyWriteAll()
223 for_each(bstore.begin(), bstore.end(),
224 bind(&BufferList::emergencyWrite, this, _1));
228 docstring BufferList::emergencyWrite(Buffer * buf)
230 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
231 // compare with 0 to avoid pointer/interger comparison
232 // ::assert(buf != 0);
234 return _("No file open!");
236 // No need to save if the buffer has not changed.
240 string const doc = buf->isUnnamed()
241 ? onlyFilename(buf->absFileName()) : buf->absFileName();
243 docstring user_message = bformat(
244 _("LyX: Attempting to save document %1$s\n"), from_utf8(doc));
246 // We try to save three places:
247 // 1) Same place as document. Unless it is an unnamed doc.
248 if (!buf->isUnnamed()) {
249 string s = buf->absFileName();
251 lyxerr << " " << s << endl;
252 if (buf->writeFile(FileName(s))) {
254 user_message += _(" Save seems successful. Phew.\n");
257 user_message += _(" Save failed! Trying...\n");
261 // 2) In HOME directory.
262 string s = addName(package().home_dir().absFilename(), buf->absFileName());
264 lyxerr << ' ' << s << endl;
265 if (buf->writeFile(FileName(s))) {
267 user_message += _(" Save seems successful. Phew.\n");
271 user_message += _(" Save failed! Trying...\n");
273 // 3) In "/tmp" directory.
274 // MakeAbsPath to prepend the current
275 // drive letter on OS/2
276 s = addName(package().temp_dir().absFilename(), buf->absFileName());
278 lyxerr << ' ' << s << endl;
279 if (buf->writeFile(FileName(s))) {
281 user_message += _(" Save seems successful. Phew.\n");
285 user_message += _(" Save failed! Bummer. Document is lost.");
290 bool BufferList::exists(FileName const & fname) const
292 return getBuffer(fname) != 0;
296 bool BufferList::isLoaded(Buffer const * b) const
298 BufferStorage::const_iterator cit =
299 find(bstore.begin(), bstore.end(), b);
300 return cit != bstore.end();
305 struct equivalent_to : public binary_function<FileName, FileName, bool>
307 bool operator()(FileName const & x, FileName const & y) const
308 { return equivalent(x, y); }
313 Buffer * BufferList::getBuffer(support::FileName const & fname) const
315 // 1) cheap test, using string comparison of file names
316 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
317 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
318 if (it != bstore.end())
320 // 2) possibly expensive test, using quivalence test of file names
321 it = find_if(bstore.begin(), bstore.end(),
322 bind(equivalent_to(), bind(&Buffer::fileName, _1), fname));
323 return it != bstore.end() ? (*it) : 0;
327 Buffer * BufferList::getBufferFromTmp(string const & s)
329 BufferStorage::iterator it = bstore.begin();
330 BufferStorage::iterator end = bstore.end();
331 for (; it < end; ++it)
332 if (os::path_prefix_is(s, (*it)->temppath()))
338 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
340 BufferStorage::iterator it = bstore.begin();
341 BufferStorage::iterator end = bstore.end();
342 for (; it != end; ++it)
343 (*it)->params().authors().record(0, Author(name, email));
347 int BufferList::bufferNum(FileName const & fname) const
349 FileNameList const & buffers = fileNames();
350 FileNameList::const_iterator cit =
351 find(buffers.begin(), buffers.end(), fname);
352 if (cit == buffers.end())
354 return int(cit - buffers.begin());
358 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
360 LASSERT(parent, return false);
361 LASSERT(child, return false);
362 LASSERT(parent->isChild(child), return false);
364 // Child document has a different parent, don't close it.
365 if (child->parent() != parent)
368 BufferStorage::iterator it = bstore.begin();
369 BufferStorage::iterator end = bstore.end();
370 for (; it != end; ++it) {
372 if (buf != parent && buf->isChild(child)) {