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 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 " << bstore.size());
189 bstore.push_back(tmpbuf.get());
190 return tmpbuf.release();
194 void BufferList::closeAll()
196 while (!bstore.empty()) {
197 close(bstore.front(), false);
202 bool BufferList::close(Buffer * buf, bool const ask)
206 if (!ask || buf->isClean() || buf->paragraphs().empty()) {
212 if (buf->isUnnamed())
213 fname = from_utf8(onlyFilename(buf->absFileName()));
215 fname = makeDisplayPath(buf->absFileName(), 30);
217 docstring const text =
218 bformat(_("The document %1$s has unsaved changes.\n\n"
219 "Do you want to save the document or discard the changes?"),
221 int const ret = Alert::prompt(_("Save changed document?"),
222 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
225 if (buf->isUnnamed()) {
228 } else if (!buf->menuWrite())
233 removeAutosaveFile(buf->absFileName());
240 vector<string> const BufferList::getFileNames() const
243 transform(bstore.begin(), bstore.end(),
245 boost::bind(&Buffer::absFileName, _1));
250 Buffer * BufferList::first()
254 return bstore.front();
258 Buffer * BufferList::last()
262 return bstore.back();
266 Buffer * BufferList::getBuffer(unsigned int choice)
268 if (choice >= bstore.size())
270 return bstore[choice];
274 Buffer * BufferList::next(Buffer const * buf) const
280 BufferStorage::const_iterator it = find(bstore.begin(),
282 BOOST_ASSERT(it != bstore.end());
284 if (it == bstore.end())
285 return bstore.front();
291 Buffer * BufferList::previous(Buffer const * buf) const
297 BufferStorage::const_iterator it = find(bstore.begin(),
299 BOOST_ASSERT(it != bstore.end());
300 if (it == bstore.begin())
301 return bstore.back();
307 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
308 OutputParams const & runparams)
310 BufferStorage::iterator it = bstore.begin();
311 BufferStorage::iterator end = bstore.end();
312 for (; it != end; ++it) {
313 if (!(*it)->isDepClean(masterTmpDir)) {
314 string writefile = addName(masterTmpDir, (*it)->latexName());
315 (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
317 (*it)->markDepClean(masterTmpDir);
323 void BufferList::emergencyWriteAll()
325 for_each(bstore.begin(), bstore.end(),
326 bind(&BufferList::emergencyWrite, this, _1));
330 void BufferList::emergencyWrite(Buffer * buf)
332 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
333 // compare with 0 to avoid pointer/interger comparison
334 // ::assert(buf != 0);
338 // No need to save if the buffer has not changed.
342 string const doc = buf->isUnnamed()
343 ? onlyFilename(buf->absFileName()) : buf->absFileName();
346 bformat(_("LyX: Attempting to save document %1$s"), from_utf8(doc)))
349 // We try to save three places:
350 // 1) Same place as document. Unless it is an unnamed doc.
351 if (!buf->isUnnamed()) {
352 string s = buf->absFileName();
354 lyxerr << " " << s << endl;
355 if (buf->writeFile(FileName(s))) {
357 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
360 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
364 // 2) In HOME directory.
365 string s = addName(package().home_dir().absFilename(), buf->absFileName());
367 lyxerr << ' ' << s << endl;
368 if (buf->writeFile(FileName(s))) {
370 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
374 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
376 // 3) In "/tmp" directory.
377 // MakeAbsPath to prepend the current
378 // drive letter on OS/2
379 s = addName(package().temp_dir().absFilename(), buf->absFileName());
381 lyxerr << ' ' << s << endl;
382 if (buf->writeFile(FileName(s))) {
384 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
387 lyxerr << to_utf8(_(" Save failed! Bummer. Document is lost.")) << endl;
391 bool BufferList::exists(string const & s) const
393 return find_if(bstore.begin(), bstore.end(),
394 bind(equal_to<string>(),
395 bind(&Buffer::absFileName, _1),
401 bool BufferList::isLoaded(Buffer const * b) const
404 BufferStorage::const_iterator cit =
405 find(bstore.begin(), bstore.end(), b);
406 return cit != bstore.end();
410 Buffer * BufferList::getBuffer(string const & s)
412 BufferStorage::iterator it =
413 find_if(bstore.begin(), bstore.end(),
414 bind(equal_to<string>(),
415 bind(&Buffer::absFileName, _1),
418 return it != bstore.end() ? (*it) : 0;
422 Buffer * BufferList::getBufferFromTmp(string const & s)
424 BufferStorage::iterator it = bstore.begin();
425 BufferStorage::iterator end = bstore.end();
426 for (; it < end; ++it)
427 if (prefixIs(s, (*it)->temppath()))
433 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
435 BufferStorage::iterator it = bstore.begin();
436 BufferStorage::iterator end = bstore.end();
437 for (; it != end; ++it)
438 (*it)->params().authors().record(0, Author(name, email));
442 int BufferList::bufferNum(std::string const & name) const
444 vector<string> buffers = getFileNames();
445 vector<string>::const_iterator cit =
446 std::find(buffers.begin(), buffers.end(), name);
447 if (cit == buffers.end())
449 return int(cit - buffers.begin());