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"
41 using namespace lyx::support;
45 namespace Alert = lyx::frontend::Alert;
48 BufferList::BufferList()
52 BufferList::~BufferList()
54 BufferStorage::iterator it = binternal.begin();
55 BufferStorage::iterator end = binternal.end();
56 for (; it != end; ++it)
61 bool BufferList::empty() const
63 return bstore.empty();
67 BufferList::iterator BufferList::begin()
69 return bstore.begin();
73 BufferList::const_iterator BufferList::begin() const
75 return bstore.begin();
79 BufferList::iterator BufferList::end()
85 BufferList::const_iterator BufferList::end() const
91 void BufferList::release(Buffer * buf)
93 // We may leak here, but we probably do not need to
96 BufferStorage::iterator const it =
97 find(bstore.begin(), bstore.end(), buf);
98 if (it != bstore.end()) {
99 Buffer const * parent = buf->parent();
100 Buffer * tmp = (*it);
102 LASSERT(tmp, return);
105 // If this was a child, update the parent's buffer
106 // to avoid crashes due to dangling pointers (bug 9979)
107 parent->updateBuffer();
112 Buffer * BufferList::newInternalBuffer(string const & s)
114 Buffer * const buf = createNewBuffer(s);
116 buf->setInternal(true);
117 binternal.push_back(buf);
123 Buffer * BufferList::newBuffer(string const & s)
125 Buffer * const buf = createNewBuffer(s);
127 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
128 bstore.push_back(buf);
134 Buffer * BufferList::createNewBuffer(string const & s)
136 unique_ptr<Buffer> tmpbuf;
138 tmpbuf = make_unique<Buffer>(s);
139 } catch (ExceptionMessage const & message) {
140 if (message.type_ == ErrorException) {
141 Alert::error(message.title_, message.details_);
143 } else if (message.type_ == WarningException) {
144 Alert::warning(message.title_, message.details_);
148 tmpbuf->params().useClassDefaults();
149 return tmpbuf.release();
153 void BufferList::closeAll()
155 while (!bstore.empty())
156 release(bstore.front());
160 FileNameList BufferList::fileNames() const
163 BufferStorage::const_iterator it = bstore.begin();
164 BufferStorage::const_iterator end = bstore.end();
165 for (; it != end; ++it) {
167 nvec.push_back(buf->fileName());
173 Buffer * BufferList::first()
177 return bstore.front();
181 Buffer * BufferList::last()
185 return bstore.back();
189 Buffer * BufferList::getBuffer(unsigned int choice)
191 if (choice >= bstore.size())
193 return bstore[choice];
197 Buffer * BufferList::next(Buffer const * buf) const
199 // Something is wrong, but we can probably survive it.
200 LASSERT(buf, return 0);
204 BufferStorage::const_iterator it =
205 find(bstore.begin(), bstore.end(), buf);
206 LASSERT(it != bstore.end(), return 0);
208 Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
213 Buffer * BufferList::previous(Buffer const * buf) const
215 // Something is wrong, but we can probably survive it.
216 LASSERT(buf, return 0);
220 BufferStorage::const_iterator it =
221 find(bstore.begin(), bstore.end(), buf);
222 LASSERT(it != bstore.end(), return 0);
224 Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
229 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
230 OutputParams const & runparams_in)
232 OutputParams runparams = runparams_in;
233 runparams.is_child = true;
234 BufferStorage::iterator it = bstore.begin();
235 BufferStorage::iterator end = bstore.end();
236 for (; it != end; ++it) {
237 if (!(*it)->isDepClean(masterTmpDir)) {
238 string writefile = addName(masterTmpDir, (*it)->latexName());
239 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
240 runparams, Buffer::OnlyBody);
241 (*it)->markDepClean(masterTmpDir);
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 void BufferList::invalidateConverterCache() const
258 BufferStorage::const_iterator it = bstore.begin();
259 BufferStorage::const_iterator const en = bstore.end();
260 for (; it != en; ++it)
261 (*it)->params().invalidateConverterCache();
265 bool BufferList::exists(FileName const & fname) const
267 return getBuffer(fname) != 0;
271 bool BufferList::isLoaded(Buffer const * b) const
275 BufferStorage::const_iterator cit =
276 find(bstore.begin(), bstore.end(), b);
277 return cit != bstore.end();
281 bool BufferList::isInternal(Buffer const * b) const
285 BufferStorage::const_iterator cit =
286 find(binternal.begin(), binternal.end(), b);
287 return cit != binternal.end();
291 bool BufferList::isOthersChild(Buffer * parent, Buffer * child)
293 LASSERT(parent, return false);
294 LASSERT(child, return false);
295 LASSERT(parent->isChild(child), return false);
297 // Does child document have a different parent?
298 Buffer const * parent_ = child->parent();
299 if (parent_ && parent_ != parent)
302 for(Buffer * buf : bstore)
303 if (buf != parent && buf->isChild(child))
309 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
311 // 1) cheap test, using string comparison of file names
312 for (Buffer * b : bstore)
313 if (b->fileName() == fname)
315 // 2) possibly expensive test, using equivalence test of file names
316 for (Buffer * b : bstore)
317 if (equivalent(b->fileName(), fname))
320 // 1) cheap test, using string comparison of file names
321 for (Buffer * b : binternal)
322 if (b->fileName() == fname)
324 // 2) possibly expensive test, using equivalence test of file names
325 for (Buffer * b : binternal)
326 if (equivalent(b->fileName(), fname))
333 Buffer * BufferList::getBufferFromTmp(string const & s)
335 BufferStorage::iterator it = bstore.begin();
336 BufferStorage::iterator end = bstore.end();
337 for (; it < end; ++it) {
338 if (prefixIs(s, (*it)->temppath())) {
339 // check whether the filename matches the master
340 string const master_name = (*it)->latexName();
341 if (suffixIs(s, master_name))
343 // if not, try with the children
344 ListOfBuffers clist = (*it)->getDescendents();
345 ListOfBuffers::const_iterator cit = clist.begin();
346 ListOfBuffers::const_iterator cend = clist.end();
347 for (; cit != cend; ++cit) {
348 string const mangled_child_name = DocFileName(
349 changeExtension((*cit)->absFileName(),
350 ".tex")).mangledFileName();
351 if (suffixIs(s, mangled_child_name))
360 void BufferList::recordCurrentAuthor(Author const & author)
362 BufferStorage::iterator it = bstore.begin();
363 BufferStorage::iterator end = bstore.end();
364 for (; it != end; ++it)
365 (*it)->params().authors().recordCurrentAuthor(author);
369 void BufferList::updatePreviews()
371 BufferStorage::iterator it = bstore.begin();
372 BufferStorage::iterator end = bstore.end();
373 for (; it != end; ++it)
374 (*it)->updatePreviews();
378 int BufferList::bufferNum(FileName const & fname) const
380 FileNameList const buffers(fileNames());
381 FileNameList::const_iterator cit =
382 find(buffers.begin(), buffers.end(), fname);
383 if (cit == buffers.end())
385 return int(cit - buffers.begin());
389 void BufferList::changed(bool update_metrics) const
391 BufferStorage::const_iterator it = bstore.begin();
392 BufferStorage::const_iterator end = bstore.end();
393 for (; it != end; ++it)
394 (*it)->changed(update_metrics);
395 it = binternal.begin();
396 end = binternal.end();
397 for (; it != end; ++it)
398 (*it)->changed(update_metrics);