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 "paragraph.h"
25 #include "frontends/Alert.h"
27 #include "support/filetools.h"
28 #include "support/lyxfunctional.h"
30 #include <boost/bind.hpp>
32 using lyx::support::AddName;
33 using lyx::support::bformat;
34 using lyx::support::GetEnvPath;
35 using lyx::support::MakeAbsPath;
36 using lyx::support::MakeDisplayPath;
37 using lyx::support::OnlyFilename;
38 using lyx::support::removeAutosaveFile;
39 using lyx::support::prefixIs;
50 BufferList::BufferList()
54 bool BufferList::empty() const
56 return bstore.empty();
60 bool BufferList::quitWriteBuffer(Buffer * buf)
64 file = OnlyFilename(buf->fileName());
66 file = MakeDisplayPath(buf->fileName(), 30);
68 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
69 "Do you want to save the document or discard the changes?"), file);
70 int const ret = Alert::prompt(_("Save changed document?"),
71 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
74 // FIXME: WriteAs can be asynch !
75 // but not right now...maybe we should remove that
80 succeeded = WriteAs(buf);
82 succeeded = MenuWrite(buf);
86 } else if (ret == 1) {
87 // if we crash after this we could
88 // have no autosave file but I guess
89 // this is really inprobable (Jug)
91 removeAutosaveFile(buf->fileName());
101 bool BufferList::quitWriteAll()
103 BufferStorage::iterator it = bstore.begin();
104 BufferStorage::iterator end = bstore.end();
105 for (; it != end; ++it) {
106 if ((*it)->isClean())
109 if (!quitWriteBuffer(*it))
117 void BufferList::release(Buffer * buf)
120 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
121 if (it != bstore.end()) {
122 Buffer * tmp = (*it);
129 Buffer * BufferList::newBuffer(string const & s, bool ronly)
131 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
132 tmpbuf->params().useClassDefaults();
133 lyxerr[Debug::INFO] << "Assigning to buffer "
134 << bstore.size() << endl;
135 bstore.push_back(tmpbuf.get());
136 return tmpbuf.release();
140 void BufferList::closeAll()
142 while (!bstore.empty()) {
143 close(bstore.front(), false);
148 bool BufferList::close(Buffer * buf, bool ask)
152 // FIXME: is the quitting check still necessary ?
153 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
159 if (buf->isUnnamed())
160 fname = OnlyFilename(buf->fileName());
162 fname = MakeDisplayPath(buf->fileName(), 30);
164 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
165 "Do you want to save the document or discard the changes?"), fname);
166 int const ret = Alert::prompt(_("Save changed document?"),
167 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
170 if (buf->isUnnamed()) {
173 } else if (buf->save()) {
174 LyX::ref().lastfiles().newFile(buf->fileName());
178 } else if (ret == 2) {
182 if (buf->isUnnamed()) {
183 removeAutosaveFile(buf->fileName());
191 vector<string> const BufferList::getFileNames() const
194 std::copy(bstore.begin(), bstore.end(),
195 lyx::back_inserter_fun(nvec, &Buffer::fileName));
200 Buffer * BufferList::first()
204 return bstore.front();
208 Buffer * BufferList::getBuffer(unsigned int choice)
210 if (choice >= bstore.size())
212 return bstore[choice];
216 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
217 LatexRunParams const & runparams)
219 BufferStorage::iterator it = bstore.begin();
220 BufferStorage::iterator end = bstore.end();
221 for (; it != end; ++it) {
222 if (!(*it)->isDepClean(mastertmpdir)) {
223 string writefile = mastertmpdir;
225 writefile += (*it)->getLatexName();
226 (*it)->makeLaTeXFile(writefile, mastertmpdir,
228 (*it)->markDepClean(mastertmpdir);
234 void BufferList::emergencyWriteAll()
236 for_each(bstore.begin(), bstore.end(),
237 boost::bind(&BufferList::emergencyWrite, this, _1));
241 void BufferList::emergencyWrite(Buffer * buf)
243 // assert(buf) // this is not good since C assert takes an int
244 // and a pointer is a long (JMarc)
245 assert(buf != 0); // use c assert to avoid a loop
248 // No need to save if the buffer has not changed.
252 string const doc = buf->isUnnamed()
253 ? OnlyFilename(buf->fileName()) : buf->fileName();
255 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
257 // We try to save three places:
258 // 1) Same place as document. Unless it is an unnamed doc.
259 if (!buf->isUnnamed()) {
260 string s = buf->fileName();
262 lyxerr << " " << s << endl;
263 if (buf->writeFile(s)) {
265 lyxerr << _(" Save seems successful. Phew.") << endl;
268 lyxerr << _(" Save failed! Trying...") << endl;
272 // 2) In HOME directory.
273 string s = AddName(GetEnvPath("HOME"), buf->fileName());
275 lyxerr << ' ' << s << endl;
276 if (buf->writeFile(s)) {
278 lyxerr << _(" Save seems successful. Phew.") << endl;
282 lyxerr << _(" Save failed! Trying...") << endl;
284 // 3) In "/tmp" directory.
285 // MakeAbsPath to prepend the current
286 // drive letter on OS/2
287 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
289 lyxerr << ' ' << s << endl;
290 if (buf->writeFile(s)) {
292 lyxerr << _(" Save seems successful. Phew.") << endl;
295 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
299 bool BufferList::exists(string const & s) const
301 return find_if(bstore.begin(), bstore.end(),
302 lyx::compare_memfun(&Buffer::fileName, s))
307 bool BufferList::isLoaded(Buffer const * b) const
310 BufferStorage::const_iterator cit =
311 find(bstore.begin(), bstore.end(), b);
312 return cit != bstore.end();
316 Buffer * BufferList::getBuffer(string const & s)
318 BufferStorage::iterator it =
319 find_if(bstore.begin(), bstore.end(),
320 lyx::compare_memfun(&Buffer::fileName, s));
321 return it != bstore.end() ? (*it) : 0;
325 Buffer * BufferList::getBufferFromTmp(string const & s)
327 BufferStorage::iterator it = bstore.begin();
328 BufferStorage::iterator end = bstore.end();
329 for (; it < end; ++it)
330 if (prefixIs(s, (*it)->temppath()))
336 void BufferList::setCurrentAuthor(string const & name, string const & email)
338 BufferStorage::iterator it = bstore.begin();
339 BufferStorage::iterator end = bstore.end();
340 for (; it != end; ++it) {
341 (*it)->params().authors().record(0, Author(name, email));