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>
40 using support::addName;
41 using support::bformat;
42 using support::makeAbsPath;
43 using support::makeDisplayPath;
44 using support::onlyFilename;
45 using support::removeAutosaveFile;
46 using support::package;
47 using support::prefixIs;
59 using std::back_inserter;
62 namespace Alert = lyx::frontend::Alert;
65 BufferList::BufferList()
69 bool BufferList::empty() const
71 return bstore.empty();
75 bool BufferList::quitWriteBuffer(Buffer * buf)
81 file = from_utf8(onlyFilename(buf->fileName()));
83 file = makeDisplayPath(buf->fileName(), 30);
85 docstring const text =
86 bformat(_("The document %1$s has unsaved changes.\n\n"
87 "Do you want to save the document or discard the changes?"),
89 int const ret = Alert::prompt(_("Save changed document?"),
90 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
93 // FIXME: WriteAs can be asynch !
94 // but not right now...maybe we should remove that
99 succeeded = writeAs(buf);
101 succeeded = menuWrite(buf);
105 } else if (ret == 1) {
106 // if we crash after this we could
107 // have no autosave file but I guess
108 // this is really inprobable (Jug)
109 if (buf->isUnnamed())
110 removeAutosaveFile(buf->fileName());
120 bool BufferList::quitWriteAll()
122 BufferStorage::iterator it = bstore.begin();
123 BufferStorage::iterator end = bstore.end();
124 for (; it != end; ++it) {
125 if ((*it)->isClean())
128 if (!quitWriteBuffer(*it))
131 // now, all buffers have been written sucessfully
132 // save file names to .lyx/session
134 for (; it != end; ++it) {
135 // if master/slave are both open, do not save slave since it
136 // will be automatically loaded when the master is loaded
137 if ((*it)->getMasterBuffer() == (*it))
138 LyX::ref().session().lastOpened().add((*it)->fileName());
145 void BufferList::release(Buffer * buf)
148 BufferStorage::iterator const it =
149 find(bstore.begin(), bstore.end(), buf);
150 if (it != bstore.end()) {
151 Buffer * tmp = (*it);
159 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
161 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
162 tmpbuf->params().useClassDefaults();
163 lyxerr[Debug::INFO] << "Assigning to buffer "
164 << bstore.size() << endl;
165 bstore.push_back(tmpbuf.get());
166 return tmpbuf.release();
170 void BufferList::closeAll()
172 while (!bstore.empty()) {
173 close(bstore.front(), false);
178 bool BufferList::close(Buffer * buf, bool const ask)
182 if (!ask || buf->isClean() || buf->paragraphs().empty()) {
188 if (buf->isUnnamed())
189 fname = from_utf8(onlyFilename(buf->fileName()));
191 fname = makeDisplayPath(buf->fileName(), 30);
193 docstring const text =
194 bformat(_("The document %1$s has unsaved changes.\n\n"
195 "Do you want to save the document or discard the changes?"),
197 int const ret = Alert::prompt(_("Save changed document?"),
198 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
201 if (buf->isUnnamed()) {
204 } else if (!menuWrite(buf))
211 if (buf->isUnnamed()) {
212 removeAutosaveFile(buf->fileName());
220 vector<string> const BufferList::getFileNames() const
223 transform(bstore.begin(), bstore.end(),
225 boost::bind(&Buffer::fileName, _1));
230 Buffer * BufferList::first()
234 return bstore.front();
238 Buffer * BufferList::getBuffer(unsigned int const choice)
240 if (choice >= bstore.size())
242 return bstore[choice];
246 Buffer * BufferList::next(Buffer const * buf) const
252 BufferStorage::const_iterator it = find(bstore.begin(),
254 BOOST_ASSERT(it != bstore.end());
256 if (it == bstore.end())
257 return bstore.front();
263 Buffer * BufferList::previous(Buffer const * buf) const
269 BufferStorage::const_iterator it = find(bstore.begin(),
271 BOOST_ASSERT(it != bstore.end());
272 if (it == bstore.begin())
273 return bstore.back();
279 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
280 OutputParams const & runparams)
282 BufferStorage::iterator it = bstore.begin();
283 BufferStorage::iterator end = bstore.end();
284 for (; it != end; ++it) {
285 if (!(*it)->isDepClean(mastertmpdir)) {
286 string writefile = mastertmpdir;
288 writefile += (*it)->getLatexName();
289 (*it)->makeLaTeXFile(writefile, mastertmpdir,
291 (*it)->markDepClean(mastertmpdir);
297 void BufferList::emergencyWriteAll()
299 for_each(bstore.begin(), bstore.end(),
300 bind(&BufferList::emergencyWrite, this, _1));
304 void BufferList::emergencyWrite(Buffer * buf)
306 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
307 // compare with 0 to avoid pointer/interger comparison
310 // No need to save if the buffer has not changed.
314 string const doc = buf->isUnnamed()
315 ? onlyFilename(buf->fileName()) : buf->fileName();
318 bformat(_("LyX: Attempting to save document %1$s"), from_utf8(doc)))
321 // We try to save three places:
322 // 1) Same place as document. Unless it is an unnamed doc.
323 if (!buf->isUnnamed()) {
324 string s = buf->fileName();
326 lyxerr << " " << s << endl;
327 if (buf->writeFile(s)) {
329 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
332 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
336 // 2) In HOME directory.
337 string s = addName(package().home_dir(), buf->fileName());
339 lyxerr << ' ' << s << endl;
340 if (buf->writeFile(s)) {
342 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
346 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
348 // 3) In "/tmp" directory.
349 // MakeAbsPath to prepend the current
350 // drive letter on OS/2
351 s = addName(package().temp_dir(), buf->fileName());
353 lyxerr << ' ' << s << endl;
354 if (buf->writeFile(s)) {
356 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
359 lyxerr << to_utf8(_(" Save failed! Bummer. Document is lost.")) << endl;
363 bool BufferList::exists(string const & s) const
365 return find_if(bstore.begin(), bstore.end(),
366 bind(equal_to<string>(),
367 bind(&Buffer::fileName, _1),
373 bool BufferList::isLoaded(Buffer const * b) const
376 BufferStorage::const_iterator cit =
377 find(bstore.begin(), bstore.end(), b);
378 return cit != bstore.end();
382 Buffer * BufferList::getBuffer(string const & s)
384 BufferStorage::iterator it =
385 find_if(bstore.begin(), bstore.end(),
386 bind(equal_to<string>(),
387 bind(&Buffer::fileName, _1),
390 return it != bstore.end() ? (*it) : 0;
394 Buffer * BufferList::getBufferFromTmp(string const & s)
396 BufferStorage::iterator it = bstore.begin();
397 BufferStorage::iterator end = bstore.end();
398 for (; it < end; ++it)
399 if (prefixIs(s, (*it)->temppath()))
405 void BufferList::setCurrentAuthor(string const & name, string const & email)
407 BufferStorage::iterator it = bstore.begin();
408 BufferStorage::iterator end = bstore.end();
409 for (; it != end; ++it) {
410 (*it)->params().authors().record(0, Author(name, email));