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 "lastfiles.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))
130 void BufferList::release(Buffer * buf)
133 BufferStorage::iterator const it =
134 find(bstore.begin(), bstore.end(), buf);
135 if (it != bstore.end()) {
136 Buffer * tmp = (*it);
144 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
146 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
147 tmpbuf->params().useClassDefaults();
148 lyxerr[Debug::INFO] << "Assigning to buffer "
149 << bstore.size() << endl;
150 bstore.push_back(tmpbuf.get());
151 return tmpbuf.release();
155 void BufferList::closeAll()
157 while (!bstore.empty()) {
158 close(bstore.front(), false);
163 bool BufferList::close(Buffer * buf, bool const ask)
167 // FIXME: is the quitting check still necessary ?
168 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
174 if (buf->isUnnamed())
175 fname = OnlyFilename(buf->fileName());
177 fname = MakeDisplayPath(buf->fileName(), 30);
180 bformat(_("The document %1$s has unsaved changes.\n\n"
181 "Do you want to save the document or discard the changes?"), fname);
182 int const ret = Alert::prompt(_("Save changed document?"),
183 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
186 if (buf->isUnnamed()) {
189 } else if (buf->save()) {
190 LyX::ref().lastfiles().newFile(buf->fileName());
194 } else if (ret == 2) {
198 if (buf->isUnnamed()) {
199 removeAutosaveFile(buf->fileName());
207 vector<string> const BufferList::getFileNames() const
210 transform(bstore.begin(), bstore.end(),
212 boost::bind(&Buffer::fileName, _1));
217 Buffer * BufferList::first()
221 return bstore.front();
225 Buffer * BufferList::getBuffer(unsigned int const choice)
227 if (choice >= bstore.size())
229 return bstore[choice];
233 Buffer * BufferList::next(Buffer const * buf) const
239 BufferStorage::const_iterator it = find(bstore.begin(),
241 BOOST_ASSERT(it != bstore.end());
243 if (it == bstore.end())
244 return bstore.front();
250 Buffer * BufferList::previous(Buffer const * buf) const
256 BufferStorage::const_iterator it = find(bstore.begin(),
258 BOOST_ASSERT(it != bstore.end());
259 if (it == bstore.begin())
260 return bstore.back();
266 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
267 OutputParams const & runparams)
269 BufferStorage::iterator it = bstore.begin();
270 BufferStorage::iterator end = bstore.end();
271 for (; it != end; ++it) {
272 if (!(*it)->isDepClean(mastertmpdir)) {
273 string writefile = mastertmpdir;
275 writefile += (*it)->getLatexName();
276 (*it)->makeLaTeXFile(writefile, mastertmpdir,
278 (*it)->markDepClean(mastertmpdir);
284 void BufferList::emergencyWriteAll()
286 for_each(bstore.begin(), bstore.end(),
287 bind(&BufferList::emergencyWrite, this, _1));
291 void BufferList::emergencyWrite(Buffer * buf)
293 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
294 // compare with 0 to avoid pointer/interger comparison
297 // No need to save if the buffer has not changed.
301 string const doc = buf->isUnnamed()
302 ? OnlyFilename(buf->fileName()) : buf->fileName();
304 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
306 // We try to save three places:
307 // 1) Same place as document. Unless it is an unnamed doc.
308 if (!buf->isUnnamed()) {
309 string s = buf->fileName();
311 lyxerr << " " << s << endl;
312 if (buf->writeFile(s)) {
314 lyxerr << _(" Save seems successful. Phew.") << endl;
317 lyxerr << _(" Save failed! Trying...") << endl;
321 // 2) In HOME directory.
322 string s = AddName(package().home_dir(), buf->fileName());
324 lyxerr << ' ' << s << endl;
325 if (buf->writeFile(s)) {
327 lyxerr << _(" Save seems successful. Phew.") << endl;
331 lyxerr << _(" Save failed! Trying...") << endl;
333 // 3) In "/tmp" directory.
334 // MakeAbsPath to prepend the current
335 // drive letter on OS/2
336 s = AddName(package().temp_dir(), buf->fileName());
338 lyxerr << ' ' << s << endl;
339 if (buf->writeFile(s)) {
341 lyxerr << _(" Save seems successful. Phew.") << endl;
344 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
348 bool BufferList::exists(string const & s) const
350 return find_if(bstore.begin(), bstore.end(),
351 bind(equal_to<string>(),
352 bind(&Buffer::fileName, _1),
358 bool BufferList::isLoaded(Buffer const * b) const
361 BufferStorage::const_iterator cit =
362 find(bstore.begin(), bstore.end(), b);
363 return cit != bstore.end();
367 Buffer * BufferList::getBuffer(string const & s)
369 BufferStorage::iterator it =
370 find_if(bstore.begin(), bstore.end(),
371 bind(equal_to<string>(),
372 bind(&Buffer::fileName, _1),
375 return it != bstore.end() ? (*it) : 0;
379 Buffer * BufferList::getBufferFromTmp(string const & s)
381 BufferStorage::iterator it = bstore.begin();
382 BufferStorage::iterator end = bstore.end();
383 for (; it < end; ++it)
384 if (prefixIs(s, (*it)->temppath()))
390 void BufferList::setCurrentAuthor(string const & name, string const & email)
392 BufferStorage::iterator it = bstore.begin();
393 BufferStorage::iterator end = bstore.end();
394 for (; it != end; ++it) {
395 (*it)->params().authors().record(0, Author(name, email));