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"
23 #include "output_latex.h"
24 #include "paragraph.h"
25 #include "ParagraphList.h"
27 #include "frontends/Alert.h"
29 #include "support/filetools.h"
30 #include "support/package.h"
32 #include <boost/bind.hpp>
38 using lyx::support::addName;
39 using lyx::support::bformat;
40 using lyx::support::makeAbsPath;
41 using lyx::support::makeDisplayPath;
42 using lyx::support::onlyFilename;
43 using lyx::support::removeAutosaveFile;
44 using lyx::support::package;
45 using lyx::support::prefixIs;
57 using std::back_inserter;
60 namespace Alert = lyx::frontend::Alert;
63 BufferList::BufferList()
67 bool BufferList::empty() const
69 return bstore.empty();
73 bool BufferList::quitWriteBuffer(Buffer * buf)
79 file = lyx::from_utf8(onlyFilename(buf->fileName()));
81 file = makeDisplayPath(buf->fileName(), 30);
83 docstring const text =
84 bformat(_("The document %1$s has unsaved changes.\n\n"
85 "Do you want to save the document or discard the changes?"),
87 int const ret = Alert::prompt(_("Save changed document?"),
88 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
91 // FIXME: WriteAs can be asynch !
92 // but not right now...maybe we should remove that
97 succeeded = writeAs(buf);
99 succeeded = menuWrite(buf);
103 } else if (ret == 1) {
104 // if we crash after this we could
105 // have no autosave file but I guess
106 // this is really inprobable (Jug)
107 if (buf->isUnnamed())
108 removeAutosaveFile(buf->fileName());
118 bool BufferList::quitWriteAll()
120 BufferStorage::iterator it = bstore.begin();
121 BufferStorage::iterator end = bstore.end();
122 for (; it != end; ++it) {
123 if ((*it)->isClean())
126 if (!quitWriteBuffer(*it))
129 // now, all buffers have been written sucessfully
130 // save file names to .lyx/session
132 for (; it != end; ++it) {
133 // if master/slave are both open, do not save slave since it
134 // will be automatically loaded when the master is loaded
135 if ((*it)->getMasterBuffer() == (*it))
136 LyX::ref().session().addLastOpenedFile((*it)->fileName());
143 void BufferList::release(Buffer * buf)
146 BufferStorage::iterator const it =
147 find(bstore.begin(), bstore.end(), buf);
148 if (it != bstore.end()) {
149 Buffer * tmp = (*it);
157 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
159 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
160 tmpbuf->params().useClassDefaults();
161 lyxerr[Debug::INFO] << "Assigning to buffer "
162 << bstore.size() << endl;
163 bstore.push_back(tmpbuf.get());
164 return tmpbuf.release();
168 void BufferList::closeAll()
170 while (!bstore.empty()) {
171 close(bstore.front(), false);
176 bool BufferList::close(Buffer * buf, bool const ask)
180 if (!ask || buf->isClean() || buf->paragraphs().empty()) {
186 if (buf->isUnnamed())
187 fname = lyx::from_utf8(onlyFilename(buf->fileName()));
189 fname = makeDisplayPath(buf->fileName(), 30);
191 docstring const text =
192 bformat(_("The document %1$s has unsaved changes.\n\n"
193 "Do you want to save the document or discard the changes?"),
195 int const ret = Alert::prompt(_("Save changed document?"),
196 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
199 if (buf->isUnnamed()) {
202 } else if (!menuWrite(buf))
209 if (buf->isUnnamed()) {
210 removeAutosaveFile(buf->fileName());
218 vector<string> const BufferList::getFileNames() const
221 transform(bstore.begin(), bstore.end(),
223 boost::bind(&Buffer::fileName, _1));
228 Buffer * BufferList::first()
232 return bstore.front();
236 Buffer * BufferList::getBuffer(unsigned int const choice)
238 if (choice >= bstore.size())
240 return bstore[choice];
244 Buffer * BufferList::next(Buffer const * buf) const
250 BufferStorage::const_iterator it = find(bstore.begin(),
252 BOOST_ASSERT(it != bstore.end());
254 if (it == bstore.end())
255 return bstore.front();
261 Buffer * BufferList::previous(Buffer const * buf) const
267 BufferStorage::const_iterator it = find(bstore.begin(),
269 BOOST_ASSERT(it != bstore.end());
270 if (it == bstore.begin())
271 return bstore.back();
277 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
278 OutputParams const & runparams)
280 BufferStorage::iterator it = bstore.begin();
281 BufferStorage::iterator end = bstore.end();
282 for (; it != end; ++it) {
283 if (!(*it)->isDepClean(mastertmpdir)) {
284 string writefile = mastertmpdir;
286 writefile += (*it)->getLatexName();
287 (*it)->makeLaTeXFile(writefile, mastertmpdir,
289 (*it)->markDepClean(mastertmpdir);
295 void BufferList::emergencyWriteAll()
297 for_each(bstore.begin(), bstore.end(),
298 bind(&BufferList::emergencyWrite, this, _1));
302 void BufferList::emergencyWrite(Buffer * buf)
304 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
305 // compare with 0 to avoid pointer/interger comparison
308 // No need to save if the buffer has not changed.
312 string const doc = buf->isUnnamed()
313 ? onlyFilename(buf->fileName()) : buf->fileName();
315 lyxerr << lyx::to_utf8(
316 bformat(_("LyX: Attempting to save document %1$s"), lyx::from_utf8(doc)))
319 // We try to save three places:
320 // 1) Same place as document. Unless it is an unnamed doc.
321 if (!buf->isUnnamed()) {
322 string s = buf->fileName();
324 lyxerr << " " << s << endl;
325 if (buf->writeFile(s)) {
327 lyxerr << lyx::to_utf8(_(" Save seems successful. Phew.")) << endl;
330 lyxerr << lyx::to_utf8(_(" Save failed! Trying...")) << endl;
334 // 2) In HOME directory.
335 string s = addName(package().home_dir(), buf->fileName());
337 lyxerr << ' ' << s << endl;
338 if (buf->writeFile(s)) {
340 lyxerr << lyx::to_utf8(_(" Save seems successful. Phew.")) << endl;
344 lyxerr << lyx::to_utf8(_(" Save failed! Trying...")) << endl;
346 // 3) In "/tmp" directory.
347 // MakeAbsPath to prepend the current
348 // drive letter on OS/2
349 s = addName(package().temp_dir(), buf->fileName());
351 lyxerr << ' ' << s << endl;
352 if (buf->writeFile(s)) {
354 lyxerr << lyx::to_utf8(_(" Save seems successful. Phew.")) << endl;
357 lyxerr << lyx::to_utf8(_(" Save failed! Bummer. Document is lost.")) << endl;
361 bool BufferList::exists(string const & s) const
363 return find_if(bstore.begin(), bstore.end(),
364 bind(equal_to<string>(),
365 bind(&Buffer::fileName, _1),
371 bool BufferList::isLoaded(Buffer const * b) const
374 BufferStorage::const_iterator cit =
375 find(bstore.begin(), bstore.end(), b);
376 return cit != bstore.end();
380 Buffer * BufferList::getBuffer(string const & s)
382 BufferStorage::iterator it =
383 find_if(bstore.begin(), bstore.end(),
384 bind(equal_to<string>(),
385 bind(&Buffer::fileName, _1),
388 return it != bstore.end() ? (*it) : 0;
392 Buffer * BufferList::getBufferFromTmp(string const & s)
394 BufferStorage::iterator it = bstore.begin();
395 BufferStorage::iterator end = bstore.end();
396 for (; it < end; ++it)
397 if (prefixIs(s, (*it)->temppath()))
403 void BufferList::setCurrentAuthor(string const & name, string const & email)
405 BufferStorage::iterator it = bstore.begin();
406 BufferStorage::iterator end = bstore.end();
407 for (; it != end; ++it) {
408 (*it)->params().authors().record(0, Author(name, email));