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>
37 using lyx::support::addName;
38 using lyx::support::bformat;
39 using lyx::support::makeAbsPath;
40 using lyx::support::makeDisplayPath;
41 using lyx::support::onlyFilename;
42 using lyx::support::removeAutosaveFile;
43 using lyx::support::package;
44 using lyx::support::prefixIs;
56 using std::back_inserter;
60 BufferList::BufferList()
64 bool BufferList::empty() const
66 return bstore.empty();
70 bool BufferList::quitWriteBuffer(Buffer * buf)
76 file = onlyFilename(buf->fileName());
78 file = makeDisplayPath(buf->fileName(), 30);
81 bformat(_("The document %1$s has unsaved changes.\n\n"
82 "Do you want to save the document or discard the changes?"), file);
83 int const ret = Alert::prompt(_("Save changed document?"),
84 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
87 // FIXME: WriteAs can be asynch !
88 // but not right now...maybe we should remove that
93 succeeded = writeAs(buf);
95 succeeded = menuWrite(buf);
99 } else if (ret == 1) {
100 // if we crash after this we could
101 // have no autosave file but I guess
102 // this is really inprobable (Jug)
103 if (buf->isUnnamed())
104 removeAutosaveFile(buf->fileName());
114 bool BufferList::quitWriteAll()
116 BufferStorage::iterator it = bstore.begin();
117 BufferStorage::iterator end = bstore.end();
118 for (; it != end; ++it) {
119 if ((*it)->isClean())
122 if (!quitWriteBuffer(*it))
125 // now, all buffers have been written sucessfully
126 // save file names to .lyx/session
128 for (; it != end; ++it) {
129 // if master/slave are both open, do not save slave since it
130 // will be automatically loaded when the master is loaded
131 if ((*it)->getMasterBuffer() == (*it))
132 LyX::ref().session().addLastOpenedFile((*it)->fileName());
139 void BufferList::release(Buffer * buf)
142 BufferStorage::iterator const it =
143 find(bstore.begin(), bstore.end(), buf);
144 if (it != bstore.end()) {
145 Buffer * tmp = (*it);
153 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
155 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
156 tmpbuf->params().useClassDefaults();
157 lyxerr[Debug::INFO] << "Assigning to buffer "
158 << bstore.size() << endl;
159 bstore.push_back(tmpbuf.get());
160 return tmpbuf.release();
164 void BufferList::closeAll()
166 while (!bstore.empty()) {
167 close(bstore.front(), false);
172 bool BufferList::close(Buffer * buf, bool const ask)
176 if (!ask || buf->isClean() || buf->paragraphs().empty()) {
182 if (buf->isUnnamed())
183 fname = onlyFilename(buf->fileName());
185 fname = makeDisplayPath(buf->fileName(), 30);
188 bformat(_("The document %1$s has unsaved changes.\n\n"
189 "Do you want to save the document or discard the changes?"), fname);
190 int const ret = Alert::prompt(_("Save changed document?"),
191 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
194 if (buf->isUnnamed()) {
197 } else if (!menuWrite(buf))
204 if (buf->isUnnamed()) {
205 removeAutosaveFile(buf->fileName());
213 vector<string> const BufferList::getFileNames() const
216 transform(bstore.begin(), bstore.end(),
218 boost::bind(&Buffer::fileName, _1));
223 Buffer * BufferList::first()
227 return bstore.front();
231 Buffer * BufferList::getBuffer(unsigned int const choice)
233 if (choice >= bstore.size())
235 return bstore[choice];
239 Buffer * BufferList::next(Buffer const * buf) const
245 BufferStorage::const_iterator it = find(bstore.begin(),
247 BOOST_ASSERT(it != bstore.end());
249 if (it == bstore.end())
250 return bstore.front();
256 Buffer * BufferList::previous(Buffer const * buf) const
262 BufferStorage::const_iterator it = find(bstore.begin(),
264 BOOST_ASSERT(it != bstore.end());
265 if (it == bstore.begin())
266 return bstore.back();
272 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
273 OutputParams const & runparams)
275 BufferStorage::iterator it = bstore.begin();
276 BufferStorage::iterator end = bstore.end();
277 for (; it != end; ++it) {
278 if (!(*it)->isDepClean(mastertmpdir)) {
279 string writefile = mastertmpdir;
281 writefile += (*it)->getLatexName();
282 (*it)->makeLaTeXFile(writefile, mastertmpdir,
284 (*it)->markDepClean(mastertmpdir);
290 void BufferList::emergencyWriteAll()
292 for_each(bstore.begin(), bstore.end(),
293 bind(&BufferList::emergencyWrite, this, _1));
297 void BufferList::emergencyWrite(Buffer * buf)
299 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
300 // compare with 0 to avoid pointer/interger comparison
303 // No need to save if the buffer has not changed.
307 string const doc = buf->isUnnamed()
308 ? onlyFilename(buf->fileName()) : buf->fileName();
310 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
312 // We try to save three places:
313 // 1) Same place as document. Unless it is an unnamed doc.
314 if (!buf->isUnnamed()) {
315 string s = buf->fileName();
317 lyxerr << " " << s << endl;
318 if (buf->writeFile(s)) {
320 lyxerr << _(" Save seems successful. Phew.") << endl;
323 lyxerr << _(" Save failed! Trying...") << endl;
327 // 2) In HOME directory.
328 string s = addName(package().home_dir(), buf->fileName());
330 lyxerr << ' ' << s << endl;
331 if (buf->writeFile(s)) {
333 lyxerr << _(" Save seems successful. Phew.") << endl;
337 lyxerr << _(" Save failed! Trying...") << endl;
339 // 3) In "/tmp" directory.
340 // MakeAbsPath to prepend the current
341 // drive letter on OS/2
342 s = addName(package().temp_dir(), buf->fileName());
344 lyxerr << ' ' << s << endl;
345 if (buf->writeFile(s)) {
347 lyxerr << _(" Save seems successful. Phew.") << endl;
350 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
354 bool BufferList::exists(string const & s) const
356 return find_if(bstore.begin(), bstore.end(),
357 bind(equal_to<string>(),
358 bind(&Buffer::fileName, _1),
364 bool BufferList::isLoaded(Buffer const * b) const
367 BufferStorage::const_iterator cit =
368 find(bstore.begin(), bstore.end(), b);
369 return cit != bstore.end();
373 Buffer * BufferList::getBuffer(string const & s)
375 BufferStorage::iterator it =
376 find_if(bstore.begin(), bstore.end(),
377 bind(equal_to<string>(),
378 bind(&Buffer::fileName, _1),
381 return it != bstore.end() ? (*it) : 0;
385 Buffer * BufferList::getBufferFromTmp(string const & s)
387 BufferStorage::iterator it = bstore.begin();
388 BufferStorage::iterator end = bstore.end();
389 for (; it < end; ++it)
390 if (prefixIs(s, (*it)->temppath()))
396 void BufferList::setCurrentAuthor(string const & name, string const & email)
398 BufferStorage::iterator it = bstore.begin();
399 BufferStorage::iterator end = bstore.end();
400 for (; it != end; ++it) {
401 (*it)->params().authors().record(0, Author(name, email));