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_fwd.h"
27 #include "frontends/Alert.h"
29 #include "support/filetools.h"
31 #include <boost/bind.hpp>
36 using lyx::support::AddName;
37 using lyx::support::bformat;
38 using lyx::support::GetEnvPath;
39 using lyx::support::MakeAbsPath;
40 using lyx::support::MakeDisplayPath;
41 using lyx::support::OnlyFilename;
42 using lyx::support::removeAutosaveFile;
43 using lyx::support::prefixIs;
55 using std::back_inserter;
59 BufferList::BufferList()
63 bool BufferList::empty() const
65 return bstore.empty();
69 bool BufferList::quitWriteBuffer(Buffer * buf)
73 file = OnlyFilename(buf->fileName());
75 file = MakeDisplayPath(buf->fileName(), 30);
77 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
78 "Do you want to save the document or discard the changes?"), file);
79 int const ret = Alert::prompt(_("Save changed document?"),
80 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
83 // FIXME: WriteAs can be asynch !
84 // but not right now...maybe we should remove that
89 succeeded = WriteAs(buf);
91 succeeded = MenuWrite(buf);
95 } else if (ret == 1) {
96 // if we crash after this we could
97 // have no autosave file but I guess
98 // this is really inprobable (Jug)
100 removeAutosaveFile(buf->fileName());
110 bool BufferList::quitWriteAll()
112 BufferStorage::iterator it = bstore.begin();
113 BufferStorage::iterator end = bstore.end();
114 for (; it != end; ++it) {
115 if ((*it)->isClean())
118 if (!quitWriteBuffer(*it))
126 void BufferList::release(Buffer * buf)
129 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
130 if (it != bstore.end()) {
131 Buffer * tmp = (*it);
138 Buffer * BufferList::newBuffer(string const & s, bool ronly)
140 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
141 tmpbuf->params().useClassDefaults();
142 lyxerr[Debug::INFO] << "Assigning to buffer "
143 << bstore.size() << endl;
144 bstore.push_back(tmpbuf.get());
145 return tmpbuf.release();
149 void BufferList::closeAll()
151 while (!bstore.empty()) {
152 close(bstore.front(), false);
157 bool BufferList::close(Buffer * buf, bool ask)
161 // FIXME: is the quitting check still necessary ?
162 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
168 if (buf->isUnnamed())
169 fname = OnlyFilename(buf->fileName());
171 fname = MakeDisplayPath(buf->fileName(), 30);
173 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
174 "Do you want to save the document or discard the changes?"), fname);
175 int const ret = Alert::prompt(_("Save changed document?"),
176 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
179 if (buf->isUnnamed()) {
182 } else if (buf->save()) {
183 LyX::ref().lastfiles().newFile(buf->fileName());
187 } else if (ret == 2) {
191 if (buf->isUnnamed()) {
192 removeAutosaveFile(buf->fileName());
200 vector<string> const BufferList::getFileNames() const
203 transform(bstore.begin(), bstore.end(),
205 boost::bind(&Buffer::fileName, _1));
210 Buffer * BufferList::first()
214 return bstore.front();
218 Buffer * BufferList::getBuffer(unsigned int choice)
220 if (choice >= bstore.size())
222 return bstore[choice];
226 Buffer * BufferList::next(Buffer const * buf) const
230 BufferStorage::const_iterator it = find(bstore.begin(),
232 BOOST_ASSERT(it != bstore.end());
234 if (it == bstore.end())
235 return bstore.front();
241 Buffer * BufferList::previous(Buffer const * buf) const
245 BufferStorage::const_iterator it = find(bstore.begin(),
247 BOOST_ASSERT(it != bstore.end());
248 if (it == bstore.begin())
249 return bstore.back();
255 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
256 OutputParams const & runparams)
258 BufferStorage::iterator it = bstore.begin();
259 BufferStorage::iterator end = bstore.end();
260 for (; it != end; ++it) {
261 if (!(*it)->isDepClean(mastertmpdir)) {
262 string writefile = mastertmpdir;
264 writefile += (*it)->getLatexName();
265 (*it)->makeLaTeXFile(writefile, mastertmpdir,
267 (*it)->markDepClean(mastertmpdir);
273 void BufferList::emergencyWriteAll()
275 for_each(bstore.begin(), bstore.end(),
276 bind(&BufferList::emergencyWrite, this, _1));
280 void BufferList::emergencyWrite(Buffer * buf)
282 // assert(buf) // this is not good since C assert takes an int
283 // and a pointer is a long (JMarc)
284 assert(buf != 0); // use c assert to avoid a loop
287 // No need to save if the buffer has not changed.
291 string const doc = buf->isUnnamed()
292 ? OnlyFilename(buf->fileName()) : buf->fileName();
294 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
296 // We try to save three places:
297 // 1) Same place as document. Unless it is an unnamed doc.
298 if (!buf->isUnnamed()) {
299 string s = buf->fileName();
301 lyxerr << " " << s << endl;
302 if (buf->writeFile(s)) {
304 lyxerr << _(" Save seems successful. Phew.") << endl;
307 lyxerr << _(" Save failed! Trying...") << endl;
311 // 2) In HOME directory.
312 string s = AddName(GetEnvPath("HOME"), buf->fileName());
314 lyxerr << ' ' << s << endl;
315 if (buf->writeFile(s)) {
317 lyxerr << _(" Save seems successful. Phew.") << endl;
321 lyxerr << _(" Save failed! Trying...") << endl;
323 // 3) In "/tmp" directory.
324 // MakeAbsPath to prepend the current
325 // drive letter on OS/2
326 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
328 lyxerr << ' ' << s << endl;
329 if (buf->writeFile(s)) {
331 lyxerr << _(" Save seems successful. Phew.") << endl;
334 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
338 bool BufferList::exists(string const & s) const
340 return find_if(bstore.begin(), bstore.end(),
341 bind(equal_to<string>(),
342 bind(&Buffer::fileName, _1),
348 bool BufferList::isLoaded(Buffer const * b) const
351 BufferStorage::const_iterator cit =
352 find(bstore.begin(), bstore.end(), b);
353 return cit != bstore.end();
357 Buffer * BufferList::getBuffer(string const & s)
359 BufferStorage::iterator it =
360 find_if(bstore.begin(), bstore.end(),
361 bind(equal_to<string>(),
362 bind(&Buffer::fileName, _1),
365 return it != bstore.end() ? (*it) : 0;
369 Buffer * BufferList::getBufferFromTmp(string const & s)
371 BufferStorage::iterator it = bstore.begin();
372 BufferStorage::iterator end = bstore.end();
373 for (; it < end; ++it)
374 if (prefixIs(s, (*it)->temppath()))
380 void BufferList::setCurrentAuthor(string const & name, string const & email)
382 BufferStorage::iterator it = bstore.begin();
383 BufferStorage::iterator end = bstore.end();
384 for (; it != end; ++it) {
385 (*it)->params().authors().record(0, Author(name, email));