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)
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 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
239 BufferStorage::const_iterator cit =
240 find(bstore.begin(), bstore.end(), b);
241 return cit != bstore.end();
246 struct equivalent_to : public binary_function<FileName, FileName, bool>
248 bool operator()(FileName const & x, FileName const & y) const
249 { return equivalent(x, y); }
254 Buffer * BufferList::getBuffer(support::FileName const & fname) const
256 // 1) cheap test, using string comparison of file names
257 BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
258 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
259 if (it != bstore.end())
261 // 2) possibly expensive test, using equivalence test of file names
262 it = find_if(bstore.begin(), bstore.end(),
263 bind(equivalent_to(), bind(&Buffer::fileName, _1), fname));
264 return it != bstore.end() ? (*it) : 0;
268 Buffer * BufferList::getBufferFromTmp(string const & s)
270 BufferStorage::iterator it = bstore.begin();
271 BufferStorage::iterator end = bstore.end();
272 for (; it < end; ++it) {
273 if (prefixIs(s, (*it)->temppath())) {
274 // check whether the filename matches the master
275 string const master_name = changeExtension(onlyFileName(
276 (*it)->absFileName()), ".tex");
277 if (suffixIs(s, master_name))
279 // if not, try with the children
280 vector<Buffer *> clist = (*it)->getChildren();
281 vector<Buffer *>::const_iterator cit = clist.begin();
282 vector<Buffer *>::const_iterator cend = clist.end();
283 for (; cit < cend; ++cit) {
284 string const mangled_child_name = DocFileName(
285 changeExtension((*cit)->absFileName(),
286 ".tex")).mangledFileName();
287 if (suffixIs(s, mangled_child_name))
296 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
298 BufferStorage::iterator it = bstore.begin();
299 BufferStorage::iterator end = bstore.end();
300 for (; it != end; ++it)
301 (*it)->params().authors().record(0, Author(name, email));
305 int BufferList::bufferNum(FileName const & fname) const
307 FileNameList const & buffers = fileNames();
308 FileNameList::const_iterator cit =
309 find(buffers.begin(), buffers.end(), fname);
310 if (cit == buffers.end())
312 return int(cit - buffers.begin());
316 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
318 LASSERT(parent, return false);
319 LASSERT(child, return false);
320 LASSERT(parent->isChild(child), return false);
322 // Child document has a different parent, don't close it.
323 Buffer const * parent_ = child->parent();
324 if (parent_ && parent_ != parent)
327 BufferStorage::iterator it = bstore.begin();
328 BufferStorage::iterator end = bstore.end();
329 for (; it != end; ++it) {
331 if (buf != parent && buf->isChild(child)) {
341 void BufferList::changed(bool update_metrics) const
343 BufferStorage::const_iterator it = bstore.begin();
344 BufferStorage::const_iterator end = bstore.end();
345 for (; it != end; ++it)
346 (*it)->changed(update_metrics);
347 it = binternal.begin();
348 end = binternal.end();
349 for (; it != end; ++it)
350 (*it)->changed(update_metrics);