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 "support/bind.h"
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::newInternalBuffer(string const & s)
109 Buffer * const buf = createNewBuffer(s);
111 buf->setInternal(true);
112 binternal.push_back(buf);
118 Buffer * BufferList::newBuffer(string const & s)
120 Buffer * const buf = createNewBuffer(s);
122 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
123 bstore.push_back(buf);
129 Buffer * BufferList::createNewBuffer(string const & s)
131 auto_ptr<Buffer> tmpbuf;
133 tmpbuf.reset(new Buffer(s));
134 } catch (ExceptionMessage const & message) {
135 if (message.type_ == ErrorException) {
136 Alert::error(message.title_, message.details_);
138 } else if (message.type_ == WarningException) {
139 Alert::warning(message.title_, message.details_);
143 tmpbuf->params().useClassDefaults();
144 return tmpbuf.release();
148 void BufferList::closeAll()
150 while (!bstore.empty())
151 release(bstore.front());
155 FileNameList const & BufferList::fileNames() const
157 static FileNameList nvec;
159 BufferStorage::const_iterator it = bstore.begin();
160 BufferStorage::const_iterator end = bstore.end();
161 for (; it != end; ++it) {
163 nvec.push_back(buf->fileName());
169 Buffer * BufferList::first()
173 return bstore.front();
177 Buffer * BufferList::last()
181 return bstore.back();
185 Buffer * BufferList::getBuffer(unsigned int choice)
187 if (choice >= bstore.size())
189 return bstore[choice];
193 Buffer * BufferList::next(Buffer const * buf) const
199 BufferStorage::const_iterator it =
200 find(bstore.begin(), bstore.end(), buf);
201 LASSERT(it != bstore.end(), /**/);
203 Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
208 Buffer * BufferList::previous(Buffer const * buf) const
214 BufferStorage::const_iterator it =
215 find(bstore.begin(), bstore.end(), buf);
216 LASSERT(it != bstore.end(), /**/);
218 Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
223 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
224 OutputParams const & runparams)
226 BufferStorage::iterator it = bstore.begin();
227 BufferStorage::iterator end = bstore.end();
228 for (; it != end; ++it) {
229 if (!(*it)->isDepClean(masterTmpDir)) {
230 string writefile = addName(masterTmpDir, (*it)->latexName());
231 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
232 runparams, Buffer::OnlyBody);
233 (*it)->markDepClean(masterTmpDir);
239 void BufferList::emergencyWriteAll()
241 BufferStorage::const_iterator it = bstore.begin();
242 BufferStorage::const_iterator const en = bstore.end();
243 for (; it != en; ++it)
244 (*it)->emergencyWrite();
248 bool BufferList::exists(FileName const & fname) const
250 return getBuffer(fname) != 0;
254 bool BufferList::isLoaded(Buffer const * b) const
258 BufferStorage::const_iterator cit =
259 find(bstore.begin(), bstore.end(), b);
260 return cit != bstore.end();
265 struct equivalent_to : public binary_function<FileName, FileName, bool>
267 bool operator()(FileName const & x, FileName const & y) const
268 { return equivalent(x, y); }
273 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
275 // 1) cheap test, using string comparison of file names
276 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
277 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
278 if (it != bstore.end())
280 // 2) possibly expensive test, using equivalence test of file names
281 it = find_if(bstore.begin(), bstore.end(),
282 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
283 if (it != bstore.end())
287 // 1) cheap test, using string comparison of file names
288 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
289 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
290 if (it != binternal.end())
292 // 2) possibly expensive test, using equivalence test of file names
293 it = find_if(binternal.begin(), binternal.end(),
294 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
295 if (it != binternal.end())
303 Buffer * BufferList::getBufferFromTmp(string const & s)
305 BufferStorage::iterator it = bstore.begin();
306 BufferStorage::iterator end = bstore.end();
307 for (; it < end; ++it) {
308 if (prefixIs(s, (*it)->temppath())) {
309 // check whether the filename matches the master
310 string const master_name = (*it)->latexName();
311 if (suffixIs(s, master_name))
313 // if not, try with the children
314 ListOfBuffers clist = (*it)->getDescendents();
315 ListOfBuffers::const_iterator cit = clist.begin();
316 ListOfBuffers::const_iterator cend = clist.end();
317 for (; cit != cend; ++cit) {
318 string const mangled_child_name = DocFileName(
319 changeExtension((*cit)->absFileName(),
320 ".tex")).mangledFileName();
321 if (suffixIs(s, mangled_child_name))
330 void BufferList::recordCurrentAuthor(Author const & author)
332 BufferStorage::iterator it = bstore.begin();
333 BufferStorage::iterator end = bstore.end();
334 for (; it != end; ++it)
335 (*it)->params().authors().recordCurrentAuthor(author);
339 int BufferList::bufferNum(FileName const & fname) const
341 FileNameList const & buffers = fileNames();
342 FileNameList::const_iterator cit =
343 find(buffers.begin(), buffers.end(), fname);
344 if (cit == buffers.end())
346 return int(cit - buffers.begin());
350 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
352 LASSERT(parent, return false);
353 LASSERT(child, return false);
354 LASSERT(parent->isChild(child), return false);
356 // Child document has a different parent, don't close it.
357 Buffer const * parent_ = child->parent();
358 if (parent_ && parent_ != parent)
361 BufferStorage::iterator it = bstore.begin();
362 BufferStorage::iterator end = bstore.end();
363 for (; it != end; ++it) {
365 if (buf != parent && buf->isChild(child)) {
375 void BufferList::changed(bool update_metrics) const
377 BufferStorage::const_iterator it = bstore.begin();
378 BufferStorage::const_iterator end = bstore.end();
379 for (; it != end; ++it)
380 (*it)->changed(update_metrics);
381 it = binternal.begin();
382 end = binternal.end();
383 for (; it != end; ++it)
384 (*it)->changed(update_metrics);