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 <boost/bind.hpp>
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)
96 BufferStorage::iterator const it =
97 find(bstore.begin(), bstore.end(), buf);
98 if (it != bstore.end()) {
107 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
109 auto_ptr<Buffer> tmpbuf;
111 tmpbuf.reset(new Buffer(s, ronly));
112 } catch (ExceptionMessage const & message) {
113 if (message.type_ == ErrorException) {
114 Alert::error(message.title_, message.details_);
116 } else if (message.type_ == WarningException) {
117 Alert::warning(message.title_, message.details_);
121 tmpbuf->params().useClassDefaults();
122 if (tmpbuf->isInternal()) {
123 binternal.push_back(tmpbuf.get());
125 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
126 bstore.push_back(tmpbuf.get());
128 return tmpbuf.release();
132 void BufferList::closeAll()
134 while (!bstore.empty())
135 release(bstore.front());
139 FileNameList const & BufferList::fileNames() const
141 static FileNameList nvec;
143 transform(bstore.begin(), bstore.end(),
145 boost::bind(&Buffer::fileName, _1));
150 Buffer * BufferList::first()
154 return bstore.front();
158 Buffer * BufferList::last()
162 return bstore.back();
166 Buffer * BufferList::getBuffer(unsigned int choice)
168 if (choice >= bstore.size())
170 return bstore[choice];
174 Buffer * BufferList::next(Buffer const * buf) const
180 BufferStorage::const_iterator it = find(bstore.begin(),
182 LASSERT(it != bstore.end(), /**/);
184 Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
189 Buffer * BufferList::previous(Buffer const * buf) const
195 BufferStorage::const_iterator it = find(bstore.begin(),
197 LASSERT(it != bstore.end(), /**/);
199 Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
204 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
205 OutputParams const & runparams)
207 BufferStorage::iterator it = bstore.begin();
208 BufferStorage::iterator end = bstore.end();
209 for (; it != end; ++it) {
210 if (!(*it)->isDepClean(masterTmpDir)) {
211 string writefile = addName(masterTmpDir, (*it)->latexName());
212 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
214 (*it)->markDepClean(masterTmpDir);
220 void BufferList::emergencyWriteAll()
222 BufferStorage::const_iterator it = bstore.begin();
223 BufferStorage::const_iterator const en = bstore.end();
224 for (; it != en; ++it)
225 (*it)->emergencyWrite();
229 bool BufferList::exists(FileName const & fname) const
231 return getBuffer(fname) != 0;
235 bool BufferList::isLoaded(Buffer const * b) const
237 BufferStorage::const_iterator cit =
238 find(bstore.begin(), bstore.end(), b);
239 return cit != bstore.end();
244 struct equivalent_to : public binary_function<FileName, FileName, bool>
246 bool operator()(FileName const & x, FileName const & y) const
247 { return equivalent(x, y); }
252 Buffer * BufferList::getBuffer(support::FileName const & fname) const
254 // 1) cheap test, using string comparison of file names
255 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
256 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
257 if (it != bstore.end())
259 // 2) possibly expensive test, using equivalence test of file names
260 it = find_if(bstore.begin(), bstore.end(),
261 bind(equivalent_to(), bind(&Buffer::fileName, _1), fname));
262 return it != bstore.end() ? (*it) : 0;
266 Buffer * BufferList::getBufferFromTmp(string const & s)
268 BufferStorage::iterator it = bstore.begin();
269 BufferStorage::iterator end = bstore.end();
270 for (; it < end; ++it) {
271 if (prefixIs(s, (*it)->temppath())) {
272 // check whether the filename matches the master
273 string const master_name = changeExtension(onlyFilename(
274 (*it)->absFileName()), ".tex");
275 if (suffixIs(s, master_name))
277 // if not, try with the children
278 vector<Buffer *> clist = (*it)->getChildren();
279 vector<Buffer *>::const_iterator cit = clist.begin();
280 vector<Buffer *>::const_iterator cend = clist.end();
281 for (; cit < cend; ++cit) {
282 string const mangled_child_name = DocFileName(
283 changeExtension((*cit)->absFileName(),
284 ".tex")).mangledFilename();
285 if (suffixIs(s, mangled_child_name))
294 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
296 BufferStorage::iterator it = bstore.begin();
297 BufferStorage::iterator end = bstore.end();
298 for (; it != end; ++it)
299 (*it)->params().authors().record(0, Author(name, email));
303 int BufferList::bufferNum(FileName const & fname) const
305 FileNameList const & buffers = fileNames();
306 FileNameList::const_iterator cit =
307 find(buffers.begin(), buffers.end(), fname);
308 if (cit == buffers.end())
310 return int(cit - buffers.begin());
314 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
316 LASSERT(parent, return false);
317 LASSERT(child, return false);
318 LASSERT(parent->isChild(child), return false);
320 // Child document has a different parent, don't close it.
321 Buffer const * parent_ = child->parent();
322 if (parent_ && parent_ != parent)
325 BufferStorage::iterator it = bstore.begin();
326 BufferStorage::iterator end = bstore.end();
327 for (; it != end; ++it) {
329 if (buf != parent && buf->isChild(child)) {
339 void BufferList::changed(bool update_metrics) const
341 BufferStorage::const_iterator it = bstore.begin();
342 BufferStorage::const_iterator end = bstore.end();
343 for (; it != end; ++it)
344 (*it)->changed(update_metrics);
345 it = binternal.begin();
346 end = binternal.end();
347 for (; it != end; ++it)
348 (*it)->changed(update_metrics);