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;
61 BufferList::BufferList()
65 bool BufferList::empty() const
67 return bstore.empty();
71 bool BufferList::quitWriteBuffer(Buffer * buf)
77 file = lyx::from_utf8(onlyFilename(buf->fileName()));
79 file = makeDisplayPath(buf->fileName(), 30);
81 docstring const text =
82 bformat(_("The document %1$s has unsaved changes.\n\n"
83 "Do you want to save the document or discard the changes?"),
85 int const ret = Alert::prompt(_("Save changed document?"),
86 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
89 // FIXME: WriteAs can be asynch !
90 // but not right now...maybe we should remove that
95 succeeded = writeAs(buf);
97 succeeded = menuWrite(buf);
101 } else if (ret == 1) {
102 // if we crash after this we could
103 // have no autosave file but I guess
104 // this is really inprobable (Jug)
105 if (buf->isUnnamed())
106 removeAutosaveFile(buf->fileName());
116 bool BufferList::quitWriteAll()
118 BufferStorage::iterator it = bstore.begin();
119 BufferStorage::iterator end = bstore.end();
120 for (; it != end; ++it) {
121 if ((*it)->isClean())
124 if (!quitWriteBuffer(*it))
127 // now, all buffers have been written sucessfully
128 // save file names to .lyx/session
130 for (; it != end; ++it) {
131 // if master/slave are both open, do not save slave since it
132 // will be automatically loaded when the master is loaded
133 if ((*it)->getMasterBuffer() == (*it))
134 LyX::ref().session().addLastOpenedFile((*it)->fileName());
141 void BufferList::release(Buffer * buf)
144 BufferStorage::iterator const it =
145 find(bstore.begin(), bstore.end(), buf);
146 if (it != bstore.end()) {
147 Buffer * tmp = (*it);
155 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
157 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
158 tmpbuf->params().useClassDefaults();
159 lyxerr[Debug::INFO] << "Assigning to buffer "
160 << bstore.size() << endl;
161 bstore.push_back(tmpbuf.get());
162 return tmpbuf.release();
166 void BufferList::closeAll()
168 while (!bstore.empty()) {
169 close(bstore.front(), false);
174 bool BufferList::close(Buffer * buf, bool const ask)
178 if (!ask || buf->isClean() || buf->paragraphs().empty()) {
184 if (buf->isUnnamed())
185 fname = lyx::from_utf8(onlyFilename(buf->fileName()));
187 fname = makeDisplayPath(buf->fileName(), 30);
189 docstring const text =
190 bformat(_("The document %1$s has unsaved changes.\n\n"
191 "Do you want to save the document or discard the changes?"),
193 int const ret = Alert::prompt(_("Save changed document?"),
194 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
197 if (buf->isUnnamed()) {
200 } else if (!menuWrite(buf))
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 << lyx::to_utf8(
314 bformat(_("LyX: Attempting to save document %1$s"), lyx::from_utf8(doc)))
317 // We try to save three places:
318 // 1) Same place as document. Unless it is an unnamed doc.
319 if (!buf->isUnnamed()) {
320 string s = buf->fileName();
322 lyxerr << " " << s << endl;
323 if (buf->writeFile(s)) {
325 lyxerr << lyx::to_utf8(_(" Save seems successful. Phew.")) << endl;
328 lyxerr << lyx::to_utf8(_(" Save failed! Trying...")) << endl;
332 // 2) In HOME directory.
333 string s = addName(package().home_dir(), buf->fileName());
335 lyxerr << ' ' << s << endl;
336 if (buf->writeFile(s)) {
338 lyxerr << lyx::to_utf8(_(" Save seems successful. Phew.")) << endl;
342 lyxerr << lyx::to_utf8(_(" Save failed! Trying...")) << endl;
344 // 3) In "/tmp" directory.
345 // MakeAbsPath to prepend the current
346 // drive letter on OS/2
347 s = addName(package().temp_dir(), buf->fileName());
349 lyxerr << ' ' << s << endl;
350 if (buf->writeFile(s)) {
352 lyxerr << lyx::to_utf8(_(" Save seems successful. Phew.")) << endl;
355 lyxerr << lyx::to_utf8(_(" Save failed! Bummer. Document is lost.")) << endl;
359 bool BufferList::exists(string const & s) const
361 return find_if(bstore.begin(), bstore.end(),
362 bind(equal_to<string>(),
363 bind(&Buffer::fileName, _1),
369 bool BufferList::isLoaded(Buffer const * b) const
372 BufferStorage::const_iterator cit =
373 find(bstore.begin(), bstore.end(), b);
374 return cit != bstore.end();
378 Buffer * BufferList::getBuffer(string const & s)
380 BufferStorage::iterator it =
381 find_if(bstore.begin(), bstore.end(),
382 bind(equal_to<string>(),
383 bind(&Buffer::fileName, _1),
386 return it != bstore.end() ? (*it) : 0;
390 Buffer * BufferList::getBufferFromTmp(string const & s)
392 BufferStorage::iterator it = bstore.begin();
393 BufferStorage::iterator end = bstore.end();
394 for (; it < end; ++it)
395 if (prefixIs(s, (*it)->temppath()))
401 void BufferList::setCurrentAuthor(string const & name, string const & email)
403 BufferStorage::iterator it = bstore.begin();
404 BufferStorage::iterator end = bstore.end();
405 for (; it != end; ++it) {
406 (*it)->params().authors().record(0, Author(name, email));