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"
22 #include "output_latex.h"
23 #include "ParagraphList.h"
25 #include "frontends/alert.h"
27 #include "support/filetools.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 bool BufferList::quitWriteBuffer(Buffer * buf)
105 if (buf->isUnnamed())
106 file = from_utf8(buf->fileName().onlyFileName());
108 file = buf->fileName().displayName(30);
110 docstring const text =
111 bformat(_("The document %1$s has unsaved changes.\n\n"
112 "Do you want to save the document or discard the changes?"),
114 int const ret = Alert::prompt(_("Save changed document?"),
115 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
118 // FIXME: WriteAs can be asynch !
119 // but not right now...maybe we should remove that
123 if (buf->isUnnamed())
124 succeeded = buf->writeAs();
126 succeeded = buf->menuWrite();
130 } else if (ret == 1) {
131 // if we crash after this we could
132 // have no autosave file but I guess
133 // this is really inprobable (Jug)
134 if (buf->isUnnamed())
135 removeAutosaveFile(buf->absFileName());
145 bool BufferList::quitWriteAll()
147 BufferStorage::iterator it = bstore.begin();
148 BufferStorage::iterator end = bstore.end();
149 for (; it != end; ++it) {
150 if ((*it)->isClean())
153 if (!quitWriteBuffer(*it))
156 // now, all buffers have been written sucessfully
157 // save file names to .lyx/session
159 for (; it != end; ++it) {
160 // if master/slave are both open, do not save slave since it
161 // will be automatically loaded when the master is loaded
162 if ((*it)->masterBuffer() == (*it))
163 LyX::ref().session().lastOpened().add(FileName((*it)->absFileName()));
170 void BufferList::release(Buffer * buf)
173 BufferStorage::iterator const it =
174 find(bstore.begin(), bstore.end(), buf);
175 if (it != bstore.end()) {
176 Buffer * tmp = (*it);
184 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
186 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
187 tmpbuf->params().useClassDefaults();
188 LYXERR(Debug::INFO) << "Assigning to buffer "
189 << bstore.size() << endl;
190 bstore.push_back(tmpbuf.get());
191 return tmpbuf.release();
195 void BufferList::closeAll()
197 while (!bstore.empty()) {
198 close(bstore.front(), false);
203 bool BufferList::close(Buffer * buf, bool const ask)
207 if (!ask || buf->isClean() || buf->paragraphs().empty()) {
213 if (buf->isUnnamed())
214 fname = from_utf8(onlyFilename(buf->absFileName()));
216 fname = makeDisplayPath(buf->absFileName(), 30);
218 docstring const text =
219 bformat(_("The document %1$s has unsaved changes.\n\n"
220 "Do you want to save the document or discard the changes?"),
222 int const ret = Alert::prompt(_("Save changed document?"),
223 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
226 if (buf->isUnnamed()) {
229 } else if (!buf->menuWrite())
234 removeAutosaveFile(buf->absFileName());
241 vector<string> const BufferList::getFileNames() const
244 transform(bstore.begin(), bstore.end(),
246 boost::bind(&Buffer::absFileName, _1));
251 Buffer * BufferList::first()
255 return bstore.front();
259 Buffer * BufferList::last()
263 return bstore.back();
267 Buffer * BufferList::getBuffer(unsigned int choice)
269 if (choice >= bstore.size())
271 return bstore[choice];
275 Buffer * BufferList::next(Buffer const * buf) const
281 BufferStorage::const_iterator it = find(bstore.begin(),
283 BOOST_ASSERT(it != bstore.end());
285 if (it == bstore.end())
286 return bstore.front();
292 Buffer * BufferList::previous(Buffer const * buf) const
298 BufferStorage::const_iterator it = find(bstore.begin(),
300 BOOST_ASSERT(it != bstore.end());
301 if (it == bstore.begin())
302 return bstore.back();
308 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
309 OutputParams const & runparams)
311 BufferStorage::iterator it = bstore.begin();
312 BufferStorage::iterator end = bstore.end();
313 for (; it != end; ++it) {
314 if (!(*it)->isDepClean(masterTmpDir)) {
315 string writefile = addName(masterTmpDir, (*it)->latexName());
316 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
318 (*it)->markDepClean(masterTmpDir);
324 void BufferList::emergencyWriteAll()
326 for_each(bstore.begin(), bstore.end(),
327 bind(&BufferList::emergencyWrite, this, _1));
331 void BufferList::emergencyWrite(Buffer * buf)
333 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
334 // compare with 0 to avoid pointer/interger comparison
335 // ::assert(buf != 0);
339 // No need to save if the buffer has not changed.
343 string const doc = buf->isUnnamed()
344 ? onlyFilename(buf->absFileName()) : buf->absFileName();
347 bformat(_("LyX: Attempting to save document %1$s"), from_utf8(doc)))
350 // We try to save three places:
351 // 1) Same place as document. Unless it is an unnamed doc.
352 if (!buf->isUnnamed()) {
353 string s = buf->absFileName();
355 lyxerr << " " << s << endl;
356 if (buf->writeFile(FileName(s))) {
358 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
361 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
365 // 2) In HOME directory.
366 string s = addName(package().home_dir().absFilename(), buf->absFileName());
368 lyxerr << ' ' << s << endl;
369 if (buf->writeFile(FileName(s))) {
371 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
375 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
377 // 3) In "/tmp" directory.
378 // MakeAbsPath to prepend the current
379 // drive letter on OS/2
380 s = addName(package().temp_dir().absFilename(), buf->absFileName());
382 lyxerr << ' ' << s << endl;
383 if (buf->writeFile(FileName(s))) {
385 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
388 lyxerr << to_utf8(_(" Save failed! Bummer. Document is lost.")) << endl;
392 bool BufferList::exists(string const & s) const
394 return find_if(bstore.begin(), bstore.end(),
395 bind(equal_to<string>(),
396 bind(&Buffer::absFileName, _1),
402 bool BufferList::isLoaded(Buffer const * b) const
405 BufferStorage::const_iterator cit =
406 find(bstore.begin(), bstore.end(), b);
407 return cit != bstore.end();
411 Buffer * BufferList::getBuffer(string const & s)
413 BufferStorage::iterator it =
414 find_if(bstore.begin(), bstore.end(),
415 bind(equal_to<string>(),
416 bind(&Buffer::absFileName, _1),
419 return it != bstore.end() ? (*it) : 0;
423 Buffer * BufferList::getBufferFromTmp(string const & s)
425 BufferStorage::iterator it = bstore.begin();
426 BufferStorage::iterator end = bstore.end();
427 for (; it < end; ++it)
428 if (prefixIs(s, (*it)->temppath()))
434 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
436 BufferStorage::iterator it = bstore.begin();
437 BufferStorage::iterator end = bstore.end();
438 for (; it != end; ++it)
439 (*it)->params().authors().record(0, Author(name, email));
443 int BufferList::bufferNum(std::string const & name) const
445 vector<string> buffers = getFileNames();
446 vector<string>::const_iterator cit =
447 std::find(buffers.begin(), buffers.end(), name);
448 if (cit == buffers.end())
450 return int(cit - buffers.begin());