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 bool BufferList::empty() const
56 return bstore.empty();
60 BufferList::iterator BufferList::begin()
62 return bstore.begin();
66 BufferList::const_iterator BufferList::begin() const
68 return bstore.begin();
72 BufferList::iterator BufferList::end()
78 BufferList::const_iterator BufferList::end() const
84 void BufferList::release(Buffer * buf)
87 BufferStorage::iterator const it =
88 find(bstore.begin(), bstore.end(), buf);
89 if (it != bstore.end()) {
98 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
100 auto_ptr<Buffer> tmpbuf;
102 tmpbuf.reset(new Buffer(s, ronly));
103 } catch (ExceptionMessage const & message) {
104 if (message.type_ == ErrorException) {
105 Alert::error(message.title_, message.details_);
107 } else if (message.type_ == WarningException) {
108 Alert::warning(message.title_, message.details_);
112 tmpbuf->params().useClassDefaults();
113 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
114 bstore.push_back(tmpbuf.get());
115 return tmpbuf.release();
119 void BufferList::closeAll()
121 while (!bstore.empty())
122 release(bstore.front());
126 FileNameList const & BufferList::fileNames() const
128 static FileNameList nvec;
130 transform(bstore.begin(), bstore.end(),
132 boost::bind(&Buffer::fileName, _1));
137 Buffer * BufferList::first()
141 return bstore.front();
145 Buffer * BufferList::last()
149 return bstore.back();
153 Buffer * BufferList::getBuffer(unsigned int choice)
155 if (choice >= bstore.size())
157 return bstore[choice];
161 Buffer * BufferList::next(Buffer const * buf) const
167 BufferStorage::const_iterator it = find(bstore.begin(),
169 LASSERT(it != bstore.end(), /**/);
171 if (it == bstore.end())
172 return bstore.front();
178 Buffer * BufferList::previous(Buffer const * buf) const
184 BufferStorage::const_iterator it = find(bstore.begin(),
186 LASSERT(it != bstore.end(), /**/);
187 if (it == bstore.begin())
188 return bstore.back();
194 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
195 OutputParams const & runparams)
197 BufferStorage::iterator it = bstore.begin();
198 BufferStorage::iterator end = bstore.end();
199 for (; it != end; ++it) {
200 if (!(*it)->isDepClean(masterTmpDir)) {
201 string writefile = addName(masterTmpDir, (*it)->latexName());
202 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
204 (*it)->markDepClean(masterTmpDir);
210 void BufferList::emergencyWriteAll()
212 for_each(bstore.begin(), bstore.end(),
213 bind(&BufferList::emergencyWrite, this, _1));
217 docstring BufferList::emergencyWrite(Buffer * buf)
219 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
220 // compare with 0 to avoid pointer/interger comparison
221 // ::assert(buf != 0);
223 return _("No file open!");
225 // No need to save if the buffer has not changed.
229 string const doc = buf->isUnnamed()
230 ? onlyFilename(buf->absFileName()) : buf->absFileName();
232 docstring user_message = bformat(
233 _("LyX: Attempting to save document %1$s\n"), from_utf8(doc));
235 // We try to save three places:
236 // 1) Same place as document. Unless it is an unnamed doc.
237 if (!buf->isUnnamed()) {
238 string s = buf->absFileName();
240 lyxerr << " " << s << endl;
241 if (buf->writeFile(FileName(s))) {
243 user_message += _(" Save seems successful. Phew.\n");
246 user_message += _(" Save failed! Trying...\n");
250 // 2) In HOME directory.
251 string s = addName(package().home_dir().absFilename(), buf->absFileName());
253 lyxerr << ' ' << s << endl;
254 if (buf->writeFile(FileName(s))) {
256 user_message += _(" Save seems successful. Phew.\n");
260 user_message += _(" Save failed! Trying...\n");
262 // 3) In "/tmp" directory.
263 // MakeAbsPath to prepend the current
264 // drive letter on OS/2
265 s = addName(package().temp_dir().absFilename(), buf->absFileName());
267 lyxerr << ' ' << s << endl;
268 if (buf->writeFile(FileName(s))) {
270 user_message += _(" Save seems successful. Phew.\n");
274 user_message += _(" Save failed! Bummer. Document is lost.");
279 bool BufferList::exists(FileName const & fname) const
281 return getBuffer(fname) != 0;
285 bool BufferList::isLoaded(Buffer const * b) const
288 BufferStorage::const_iterator cit =
289 find(bstore.begin(), bstore.end(), b);
290 return cit != bstore.end();
294 Buffer * BufferList::getBuffer(support::FileName const & fname) const
296 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
297 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
298 return it != bstore.end() ? (*it) : 0;
302 Buffer * BufferList::getBufferFromTmp(string const & s)
304 BufferStorage::iterator it = bstore.begin();
305 BufferStorage::iterator end = bstore.end();
306 for (; it < end; ++it)
307 if (prefixIs(s, (*it)->temppath()))
313 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
315 BufferStorage::iterator it = bstore.begin();
316 BufferStorage::iterator end = bstore.end();
317 for (; it != end; ++it)
318 (*it)->params().authors().record(0, Author(name, email));
322 int BufferList::bufferNum(FileName const & fname) const
324 FileNameList const & buffers = fileNames();
325 FileNameList::const_iterator cit =
326 find(buffers.begin(), buffers.end(), fname);
327 if (cit == buffers.end())
329 return int(cit - buffers.begin());
333 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
335 LASSERT(parent, return false);
336 LASSERT(child, return false);
337 LASSERT(parent->isChild(child), return false);
339 // Child document has a different parent, don't close it.
340 if (child->parent() != parent)
343 BufferStorage::iterator it = bstore.begin();
344 BufferStorage::iterator end = bstore.end();
345 for (; it != end; ++it) {
347 if (buf != parent && buf->isChild(child)) {