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::last()
242 return bstore.back();
246 Buffer * BufferList::getBuffer(unsigned int const choice)
248 if (choice >= bstore.size())
250 return bstore[choice];
254 Buffer * BufferList::next(Buffer const * buf) const
260 BufferStorage::const_iterator it = find(bstore.begin(),
262 BOOST_ASSERT(it != bstore.end());
264 if (it == bstore.end())
265 return bstore.front();
271 Buffer * BufferList::previous(Buffer const * buf) const
277 BufferStorage::const_iterator it = find(bstore.begin(),
279 BOOST_ASSERT(it != bstore.end());
280 if (it == bstore.begin())
281 return bstore.back();
287 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
288 OutputParams const & runparams)
290 BufferStorage::iterator it = bstore.begin();
291 BufferStorage::iterator end = bstore.end();
292 for (; it != end; ++it) {
293 if (!(*it)->isDepClean(mastertmpdir)) {
294 string writefile = mastertmpdir;
296 writefile += (*it)->getLatexName();
297 (*it)->makeLaTeXFile(writefile, mastertmpdir,
299 (*it)->markDepClean(mastertmpdir);
305 void BufferList::emergencyWriteAll()
307 for_each(bstore.begin(), bstore.end(),
308 bind(&BufferList::emergencyWrite, this, _1));
312 void BufferList::emergencyWrite(Buffer * buf)
314 // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
315 // compare with 0 to avoid pointer/interger comparison
318 // No need to save if the buffer has not changed.
322 string const doc = buf->isUnnamed()
323 ? onlyFilename(buf->fileName()) : buf->fileName();
326 bformat(_("LyX: Attempting to save document %1$s"), from_utf8(doc)))
329 // We try to save three places:
330 // 1) Same place as document. Unless it is an unnamed doc.
331 if (!buf->isUnnamed()) {
332 string s = buf->fileName();
334 lyxerr << " " << s << endl;
335 if (buf->writeFile(s)) {
337 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
340 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
344 // 2) In HOME directory.
345 string s = addName(package().home_dir(), buf->fileName());
347 lyxerr << ' ' << s << endl;
348 if (buf->writeFile(s)) {
350 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
354 lyxerr << to_utf8(_(" Save failed! Trying...")) << endl;
356 // 3) In "/tmp" directory.
357 // MakeAbsPath to prepend the current
358 // drive letter on OS/2
359 s = addName(package().temp_dir(), buf->fileName());
361 lyxerr << ' ' << s << endl;
362 if (buf->writeFile(s)) {
364 lyxerr << to_utf8(_(" Save seems successful. Phew.")) << endl;
367 lyxerr << to_utf8(_(" Save failed! Bummer. Document is lost.")) << endl;
371 bool BufferList::exists(string const & s) const
373 return find_if(bstore.begin(), bstore.end(),
374 bind(equal_to<string>(),
375 bind(&Buffer::fileName, _1),
381 bool BufferList::isLoaded(Buffer const * b) const
384 BufferStorage::const_iterator cit =
385 find(bstore.begin(), bstore.end(), b);
386 return cit != bstore.end();
390 Buffer * BufferList::getBuffer(string const & s)
392 BufferStorage::iterator it =
393 find_if(bstore.begin(), bstore.end(),
394 bind(equal_to<string>(),
395 bind(&Buffer::fileName, _1),
398 return it != bstore.end() ? (*it) : 0;
402 Buffer * BufferList::getBufferFromTmp(string const & s)
404 BufferStorage::iterator it = bstore.begin();
405 BufferStorage::iterator end = bstore.end();
406 for (; it < end; ++it)
407 if (prefixIs(s, (*it)->temppath()))
413 void BufferList::setCurrentAuthor(string const & name, string const & email)
415 BufferStorage::iterator it = bstore.begin();
416 BufferStorage::iterator end = bstore.end();
417 for (; it != end; ++it) {
418 (*it)->params().authors().record(0, Author(name, email));