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>
46 using std::back_inserter;
52 using support::addName;
53 using support::bformat;
54 using support::FileName;
55 using support::makeDisplayPath;
56 using support::onlyFilename;
57 using support::removeAutosaveFile;
58 using support::package;
59 using support::prefixIs;
61 namespace Alert = lyx::frontend::Alert;
64 BufferList::BufferList()
68 bool BufferList::empty() const
70 return bstore.empty();
74 BufferList::iterator BufferList::begin()
76 return bstore.begin();
80 BufferList::const_iterator BufferList::begin() const
82 return bstore.begin();
86 BufferList::iterator BufferList::end()
92 BufferList::const_iterator BufferList::end() const
98 void BufferList::release(Buffer * buf)
101 BufferStorage::iterator const it =
102 find(bstore.begin(), bstore.end(), buf);
103 if (it != bstore.end()) {
104 Buffer * tmp = (*it);
112 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
114 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
115 tmpbuf->params().useClassDefaults();
116 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
117 bstore.push_back(tmpbuf.get());
118 return tmpbuf.release();
122 void BufferList::closeAll()
124 while (!bstore.empty())
125 release(bstore.front());
129 vector<string> const BufferList::getFileNames() const
132 transform(bstore.begin(), bstore.end(),
134 boost::bind(&Buffer::absFileName, _1));
139 Buffer * BufferList::first()
143 return bstore.front();
147 Buffer * BufferList::last()
151 return bstore.back();
155 Buffer * BufferList::getBuffer(unsigned int choice)
157 if (choice >= bstore.size())
159 return bstore[choice];
163 Buffer * BufferList::next(Buffer const * buf) const
169 BufferStorage::const_iterator it = find(bstore.begin(),
171 BOOST_ASSERT(it != bstore.end());
173 if (it == bstore.end())
174 return bstore.front();
180 Buffer * BufferList::previous(Buffer const * buf) const
186 BufferStorage::const_iterator it = find(bstore.begin(),
188 BOOST_ASSERT(it != bstore.end());
189 if (it == bstore.begin())
190 return bstore.back();
196 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
197 OutputParams const & runparams)
199 BufferStorage::iterator it = bstore.begin();
200 BufferStorage::iterator end = bstore.end();
201 for (; it != end; ++it) {
202 if (!(*it)->isDepClean(masterTmpDir)) {
203 string writefile = addName(masterTmpDir, (*it)->latexName());
204 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
206 (*it)->markDepClean(masterTmpDir);
212 void BufferList::emergencyWriteAll()
214 for_each(bstore.begin(), bstore.end(),
215 bind(&BufferList::emergencyWrite, this, _1));
219 void BufferList::emergencyWrite(Buffer * buf)
221 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
222 // compare with 0 to avoid pointer/interger comparison
223 // ::assert(buf != 0);
227 // No need to save if the buffer has not changed.
231 string const doc = buf->isUnnamed()
232 ? onlyFilename(buf->absFileName()) : buf->absFileName();
235 bformat(_("LyX: Attempting to save document %1$s"), from_utf8(doc)))
238 // We try to save three places:
239 // 1) Same place as document. Unless it is an unnamed doc.
240 if (!buf->isUnnamed()) {
241 string s = buf->absFileName();
243 lyxerr << " " << s << endl;
244 if (buf->writeFile(FileName(s))) {
246 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
249 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
253 // 2) In HOME directory.
254 string s = addName(package().home_dir().absFilename(), buf->absFileName());
256 lyxerr << ' ' << s << endl;
257 if (buf->writeFile(FileName(s))) {
259 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
263 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
265 // 3) In "/tmp" directory.
266 // MakeAbsPath to prepend the current
267 // drive letter on OS/2
268 s = addName(package().temp_dir().absFilename(), buf->absFileName());
270 lyxerr << ' ' << s << endl;
271 if (buf->writeFile(FileName(s))) {
273 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
276 lyxerr << to_utf8(_(" Save failed! Bummer. Document is lost.")) << endl;
280 bool BufferList::exists(string const & s) const
282 return find_if(bstore.begin(), bstore.end(),
283 bind(equal_to<string>(),
284 bind(&Buffer::absFileName, _1),
290 bool BufferList::isLoaded(Buffer const * b) const
293 BufferStorage::const_iterator cit =
294 find(bstore.begin(), bstore.end(), b);
295 return cit != bstore.end();
299 Buffer * BufferList::getBuffer(string const & s)
301 BufferStorage::iterator it =
302 find_if(bstore.begin(), bstore.end(),
303 bind(equal_to<string>(),
304 bind(&Buffer::absFileName, _1),
307 return it != bstore.end() ? (*it) : 0;
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()))
322 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
324 BufferStorage::iterator it = bstore.begin();
325 BufferStorage::iterator end = bstore.end();
326 for (; it != end; ++it)
327 (*it)->params().authors().record(0, Author(name, email));
331 int BufferList::bufferNum(std::string const & name) const
333 vector<string> buffers = getFileNames();
334 vector<string>::const_iterator cit =
335 std::find(buffers.begin(), buffers.end(), name);
336 if (cit == buffers.end())
338 return int(cit - buffers.begin());