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_in)
226 OutputParams runparams = runparams_in;
227 runparams.is_child = true;
228 BufferStorage::iterator it = bstore.begin();
229 BufferStorage::iterator end = bstore.end();
230 for (; it != end; ++it) {
231 if (!(*it)->isDepClean(masterTmpDir)) {
232 string writefile = addName(masterTmpDir, (*it)->latexName());
233 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
234 runparams, Buffer::OnlyBody);
235 (*it)->markDepClean(masterTmpDir);
238 runparams.is_child = false;
242 void BufferList::emergencyWriteAll()
244 BufferStorage::const_iterator it = bstore.begin();
245 BufferStorage::const_iterator const en = bstore.end();
246 for (; it != en; ++it)
247 (*it)->emergencyWrite();
251 bool BufferList::exists(FileName const & fname) const
253 return getBuffer(fname) != 0;
257 bool BufferList::isLoaded(Buffer const * b) const
261 BufferStorage::const_iterator cit =
262 find(bstore.begin(), bstore.end(), b);
263 return cit != bstore.end();
269 struct equivalent_to : public binary_function<FileName, FileName, bool>
271 bool operator()(FileName const & x, FileName const & y) const
272 { return equivalent(x, y); }
278 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
280 // 1) cheap test, using string comparison of file names
281 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
282 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
283 if (it != bstore.end())
285 // 2) possibly expensive test, using equivalence test of file names
286 it = find_if(bstore.begin(), bstore.end(),
287 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
288 if (it != bstore.end())
292 // 1) cheap test, using string comparison of file names
293 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
294 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
295 if (it != binternal.end())
297 // 2) possibly expensive test, using equivalence test of file names
298 it = find_if(binternal.begin(), binternal.end(),
299 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
300 if (it != binternal.end())
308 Buffer * BufferList::getBufferFromTmp(string const & s)
310 BufferStorage::iterator it = bstore.begin();
311 BufferStorage::iterator end = bstore.end();
312 for (; it < end; ++it) {
313 if (prefixIs(s, (*it)->temppath())) {
314 // check whether the filename matches the master
315 string const master_name = (*it)->latexName();
316 if (suffixIs(s, master_name))
318 // if not, try with the children
319 ListOfBuffers clist = (*it)->getDescendents();
320 ListOfBuffers::const_iterator cit = clist.begin();
321 ListOfBuffers::const_iterator cend = clist.end();
322 for (; cit != cend; ++cit) {
323 string const mangled_child_name = DocFileName(
324 changeExtension((*cit)->absFileName(),
325 ".tex")).mangledFileName();
326 if (suffixIs(s, mangled_child_name))
335 void BufferList::recordCurrentAuthor(Author const & author)
337 BufferStorage::iterator it = bstore.begin();
338 BufferStorage::iterator end = bstore.end();
339 for (; it != end; ++it)
340 (*it)->params().authors().recordCurrentAuthor(author);
344 int BufferList::bufferNum(FileName const & fname) const
346 FileNameList const & buffers = fileNames();
347 FileNameList::const_iterator cit =
348 find(buffers.begin(), buffers.end(), fname);
349 if (cit == buffers.end())
351 return int(cit - buffers.begin());
355 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
357 LASSERT(parent, return false);
358 LASSERT(child, return false);
359 LASSERT(parent->isChild(child), return false);
361 // Child document has a different parent, don't close it.
362 Buffer const * parent_ = child->parent();
363 if (parent_ && parent_ != parent)
366 BufferStorage::iterator it = bstore.begin();
367 BufferStorage::iterator end = bstore.end();
368 for (; it != end; ++it) {
370 if (buf != parent && buf->isChild(child)) {
380 void BufferList::changed(bool update_metrics) const
382 BufferStorage::const_iterator it = bstore.begin();
383 BufferStorage::const_iterator end = bstore.end();
384 for (; it != end; ++it)
385 (*it)->changed(update_metrics);
386 it = binternal.begin();
387 end = binternal.end();
388 for (; it != end; ++it)
389 (*it)->changed(update_metrics);