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), runparams, false);
216 (*it)->markDepClean(masterTmpDir);
222 void BufferList::emergencyWriteAll()
224 BufferStorage::const_iterator it = bstore.begin();
225 BufferStorage::const_iterator const en = bstore.end();
226 for (; it != en; ++it)
227 (*it)->emergencyWrite();
231 bool BufferList::exists(FileName const & fname) const
233 return getBuffer(fname) != 0;
237 bool BufferList::isLoaded(Buffer const * b) const
241 BufferStorage::const_iterator cit =
242 find(bstore.begin(), bstore.end(), b);
243 return cit != bstore.end();
248 struct equivalent_to : public binary_function<FileName, FileName, bool>
250 bool operator()(FileName const & x, FileName const & y) const
251 { return equivalent(x, y); }
256 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
258 // 1) cheap test, using string comparison of file names
259 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
260 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
261 if (it != bstore.end())
263 // 2) possibly expensive test, using equivalence test of file names
264 it = find_if(bstore.begin(), bstore.end(),
265 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
266 if (it != bstore.end())
270 // 1) cheap test, using string comparison of file names
271 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
272 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
273 if (it != binternal.end())
275 // 2) possibly expensive test, using equivalence test of file names
276 it = find_if(binternal.begin(), binternal.end(),
277 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
278 if (it != binternal.end())
286 Buffer * BufferList::getBufferFromTmp(string const & s)
288 BufferStorage::iterator it = bstore.begin();
289 BufferStorage::iterator end = bstore.end();
290 for (; it < end; ++it) {
291 if (prefixIs(s, (*it)->temppath())) {
292 // check whether the filename matches the master
293 string const master_name = (*it)->latexName();
294 if (suffixIs(s, master_name))
296 // if not, try with the children
297 ListOfBuffers clist = (*it)->getDescendents();
298 ListOfBuffers::const_iterator cit = clist.begin();
299 ListOfBuffers::const_iterator cend = clist.end();
300 for (; cit != cend; ++cit) {
301 string const mangled_child_name = DocFileName(
302 changeExtension((*cit)->absFileName(),
303 ".tex")).mangledFileName();
304 if (suffixIs(s, mangled_child_name))
313 void BufferList::recordCurrentAuthor(Author const & author)
315 BufferStorage::iterator it = bstore.begin();
316 BufferStorage::iterator end = bstore.end();
317 for (; it != end; ++it)
318 (*it)->params().authors().recordCurrentAuthor(author);
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 Buffer const * parent_ = child->parent();
341 if (parent_ && parent_ != parent)
344 BufferStorage::iterator it = bstore.begin();
345 BufferStorage::iterator end = bstore.end();
346 for (; it != end; ++it) {
348 if (buf != parent && buf->isChild(child)) {
358 void BufferList::changed(bool update_metrics) const
360 BufferStorage::const_iterator it = bstore.begin();
361 BufferStorage::const_iterator end = bstore.end();
362 for (; it != end; ++it)
363 (*it)->changed(update_metrics);
364 it = binternal.begin();
365 end = binternal.end();
366 for (; it != end; ++it)
367 (*it)->changed(update_metrics);