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/debug.h"
26 #include "support/filetools.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29 #include "support/Package.h"
31 #include <boost/bind.hpp>
39 using namespace lyx::support;
43 namespace Alert = lyx::frontend::Alert;
46 BufferList::BufferList()
50 bool BufferList::empty() const
52 return bstore.empty();
56 BufferList::iterator BufferList::begin()
58 return bstore.begin();
62 BufferList::const_iterator BufferList::begin() const
64 return bstore.begin();
68 BufferList::iterator BufferList::end()
74 BufferList::const_iterator BufferList::end() const
80 void BufferList::release(Buffer * buf)
83 BufferStorage::iterator const it =
84 find(bstore.begin(), bstore.end(), buf);
85 if (it != bstore.end()) {
94 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
96 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
97 tmpbuf->params().useClassDefaults();
98 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
99 bstore.push_back(tmpbuf.get());
100 return tmpbuf.release();
104 void BufferList::closeAll()
106 while (!bstore.empty())
107 release(bstore.front());
111 vector<string> const BufferList::getFileNames() const
114 transform(bstore.begin(), bstore.end(),
116 boost::bind(&Buffer::absFileName, _1));
121 Buffer * BufferList::first()
125 return bstore.front();
129 Buffer * BufferList::last()
133 return bstore.back();
137 Buffer * BufferList::getBuffer(unsigned int choice)
139 if (choice >= bstore.size())
141 return bstore[choice];
145 Buffer * BufferList::next(Buffer const * buf) const
151 BufferStorage::const_iterator it = find(bstore.begin(),
153 BOOST_ASSERT(it != bstore.end());
155 if (it == bstore.end())
156 return bstore.front();
162 Buffer * BufferList::previous(Buffer const * buf) const
168 BufferStorage::const_iterator it = find(bstore.begin(),
170 BOOST_ASSERT(it != bstore.end());
171 if (it == bstore.begin())
172 return bstore.back();
178 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
179 OutputParams const & runparams)
181 BufferStorage::iterator it = bstore.begin();
182 BufferStorage::iterator end = bstore.end();
183 for (; it != end; ++it) {
184 if (!(*it)->isDepClean(masterTmpDir)) {
185 string writefile = addName(masterTmpDir, (*it)->latexName());
186 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
188 (*it)->markDepClean(masterTmpDir);
194 void BufferList::emergencyWriteAll()
196 for_each(bstore.begin(), bstore.end(),
197 bind(&BufferList::emergencyWrite, this, _1));
201 void BufferList::emergencyWrite(Buffer * buf)
203 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
204 // compare with 0 to avoid pointer/interger comparison
205 // ::assert(buf != 0);
209 // No need to save if the buffer has not changed.
213 string const doc = buf->isUnnamed()
214 ? onlyFilename(buf->absFileName()) : buf->absFileName();
217 bformat(_("LyX: Attempting to save document %1$s"), from_utf8(doc)))
220 // We try to save three places:
221 // 1) Same place as document. Unless it is an unnamed doc.
222 if (!buf->isUnnamed()) {
223 string s = buf->absFileName();
225 lyxerr << " " << s << endl;
226 if (buf->writeFile(FileName(s))) {
228 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
231 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
235 // 2) In HOME directory.
236 string s = addName(package().home_dir().absFilename(), buf->absFileName());
238 lyxerr << ' ' << s << endl;
239 if (buf->writeFile(FileName(s))) {
241 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
245 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
247 // 3) In "/tmp" directory.
248 // MakeAbsPath to prepend the current
249 // drive letter on OS/2
250 s = addName(package().temp_dir().absFilename(), buf->absFileName());
252 lyxerr << ' ' << s << endl;
253 if (buf->writeFile(FileName(s))) {
255 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
258 lyxerr << to_utf8(_(" Save failed! Bummer. Document is lost.")) << endl;
262 bool BufferList::exists(string const & s) const
264 return find_if(bstore.begin(), bstore.end(),
265 bind(equal_to<string>(),
266 bind(&Buffer::absFileName, _1),
272 bool BufferList::isLoaded(Buffer const * b) const
275 BufferStorage::const_iterator cit =
276 find(bstore.begin(), bstore.end(), b);
277 return cit != bstore.end();
281 Buffer * BufferList::getBuffer(string const & s)
283 BufferStorage::iterator it =
284 find_if(bstore.begin(), bstore.end(),
285 bind(equal_to<string>(),
286 bind(&Buffer::absFileName, _1),
289 return it != bstore.end() ? (*it) : 0;
293 Buffer * BufferList::getBufferFromTmp(string const & s)
295 BufferStorage::iterator it = bstore.begin();
296 BufferStorage::iterator end = bstore.end();
297 for (; it < end; ++it)
298 if (prefixIs(s, (*it)->temppath()))
304 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
306 BufferStorage::iterator it = bstore.begin();
307 BufferStorage::iterator end = bstore.end();
308 for (; it != end; ++it)
309 (*it)->params().authors().record(0, Author(name, email));
313 int BufferList::bufferNum(string const & name) const
315 vector<string> buffers = getFileNames();
316 vector<string>::const_iterator cit =
317 find(buffers.begin(), buffers.end(), name);
318 if (cit == buffers.end())
320 return int(cit - buffers.begin());