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 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();
272 struct equivalent_to : public binary_function<FileName, FileName, bool>
274 bool operator()(FileName const & x, FileName const & y) const
275 { return equivalent(x, y); }
281 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
283 // 1) cheap test, using string comparison of file names
284 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
285 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
286 if (it != bstore.end())
288 // 2) possibly expensive test, using equivalence test of file names
289 it = find_if(bstore.begin(), bstore.end(),
290 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
291 if (it != bstore.end())
295 // 1) cheap test, using string comparison of file names
296 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
297 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
298 if (it != binternal.end())
300 // 2) possibly expensive test, using equivalence test of file names
301 it = find_if(binternal.begin(), binternal.end(),
302 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
303 if (it != binternal.end())
311 Buffer * BufferList::getBufferFromTmp(string const & s)
313 BufferStorage::iterator it = bstore.begin();
314 BufferStorage::iterator end = bstore.end();
315 for (; it < end; ++it) {
316 if (prefixIs(s, (*it)->temppath())) {
317 // check whether the filename matches the master
318 string const master_name = (*it)->latexName();
319 if (suffixIs(s, master_name))
321 // if not, try with the children
322 ListOfBuffers clist = (*it)->getDescendents();
323 ListOfBuffers::const_iterator cit = clist.begin();
324 ListOfBuffers::const_iterator cend = clist.end();
325 for (; cit != cend; ++cit) {
326 string const mangled_child_name = DocFileName(
327 changeExtension((*cit)->absFileName(),
328 ".tex")).mangledFileName();
329 if (suffixIs(s, mangled_child_name))
338 void BufferList::recordCurrentAuthor(Author const & author)
340 BufferStorage::iterator it = bstore.begin();
341 BufferStorage::iterator end = bstore.end();
342 for (; it != end; ++it)
343 (*it)->params().authors().recordCurrentAuthor(author);
347 int BufferList::bufferNum(FileName const & fname) const
349 FileNameList const buffers(fileNames());
350 FileNameList::const_iterator cit =
351 find(buffers.begin(), buffers.end(), fname);
352 if (cit == buffers.end())
354 return int(cit - buffers.begin());
358 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
360 LASSERT(parent, return false);
361 LASSERT(child, return false);
362 LASSERT(parent->isChild(child), return false);
364 // Child document has a different parent, don't close it.
365 Buffer const * parent_ = child->parent();
366 if (parent_ && parent_ != parent)
369 BufferStorage::iterator it = bstore.begin();
370 BufferStorage::iterator end = bstore.end();
371 for (; it != end; ++it) {
373 if (buf != parent && buf->isChild(child)) {
383 void BufferList::changed(bool update_metrics) const
385 BufferStorage::const_iterator it = bstore.begin();
386 BufferStorage::const_iterator end = bstore.end();
387 for (; it != end; ++it)
388 (*it)->changed(update_metrics);
389 it = binternal.begin();
390 end = binternal.end();
391 for (; it != end; ++it)
392 (*it)->changed(update_metrics);