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
159 static FileNameList nvec;
161 BufferStorage::const_iterator it = bstore.begin();
162 BufferStorage::const_iterator end = bstore.end();
163 for (; it != end; ++it) {
165 nvec.push_back(buf->fileName());
171 Buffer * BufferList::first()
175 return bstore.front();
179 Buffer * BufferList::last()
183 return bstore.back();
187 Buffer * BufferList::getBuffer(unsigned int choice)
189 if (choice >= bstore.size())
191 return bstore[choice];
195 Buffer * BufferList::next(Buffer const * buf) const
197 // Something is wrong, but we can probably survive it.
198 LASSERT(buf, return 0);
202 BufferStorage::const_iterator it =
203 find(bstore.begin(), bstore.end(), buf);
204 LASSERT(it != bstore.end(), return 0);
206 Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
211 Buffer * BufferList::previous(Buffer const * buf) const
213 // Something is wrong, but we can probably survive it.
214 LASSERT(buf, return 0);
218 BufferStorage::const_iterator it =
219 find(bstore.begin(), bstore.end(), buf);
220 LASSERT(it != bstore.end(), return 0);
222 Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
227 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
228 OutputParams const & runparams_in)
230 OutputParams runparams = runparams_in;
231 runparams.is_child = true;
232 BufferStorage::iterator it = bstore.begin();
233 BufferStorage::iterator end = bstore.end();
234 for (; it != end; ++it) {
235 if (!(*it)->isDepClean(masterTmpDir)) {
236 string writefile = addName(masterTmpDir, (*it)->latexName());
237 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
238 runparams, Buffer::OnlyBody);
239 (*it)->markDepClean(masterTmpDir);
242 runparams.is_child = false;
246 void BufferList::emergencyWriteAll()
248 BufferStorage::const_iterator it = bstore.begin();
249 BufferStorage::const_iterator const en = bstore.end();
250 for (; it != en; ++it)
251 (*it)->emergencyWrite();
255 bool BufferList::exists(FileName const & fname) const
257 return getBuffer(fname) != 0;
261 bool BufferList::isLoaded(Buffer const * b) const
265 BufferStorage::const_iterator cit =
266 find(bstore.begin(), bstore.end(), b);
267 return cit != bstore.end();
273 struct equivalent_to : public binary_function<FileName, FileName, bool>
275 bool operator()(FileName const & x, FileName const & y) const
276 { return equivalent(x, y); }
282 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
284 // 1) cheap test, using string comparison of file names
285 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
286 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
287 if (it != bstore.end())
289 // 2) possibly expensive test, using equivalence test of file names
290 it = find_if(bstore.begin(), bstore.end(),
291 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
292 if (it != bstore.end())
296 // 1) cheap test, using string comparison of file names
297 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
298 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
299 if (it != binternal.end())
301 // 2) possibly expensive test, using equivalence test of file names
302 it = find_if(binternal.begin(), binternal.end(),
303 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
304 if (it != binternal.end())
312 Buffer * BufferList::getBufferFromTmp(string const & s)
314 BufferStorage::iterator it = bstore.begin();
315 BufferStorage::iterator end = bstore.end();
316 for (; it < end; ++it) {
317 if (prefixIs(s, (*it)->temppath())) {
318 // check whether the filename matches the master
319 string const master_name = (*it)->latexName();
320 if (suffixIs(s, master_name))
322 // if not, try with the children
323 ListOfBuffers clist = (*it)->getDescendents();
324 ListOfBuffers::const_iterator cit = clist.begin();
325 ListOfBuffers::const_iterator cend = clist.end();
326 for (; cit != cend; ++cit) {
327 string const mangled_child_name = DocFileName(
328 changeExtension((*cit)->absFileName(),
329 ".tex")).mangledFileName();
330 if (suffixIs(s, mangled_child_name))
339 void BufferList::recordCurrentAuthor(Author const & author)
341 BufferStorage::iterator it = bstore.begin();
342 BufferStorage::iterator end = bstore.end();
343 for (; it != end; ++it)
344 (*it)->params().authors().recordCurrentAuthor(author);
348 int BufferList::bufferNum(FileName const & fname) const
350 FileNameList const & buffers = fileNames();
351 FileNameList::const_iterator cit =
352 find(buffers.begin(), buffers.end(), fname);
353 if (cit == buffers.end())
355 return int(cit - buffers.begin());
359 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
361 LASSERT(parent, return false);
362 LASSERT(child, return false);
363 LASSERT(parent->isChild(child), return false);
365 // Child document has a different parent, don't close it.
366 Buffer const * parent_ = child->parent();
367 if (parent_ && parent_ != parent)
370 BufferStorage::iterator it = bstore.begin();
371 BufferStorage::iterator end = bstore.end();
372 for (; it != end; ++it) {
374 if (buf != parent && buf->isChild(child)) {
384 void BufferList::changed(bool update_metrics) const
386 BufferStorage::const_iterator it = bstore.begin();
387 BufferStorage::const_iterator end = bstore.end();
388 for (; it != end; ++it)
389 (*it)->changed(update_metrics);
390 it = binternal.begin();
391 end = binternal.end();
392 for (; it != end; ++it)
393 (*it)->changed(update_metrics);