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::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->isInternal()) {
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 BufferStorage::const_iterator it = bstore.begin();
144 BufferStorage::const_iterator end = bstore.end();
145 for (; it != end; ++it) {
147 nvec.push_back(buf->fileName());
153 Buffer * BufferList::first()
157 return bstore.front();
161 Buffer * BufferList::last()
165 return bstore.back();
169 Buffer * BufferList::getBuffer(unsigned int choice)
171 if (choice >= bstore.size())
173 return bstore[choice];
177 Buffer * BufferList::next(Buffer const * buf) const
183 BufferStorage::const_iterator it = find(bstore.begin(),
185 LASSERT(it != bstore.end(), /**/);
187 Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
192 Buffer * BufferList::previous(Buffer const * buf) const
198 BufferStorage::const_iterator it = find(bstore.begin(),
200 LASSERT(it != bstore.end(), /**/);
202 Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
207 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
208 OutputParams const & runparams)
210 BufferStorage::iterator it = bstore.begin();
211 BufferStorage::iterator end = bstore.end();
212 for (; it != end; ++it) {
213 if (!(*it)->isDepClean(masterTmpDir)) {
214 string writefile = addName(masterTmpDir, (*it)->latexName());
215 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
217 (*it)->markDepClean(masterTmpDir);
223 void BufferList::emergencyWriteAll()
225 BufferStorage::const_iterator it = bstore.begin();
226 BufferStorage::const_iterator const en = bstore.end();
227 for (; it != en; ++it)
228 (*it)->emergencyWrite();
232 bool BufferList::exists(FileName const & fname) const
234 return getBuffer(fname) != 0;
238 bool BufferList::isLoaded(Buffer const * b) const
242 BufferStorage::const_iterator cit =
243 find(bstore.begin(), bstore.end(), b);
244 return cit != bstore.end();
249 struct equivalent_to : public binary_function<FileName, FileName, bool>
251 bool operator()(FileName const & x, FileName const & y) const
252 { return equivalent(x, y); }
257 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
259 // 1) cheap test, using string comparison of file names
260 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
261 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
262 if (it != bstore.end())
264 // 2) possibly expensive test, using equivalence test of file names
265 it = find_if(bstore.begin(), bstore.end(),
266 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
267 if (it != bstore.end())
271 // 1) cheap test, using string comparison of file names
272 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
273 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
274 if (it != binternal.end())
276 // 2) possibly expensive test, using equivalence test of file names
277 it = find_if(binternal.begin(), binternal.end(),
278 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
279 if (it != binternal.end())
287 Buffer * BufferList::getBufferFromTmp(string const & s)
289 BufferStorage::iterator it = bstore.begin();
290 BufferStorage::iterator end = bstore.end();
291 for (; it < end; ++it) {
292 if (prefixIs(s, (*it)->temppath())) {
293 // check whether the filename matches the master
294 string const master_name = (*it)->latexName();
295 if (suffixIs(s, master_name))
297 // if not, try with the children
298 ListOfBuffers clist = (*it)->getDescendents();
299 ListOfBuffers::const_iterator cit = clist.begin();
300 ListOfBuffers::const_iterator cend = clist.end();
301 for (; cit != cend; ++cit) {
302 string const mangled_child_name = DocFileName(
303 changeExtension((*cit)->absFileName(),
304 ".tex")).mangledFileName();
305 if (suffixIs(s, mangled_child_name))
314 void BufferList::recordCurrentAuthor(Author const & author)
316 BufferStorage::iterator it = bstore.begin();
317 BufferStorage::iterator end = bstore.end();
318 for (; it != end; ++it)
319 (*it)->params().authors().recordCurrentAuthor(author);
323 int BufferList::bufferNum(FileName const & fname) const
325 FileNameList const & buffers = fileNames();
326 FileNameList::const_iterator cit =
327 find(buffers.begin(), buffers.end(), fname);
328 if (cit == buffers.end())
330 return int(cit - buffers.begin());
334 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
336 LASSERT(parent, return false);
337 LASSERT(child, return false);
338 LASSERT(parent->isChild(child), return false);
340 // Child document has a different parent, don't close it.
341 Buffer const * parent_ = child->parent();
342 if (parent_ && parent_ != parent)
345 BufferStorage::iterator it = bstore.begin();
346 BufferStorage::iterator end = bstore.end();
347 for (; it != end; ++it) {
349 if (buf != parent && buf->isChild(child)) {
359 void BufferList::changed(bool update_metrics) const
361 BufferStorage::const_iterator it = bstore.begin();
362 BufferStorage::const_iterator end = bstore.end();
363 for (; it != end; ++it)
364 (*it)->changed(update_metrics);
365 it = binternal.begin();
366 end = binternal.end();
367 for (; it != end; ++it)
368 (*it)->changed(update_metrics);