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"
24 #include "TextCache.h"
26 #include "frontends/Alert.h"
28 #include "support/filetools.h"
29 #include "support/LAssert.h"
30 #include "support/lyxfunctional.h"
32 #include <boost/bind.hpp>
34 using namespace lyx::support;
44 BufferList::BufferList()
48 bool BufferList::empty() const
50 return bstore.empty();
54 bool BufferList::quitWriteBuffer(Buffer * buf)
58 file = OnlyFilename(buf->fileName());
60 file = MakeDisplayPath(buf->fileName(), 30);
62 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
63 "Do you want to save the document or discard the changes?"), file);
64 int const ret = Alert::prompt(_("Save changed document?"),
65 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
68 // FIXME: WriteAs can be asynch !
69 // but not right now...maybe we should remove that
74 succeeded = WriteAs(buf);
76 succeeded = MenuWrite(buf);
80 } else if (ret == 1) {
81 // if we crash after this we could
82 // have no autosave file but I guess
83 // this is really inprobable (Jug)
85 removeAutosaveFile(buf->fileName());
95 bool BufferList::quitWriteAll()
97 BufferStorage::iterator it = bstore.begin();
98 BufferStorage::iterator end = bstore.end();
99 for (; it != end; ++it) {
100 if ((*it)->isClean())
103 if (!quitWriteBuffer(*it))
111 void BufferList::release(Buffer * buf)
114 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
115 if (it != bstore.end()) {
116 // Make sure that we don't store a LyXText in
117 // the textcache that points to the buffer
119 Buffer * tmp = (*it);
121 textcache.removeAllWithBuffer(tmp);
127 Buffer * BufferList::newBuffer(string const & s, bool ronly)
129 Buffer * tmpbuf = new Buffer(s, ronly);
130 tmpbuf->params().useClassDefaults();
131 lyxerr[Debug::INFO] << "Assigning to buffer "
132 << bstore.size() << endl;
133 bstore.push_back(tmpbuf);
138 void BufferList::closeAll()
140 // Since we are closing we can just as well delete all
141 // in the textcache this will also speed the closing/quiting up a bit.
144 while (!bstore.empty()) {
145 close(bstore.front(), false);
150 bool BufferList::close(Buffer * buf, bool ask)
154 // FIXME: is the quitting check still necessary ?
155 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
161 if (buf->isUnnamed())
162 fname = OnlyFilename(buf->fileName());
164 fname = MakeDisplayPath(buf->fileName(), 30);
166 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
167 "Do you want to save the document or discard the changes?"), fname);
168 int const ret = Alert::prompt(_("Save changed document?"),
169 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
172 if (buf->isUnnamed()) {
175 } else if (buf->save()) {
176 lastfiles->newFile(buf->fileName());
180 } else if (ret == 2) {
184 if (buf->isUnnamed()) {
185 removeAutosaveFile(buf->fileName());
193 vector<string> const BufferList::getFileNames() const
196 std::copy(bstore.begin(), bstore.end(),
197 lyx::back_inserter_fun(nvec, &Buffer::fileName));
202 Buffer * BufferList::first()
206 return bstore.front();
210 Buffer * BufferList::getBuffer(unsigned int choice)
212 if (choice >= bstore.size())
214 return bstore[choice];
218 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
219 LatexRunParams const & runparams)
221 BufferStorage::iterator it = bstore.begin();
222 BufferStorage::iterator end = bstore.end();
223 for (; it != end; ++it) {
224 if (!(*it)->isDepClean(mastertmpdir)) {
225 string writefile = mastertmpdir;
227 writefile += (*it)->getLatexName();
228 (*it)->makeLaTeXFile(writefile, mastertmpdir,
230 (*it)->markDepClean(mastertmpdir);
236 void BufferList::emergencyWriteAll()
238 for_each(bstore.begin(), bstore.end(),
239 boost::bind(&BufferList::emergencyWrite, this, _1));
243 void BufferList::emergencyWrite(Buffer * buf)
245 // assert(buf) // this is not good since C assert takes an int
246 // and a pointer is a long (JMarc)
247 assert(buf != 0); // use c assert to avoid a loop
250 // No need to save if the buffer has not changed.
254 string const doc = buf->isUnnamed()
255 ? OnlyFilename(buf->fileName()) : buf->fileName();
257 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
259 // We try to save three places:
260 // 1) Same place as document. Unless it is an unnamed doc.
261 if (!buf->isUnnamed()) {
262 string s = buf->fileName();
264 lyxerr << " " << s << endl;
265 if (buf->writeFile(s)) {
267 lyxerr << _(" Save seems successful. Phew.") << endl;
270 lyxerr << _(" Save failed! Trying...") << endl;
274 // 2) In HOME directory.
275 string s = AddName(GetEnvPath("HOME"), buf->fileName());
277 lyxerr << ' ' << s << endl;
278 if (buf->writeFile(s)) {
280 lyxerr << _(" Save seems successful. Phew.") << endl;
284 lyxerr << _(" Save failed! Trying...") << endl;
286 // 3) In "/tmp" directory.
287 // MakeAbsPath to prepend the current
288 // drive letter on OS/2
289 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
291 lyxerr << ' ' << s << endl;
292 if (buf->writeFile(s)) {
294 lyxerr << _(" Save seems successful. Phew.") << endl;
297 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
301 bool BufferList::exists(string const & s) const
303 return find_if(bstore.begin(), bstore.end(),
304 lyx::compare_memfun(&Buffer::fileName, s))
309 bool BufferList::isLoaded(Buffer const * b) const
313 BufferStorage::const_iterator cit =
314 find(bstore.begin(), bstore.end(), b);
315 return cit != bstore.end();
319 Buffer * BufferList::getBuffer(string const & s)
321 BufferStorage::iterator it =
322 find_if(bstore.begin(), bstore.end(),
323 lyx::compare_memfun(&Buffer::fileName, s));
324 return it != bstore.end() ? (*it) : 0;
328 void BufferList::setCurrentAuthor(string const & name, string const & email)
330 BufferStorage::iterator it = bstore.begin();
331 BufferStorage::iterator end = bstore.end();
332 for (; it != end; ++it) {
333 (*it)->params().authors().record(0, Author(name, email));