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 unique_ptr<Buffer> tmpbuf;
135 tmpbuf = make_unique<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 BufferList::fileNames() const
160 BufferStorage::const_iterator it = bstore.begin();
161 BufferStorage::const_iterator end = bstore.end();
162 for (; it != end; ++it) {
164 nvec.push_back(buf->fileName());
170 Buffer * BufferList::first()
174 return bstore.front();
178 Buffer * BufferList::last()
182 return bstore.back();
186 Buffer * BufferList::getBuffer(unsigned int choice)
188 if (choice >= bstore.size())
190 return bstore[choice];
194 Buffer * BufferList::next(Buffer const * buf) const
196 // Something is wrong, but we can probably survive it.
197 LASSERT(buf, return 0);
201 BufferStorage::const_iterator it =
202 find(bstore.begin(), bstore.end(), buf);
203 LASSERT(it != bstore.end(), return 0);
205 Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
210 Buffer * BufferList::previous(Buffer const * buf) const
212 // Something is wrong, but we can probably survive it.
213 LASSERT(buf, return 0);
217 BufferStorage::const_iterator it =
218 find(bstore.begin(), bstore.end(), buf);
219 LASSERT(it != bstore.end(), return 0);
221 Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
226 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
227 OutputParams const & runparams_in)
229 OutputParams runparams = runparams_in;
230 runparams.is_child = true;
231 BufferStorage::iterator it = bstore.begin();
232 BufferStorage::iterator end = bstore.end();
233 for (; it != end; ++it) {
234 if (!(*it)->isDepClean(masterTmpDir)) {
235 string writefile = addName(masterTmpDir, (*it)->latexName());
236 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
237 runparams, Buffer::OnlyBody);
238 (*it)->markDepClean(masterTmpDir);
241 runparams.is_child = false;
245 void BufferList::emergencyWriteAll()
247 BufferStorage::const_iterator it = bstore.begin();
248 BufferStorage::const_iterator const en = bstore.end();
249 for (; it != en; ++it)
250 (*it)->emergencyWrite();
254 bool BufferList::exists(FileName const & fname) const
256 return getBuffer(fname) != 0;
260 bool BufferList::isLoaded(Buffer const * b) const
264 BufferStorage::const_iterator cit =
265 find(bstore.begin(), bstore.end(), b);
266 return cit != bstore.end();
270 bool BufferList::isOthersChild(Buffer * parent, Buffer * child)
272 LASSERT(parent, return false);
273 LASSERT(child, return false);
274 LASSERT(parent->isChild(child), return false);
276 // Does child document have a different parent?
277 Buffer const * parent_ = child->parent();
278 if (parent_ && parent_ != parent)
281 BufferStorage::iterator it = bstore.begin();
282 BufferStorage::iterator end = bstore.end();
283 for (; it != end; ++it) {
285 if (buf != parent && buf->isChild(child))
294 struct equivalent_to : public binary_function<FileName, FileName, bool>
296 bool operator()(FileName const & x, FileName const & y) const
297 { return equivalent(x, y); }
303 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
305 // 1) cheap test, using string comparison of file names
306 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
307 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
308 if (it != bstore.end())
310 // 2) possibly expensive test, using equivalence test of file names
311 it = find_if(bstore.begin(), bstore.end(),
312 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
313 if (it != bstore.end())
317 // 1) cheap test, using string comparison of file names
318 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
319 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
320 if (it != binternal.end())
322 // 2) possibly expensive test, using equivalence test of file names
323 it = find_if(binternal.begin(), binternal.end(),
324 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
325 if (it != binternal.end())
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);