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)
95 // We may leak here, but we probably do not need to
98 BufferStorage::iterator const it =
99 find(bstore.begin(), bstore.end(), buf);
100 if (it != bstore.end()) {
101 Buffer * tmp = (*it);
103 LASSERT(tmp, return);
109 Buffer * BufferList::newInternalBuffer(string const & s)
111 Buffer * const buf = createNewBuffer(s);
113 buf->setInternal(true);
114 binternal.push_back(buf);
120 Buffer * BufferList::newBuffer(string const & s)
122 Buffer * const buf = createNewBuffer(s);
124 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
125 bstore.push_back(buf);
131 Buffer * BufferList::createNewBuffer(string const & s)
133 auto_ptr<Buffer> tmpbuf;
135 tmpbuf.reset(new Buffer(s));
136 } catch (ExceptionMessage const & message) {
137 if (message.type_ == ErrorException) {
138 Alert::error(message.title_, message.details_);
140 } else if (message.type_ == WarningException) {
141 Alert::warning(message.title_, message.details_);
145 tmpbuf->params().useClassDefaults();
146 return tmpbuf.release();
150 void BufferList::closeAll()
152 while (!bstore.empty())
153 release(bstore.front());
157 FileNameList const & BufferList::fileNames() const
160 static FileNameList nvec;
162 BufferStorage::const_iterator it = bstore.begin();
163 BufferStorage::const_iterator end = bstore.end();
164 for (; it != end; ++it) {
166 nvec.push_back(buf->fileName());
172 Buffer * BufferList::first()
176 return bstore.front();
180 Buffer * BufferList::last()
184 return bstore.back();
188 Buffer * BufferList::getBuffer(unsigned int choice)
190 if (choice >= bstore.size())
192 return bstore[choice];
196 Buffer * BufferList::next(Buffer const * buf) const
198 // Something is wrong, but we can probably survive it.
199 LASSERT(buf, return 0);
203 BufferStorage::const_iterator it =
204 find(bstore.begin(), bstore.end(), buf);
205 LASSERT(it != bstore.end(), return 0);
207 Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
212 Buffer * BufferList::previous(Buffer const * buf) const
214 // Something is wrong, but we can probably survive it.
215 LASSERT(buf, return 0);
219 BufferStorage::const_iterator it =
220 find(bstore.begin(), bstore.end(), buf);
221 LASSERT(it != bstore.end(), return 0);
223 Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
228 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
229 OutputParams const & runparams_in)
231 OutputParams runparams = runparams_in;
232 runparams.is_child = true;
233 BufferStorage::iterator it = bstore.begin();
234 BufferStorage::iterator end = bstore.end();
235 for (; it != end; ++it) {
236 if (!(*it)->isDepClean(masterTmpDir)) {
237 string writefile = addName(masterTmpDir, (*it)->latexName());
238 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
239 runparams, Buffer::OnlyBody);
240 (*it)->markDepClean(masterTmpDir);
243 runparams.is_child = false;
247 void BufferList::emergencyWriteAll()
249 BufferStorage::const_iterator it = bstore.begin();
250 BufferStorage::const_iterator const en = bstore.end();
251 for (; it != en; ++it)
252 (*it)->emergencyWrite();
256 bool BufferList::exists(FileName const & fname) const
258 return getBuffer(fname) != 0;
262 bool BufferList::isLoaded(Buffer const * b) const
266 BufferStorage::const_iterator cit =
267 find(bstore.begin(), bstore.end(), b);
268 return cit != bstore.end();
274 struct equivalent_to : public binary_function<FileName, FileName, bool>
276 bool operator()(FileName const & x, FileName const & y) const
277 { return equivalent(x, y); }
283 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
285 // 1) cheap test, using string comparison of file names
286 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
287 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
288 if (it != bstore.end())
290 // 2) possibly expensive test, using equivalence test of file names
291 it = find_if(bstore.begin(), bstore.end(),
292 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
293 if (it != bstore.end())
297 // 1) cheap test, using string comparison of file names
298 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
299 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
300 if (it != binternal.end())
302 // 2) possibly expensive test, using equivalence test of file names
303 it = find_if(binternal.begin(), binternal.end(),
304 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
305 if (it != binternal.end())
313 Buffer * BufferList::getBufferFromTmp(string const & s)
315 BufferStorage::iterator it = bstore.begin();
316 BufferStorage::iterator end = bstore.end();
317 for (; it < end; ++it) {
318 if (prefixIs(s, (*it)->temppath())) {
319 // check whether the filename matches the master
320 string const master_name = (*it)->latexName();
321 if (suffixIs(s, master_name))
323 // if not, try with the children
324 ListOfBuffers clist = (*it)->getDescendents();
325 ListOfBuffers::const_iterator cit = clist.begin();
326 ListOfBuffers::const_iterator cend = clist.end();
327 for (; cit != cend; ++cit) {
328 string const mangled_child_name = DocFileName(
329 changeExtension((*cit)->absFileName(),
330 ".tex")).mangledFileName();
331 if (suffixIs(s, mangled_child_name))
340 void BufferList::recordCurrentAuthor(Author const & author)
342 BufferStorage::iterator it = bstore.begin();
343 BufferStorage::iterator end = bstore.end();
344 for (; it != end; ++it)
345 (*it)->params().authors().recordCurrentAuthor(author);
349 int BufferList::bufferNum(FileName const & fname) const
351 FileNameList const & buffers = fileNames();
352 FileNameList::const_iterator cit =
353 find(buffers.begin(), buffers.end(), fname);
354 if (cit == buffers.end())
356 return int(cit - buffers.begin());
360 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
362 LASSERT(parent, return false);
363 LASSERT(child, return false);
364 LASSERT(parent->isChild(child), return false);
366 // Child document has a different parent, don't close it.
367 Buffer const * parent_ = child->parent();
368 if (parent_ && parent_ != parent)
371 BufferStorage::iterator it = bstore.begin();
372 BufferStorage::iterator end = bstore.end();
373 for (; it != end; ++it) {
375 if (buf != parent && buf->isChild(child)) {
385 void BufferList::changed(bool update_metrics) const
387 BufferStorage::const_iterator it = bstore.begin();
388 BufferStorage::const_iterator end = bstore.end();
389 for (; it != end; ++it)
390 (*it)->changed(update_metrics);
391 it = binternal.begin();
392 end = binternal.end();
393 for (; it != end; ++it)
394 (*it)->changed(update_metrics);