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 // FIXME: is the quitting check still necessary ?
177 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
183 if (buf->isUnnamed())
184 fname = onlyFilename(buf->fileName());
186 fname = makeDisplayPath(buf->fileName(), 30);
189 bformat(_("The document %1$s has unsaved changes.\n\n"
190 "Do you want to save the document or discard the changes?"), fname);
191 int const ret = Alert::prompt(_("Save changed document?"),
192 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
195 if (buf->isUnnamed()) {
198 } else if (buf->save()) {
199 LyX::ref().session().addLastFile(buf->fileName());
203 } else if (ret == 2) {
207 if (buf->isUnnamed()) {
208 removeAutosaveFile(buf->fileName());
216 vector<string> const BufferList::getFileNames() const
219 transform(bstore.begin(), bstore.end(),
221 boost::bind(&Buffer::fileName, _1));
226 Buffer * BufferList::first()
230 return bstore.front();
234 Buffer * BufferList::getBuffer(unsigned int const choice)
236 if (choice >= bstore.size())
238 return bstore[choice];
242 Buffer * BufferList::next(Buffer const * buf) const
248 BufferStorage::const_iterator it = find(bstore.begin(),
250 BOOST_ASSERT(it != bstore.end());
252 if (it == bstore.end())
253 return bstore.front();
259 Buffer * BufferList::previous(Buffer const * buf) const
265 BufferStorage::const_iterator it = find(bstore.begin(),
267 BOOST_ASSERT(it != bstore.end());
268 if (it == bstore.begin())
269 return bstore.back();
275 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
276 OutputParams const & runparams)
278 BufferStorage::iterator it = bstore.begin();
279 BufferStorage::iterator end = bstore.end();
280 for (; it != end; ++it) {
281 if (!(*it)->isDepClean(mastertmpdir)) {
282 string writefile = mastertmpdir;
284 writefile += (*it)->getLatexName();
285 (*it)->makeLaTeXFile(writefile, mastertmpdir,
287 (*it)->markDepClean(mastertmpdir);
293 void BufferList::emergencyWriteAll()
295 for_each(bstore.begin(), bstore.end(),
296 bind(&BufferList::emergencyWrite, this, _1));
300 void BufferList::emergencyWrite(Buffer * buf)
302 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
303 // compare with 0 to avoid pointer/interger comparison
306 // No need to save if the buffer has not changed.
310 string const doc = buf->isUnnamed()
311 ? onlyFilename(buf->fileName()) : buf->fileName();
313 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
315 // We try to save three places:
316 // 1) Same place as document. Unless it is an unnamed doc.
317 if (!buf->isUnnamed()) {
318 string s = buf->fileName();
320 lyxerr << " " << s << endl;
321 if (buf->writeFile(s)) {
323 lyxerr << _(" Save seems successful. Phew.") << endl;
326 lyxerr << _(" Save failed! Trying...") << endl;
330 // 2) In HOME directory.
331 string s = addName(package().home_dir(), buf->fileName());
333 lyxerr << ' ' << s << endl;
334 if (buf->writeFile(s)) {
336 lyxerr << _(" Save seems successful. Phew.") << endl;
340 lyxerr << _(" Save failed! Trying...") << endl;
342 // 3) In "/tmp" directory.
343 // MakeAbsPath to prepend the current
344 // drive letter on OS/2
345 s = addName(package().temp_dir(), buf->fileName());
347 lyxerr << ' ' << s << endl;
348 if (buf->writeFile(s)) {
350 lyxerr << _(" Save seems successful. Phew.") << endl;
353 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
357 bool BufferList::exists(string const & s) const
359 return find_if(bstore.begin(), bstore.end(),
360 bind(equal_to<string>(),
361 bind(&Buffer::fileName, _1),
367 bool BufferList::isLoaded(Buffer const * b) const
370 BufferStorage::const_iterator cit =
371 find(bstore.begin(), bstore.end(), b);
372 return cit != bstore.end();
376 Buffer * BufferList::getBuffer(string const & s)
378 BufferStorage::iterator it =
379 find_if(bstore.begin(), bstore.end(),
380 bind(equal_to<string>(),
381 bind(&Buffer::fileName, _1),
384 return it != bstore.end() ? (*it) : 0;
388 Buffer * BufferList::getBufferFromTmp(string const & s)
390 BufferStorage::iterator it = bstore.begin();
391 BufferStorage::iterator end = bstore.end();
392 for (; it < end; ++it)
393 if (prefixIs(s, (*it)->temppath()))
399 void BufferList::setCurrentAuthor(string const & name, string const & email)
401 BufferStorage::iterator it = bstore.begin();
402 BufferStorage::iterator end = bstore.end();
403 for (; it != end; ++it) {
404 (*it)->params().authors().record(0, Author(name, email));