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"
15 #include "lastfiles.h"
20 #include "TextCache.h"
22 #include "frontends/Alert.h"
24 #include "support/filetools.h"
25 #include "support/lyxfunctional.h"
26 #include "support/LAssert.h"
28 #include <boost/bind.hpp>
32 using namespace lyx::support;
42 BufferList::BufferList()
46 bool BufferList::empty() const
48 return bstore.empty();
52 bool BufferList::quitWriteBuffer(Buffer * buf)
56 file = OnlyFilename(buf->fileName());
58 file = MakeDisplayPath(buf->fileName(), 30);
60 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
61 "Do you want to save the document or discard the changes?"), file);
62 int const ret = Alert::prompt(_("Save changed document?"),
63 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
66 // FIXME: WriteAs can be asynch !
67 // but not right now...maybe we should remove that
72 succeeded = WriteAs(buf);
74 succeeded = MenuWrite(buf);
78 } else if (ret == 1) {
79 // if we crash after this we could
80 // have no autosave file but I guess
81 // this is really inprobable (Jug)
83 removeAutosaveFile(buf->fileName());
93 bool BufferList::quitWriteAll()
95 BufferStorage::iterator it = bstore.begin();
96 BufferStorage::iterator end = bstore.end();
97 for (; it != end; ++it) {
101 if (!quitWriteBuffer(*it))
109 void BufferList::release(Buffer * buf)
112 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
113 if (it != bstore.end()) {
114 // Make sure that we don't store a LyXText in
115 // the textcache that points to the buffer
117 Buffer * tmp = (*it);
119 textcache.removeAllWithBuffer(tmp);
125 Buffer * BufferList::newBuffer(string const & s, bool ronly)
127 Buffer * tmpbuf = new Buffer(s, ronly);
128 tmpbuf->params.useClassDefaults();
129 lyxerr[Debug::INFO] << "Assigning to buffer "
130 << bstore.size() << endl;
131 bstore.push_back(tmpbuf);
136 void BufferList::closeAll()
138 // Since we are closing we can just as well delete all
139 // in the textcache this will also speed the closing/quiting up a bit.
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 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
311 BufferStorage::const_iterator cit =
312 find(bstore.begin(), bstore.end(), b);
313 return cit != bstore.end();
317 Buffer * BufferList::getBuffer(string const & s)
319 BufferStorage::iterator it =
320 find_if(bstore.begin(), bstore.end(),
321 lyx::compare_memfun(&Buffer::fileName, s));
322 return it != bstore.end() ? (*it) : 0;
326 void BufferList::setCurrentAuthor(string const & name, string const & email)
328 BufferStorage::iterator it = bstore.begin();
329 BufferStorage::iterator end = bstore.end();
330 for (; it != end; ++it) {
331 (*it)->authors().record(0, Author(name, email));