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>
42 using support::addName;
43 using support::bformat;
44 using support::FileName;
45 using support::makeDisplayPath;
46 using support::onlyFilename;
47 using support::removeAutosaveFile;
48 using support::package;
49 using support::prefixIs;
51 namespace Alert = lyx::frontend::Alert;
54 BufferList::BufferList()
58 bool BufferList::empty() const
60 return bstore.empty();
64 BufferList::iterator BufferList::begin()
66 return bstore.begin();
70 BufferList::const_iterator BufferList::begin() const
72 return bstore.begin();
76 BufferList::iterator BufferList::end()
82 BufferList::const_iterator BufferList::end() const
88 void BufferList::release(Buffer * buf)
91 BufferStorage::iterator const it =
92 find(bstore.begin(), bstore.end(), buf);
93 if (it != bstore.end()) {
102 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
104 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
105 tmpbuf->params().useClassDefaults();
106 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
107 bstore.push_back(tmpbuf.get());
108 return tmpbuf.release();
112 void BufferList::closeAll()
114 while (!bstore.empty())
115 release(bstore.front());
119 vector<string> const BufferList::getFileNames() const
122 transform(bstore.begin(), bstore.end(),
124 boost::bind(&Buffer::absFileName, _1));
129 Buffer * BufferList::first()
133 return bstore.front();
137 Buffer * BufferList::last()
141 return bstore.back();
145 Buffer * BufferList::getBuffer(unsigned int choice)
147 if (choice >= bstore.size())
149 return bstore[choice];
153 Buffer * BufferList::next(Buffer const * buf) const
159 BufferStorage::const_iterator it = find(bstore.begin(),
161 BOOST_ASSERT(it != bstore.end());
163 if (it == bstore.end())
164 return bstore.front();
170 Buffer * BufferList::previous(Buffer const * buf) const
176 BufferStorage::const_iterator it = find(bstore.begin(),
178 BOOST_ASSERT(it != bstore.end());
179 if (it == bstore.begin())
180 return bstore.back();
186 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
187 OutputParams const & runparams)
189 BufferStorage::iterator it = bstore.begin();
190 BufferStorage::iterator end = bstore.end();
191 for (; it != end; ++it) {
192 if (!(*it)->isDepClean(masterTmpDir)) {
193 string writefile = addName(masterTmpDir, (*it)->latexName());
194 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
196 (*it)->markDepClean(masterTmpDir);
202 void BufferList::emergencyWriteAll()
204 for_each(bstore.begin(), bstore.end(),
205 bind(&BufferList::emergencyWrite, this, _1));
209 void BufferList::emergencyWrite(Buffer * buf)
211 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
212 // compare with 0 to avoid pointer/interger comparison
213 // ::assert(buf != 0);
217 // No need to save if the buffer has not changed.
221 string const doc = buf->isUnnamed()
222 ? onlyFilename(buf->absFileName()) : buf->absFileName();
225 bformat(_("LyX: Attempting to save document %1$s"), from_utf8(doc)))
228 // We try to save three places:
229 // 1) Same place as document. Unless it is an unnamed doc.
230 if (!buf->isUnnamed()) {
231 string s = buf->absFileName();
233 lyxerr << " " << s << endl;
234 if (buf->writeFile(FileName(s))) {
236 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
239 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
243 // 2) In HOME directory.
244 string s = addName(package().home_dir().absFilename(), buf->absFileName());
246 lyxerr << ' ' << s << endl;
247 if (buf->writeFile(FileName(s))) {
249 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
253 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
255 // 3) In "/tmp" directory.
256 // MakeAbsPath to prepend the current
257 // drive letter on OS/2
258 s = addName(package().temp_dir().absFilename(), buf->absFileName());
260 lyxerr << ' ' << s << endl;
261 if (buf->writeFile(FileName(s))) {
263 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
266 lyxerr << to_utf8(_(" Save failed! Bummer. Document is lost.")) << endl;
270 bool BufferList::exists(string const & s) const
272 return find_if(bstore.begin(), bstore.end(),
273 bind(equal_to<string>(),
274 bind(&Buffer::absFileName, _1),
280 bool BufferList::isLoaded(Buffer const * b) const
283 BufferStorage::const_iterator cit =
284 find(bstore.begin(), bstore.end(), b);
285 return cit != bstore.end();
289 Buffer * BufferList::getBuffer(string const & s)
291 BufferStorage::iterator it =
292 find_if(bstore.begin(), bstore.end(),
293 bind(equal_to<string>(),
294 bind(&Buffer::absFileName, _1),
297 return it != bstore.end() ? (*it) : 0;
301 Buffer * BufferList::getBufferFromTmp(string const & s)
303 BufferStorage::iterator it = bstore.begin();
304 BufferStorage::iterator end = bstore.end();
305 for (; it < end; ++it)
306 if (prefixIs(s, (*it)->temppath()))
312 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
314 BufferStorage::iterator it = bstore.begin();
315 BufferStorage::iterator end = bstore.end();
316 for (; it != end; ++it)
317 (*it)->params().authors().record(0, Author(name, email));
321 int BufferList::bufferNum(std::string const & name) const
323 vector<string> buffers = getFileNames();
324 vector<string>::const_iterator cit =
325 std::find(buffers.begin(), buffers.end(), name);
326 if (cit == buffers.end())
328 return int(cit - buffers.begin());