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"
21 #include "bufferview_funcs.h"
22 #include "BufferView.h"
24 #include "frontends/LyXView.h"
25 #include "vc-backend.h"
26 #include "TextCache.h"
29 #include "frontends/Alert.h"
31 #include "support/FileInfo.h"
32 #include "support/filetools.h"
33 #include "support/lyxmanip.h"
34 #include "support/lyxfunctional.h"
35 #include "support/LAssert.h"
37 #include <boost/bind.hpp>
44 using namespace lyx::support;
54 BufferList::BufferList()
58 bool BufferList::empty() const
60 return bstore.empty();
64 bool BufferList::quitWriteBuffer(Buffer * buf)
68 file = OnlyFilename(buf->fileName());
70 file = MakeDisplayPath(buf->fileName(), 30);
72 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
73 "Do you want to save the document or discard the changes?"), file);
74 int const ret = Alert::prompt(_("Save changed document?"),
75 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
78 // FIXME: WriteAs can be asynch !
79 // but not right now...maybe we should remove that
84 succeeded = WriteAs(buf);
86 succeeded = MenuWrite(buf);
90 } else if (ret == 1) {
91 // if we crash after this we could
92 // have no autosave file but I guess
93 // this is really inprobable (Jug)
95 removeAutosaveFile(buf->fileName());
105 bool BufferList::quitWriteAll()
107 BufferStorage::iterator it = bstore.begin();
108 BufferStorage::iterator end = bstore.end();
109 for (; it != end; ++it) {
110 if ((*it)->isClean())
113 if (!quitWriteBuffer(*it))
121 void BufferList::release(Buffer * buf)
124 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
125 if (it != bstore.end()) {
126 // Make sure that we don't store a LyXText in
127 // the textcache that points to the buffer
129 Buffer * tmp = (*it);
131 textcache.removeAllWithBuffer(tmp);
137 Buffer * BufferList::newBuffer(string const & s, bool ronly)
139 Buffer * tmpbuf = new Buffer(s, ronly);
140 tmpbuf->params.useClassDefaults();
141 lyxerr[Debug::INFO] << "Assigning to buffer "
142 << bstore.size() << endl;
143 bstore.push_back(tmpbuf);
148 void BufferList::closeAll()
150 // Since we are closing we can just as well delete all
151 // in the textcache this will also speed the closing/quiting up a bit.
154 while (!bstore.empty()) {
155 close(bstore.front(), false);
160 bool BufferList::close(Buffer * buf, bool ask)
164 // FIXME: is the quitting check still necessary ?
165 if (!ask || buf->isClean() || quitting || buf->paragraphs.empty()) {
171 if (buf->isUnnamed())
172 fname = OnlyFilename(buf->fileName());
174 fname = MakeDisplayPath(buf->fileName(), 30);
176 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
177 "Do you want to save the document or discard the changes?"), fname);
178 int const ret = Alert::prompt(_("Save changed document?"),
179 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
182 if (buf->isUnnamed()) {
185 } else if (buf->save()) {
186 lastfiles->newFile(buf->fileName());
190 } else if (ret == 2) {
194 if (buf->isUnnamed()) {
195 removeAutosaveFile(buf->fileName());
203 vector<string> const BufferList::getFileNames() const
206 std::copy(bstore.begin(), bstore.end(),
207 lyx::back_inserter_fun(nvec, &Buffer::fileName));
212 Buffer * BufferList::first()
216 return bstore.front();
220 Buffer * BufferList::getBuffer(unsigned int choice)
222 if (choice >= bstore.size())
224 return bstore[choice];
228 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
229 LatexRunParams const & runparams)
231 BufferStorage::iterator it = bstore.begin();
232 BufferStorage::iterator end = bstore.end();
233 for (; it != end; ++it) {
234 if (!(*it)->isDepClean(mastertmpdir)) {
235 string writefile = mastertmpdir;
237 writefile += (*it)->getLatexName();
238 (*it)->makeLaTeXFile(writefile, mastertmpdir,
240 (*it)->markDepClean(mastertmpdir);
246 void BufferList::emergencyWriteAll()
248 for_each(bstore.begin(), bstore.end(),
249 boost::bind(&BufferList::emergencyWrite, this, _1));
253 void BufferList::emergencyWrite(Buffer * buf)
255 // assert(buf) // this is not good since C assert takes an int
256 // and a pointer is a long (JMarc)
257 assert(buf != 0); // use c assert to avoid a loop
260 // No need to save if the buffer has not changed.
264 string const doc = buf->isUnnamed()
265 ? OnlyFilename(buf->fileName()) : buf->fileName();
267 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
269 // We try to save three places:
270 // 1) Same place as document. Unless it is an unnamed doc.
271 if (!buf->isUnnamed()) {
272 string s = buf->fileName();
274 lyxerr << " " << s << endl;
275 if (buf->writeFile(s)) {
277 lyxerr << _(" Save seems successful. Phew.") << endl;
280 lyxerr << _(" Save failed! Trying...") << endl;
284 // 2) In HOME directory.
285 string s = AddName(GetEnvPath("HOME"), buf->fileName());
287 lyxerr << ' ' << s << endl;
288 if (buf->writeFile(s)) {
290 lyxerr << _(" Save seems successful. Phew.") << endl;
294 lyxerr << _(" Save failed! Trying...") << endl;
296 // 3) In "/tmp" directory.
297 // MakeAbsPath to prepend the current
298 // drive letter on OS/2
299 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
301 lyxerr << ' ' << s << endl;
302 if (buf->writeFile(s)) {
304 lyxerr << _(" Save seems successful. Phew.") << endl;
307 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
311 bool BufferList::exists(string const & s) const
313 return find_if(bstore.begin(), bstore.end(),
314 lyx::compare_memfun(&Buffer::fileName, s))
319 bool BufferList::isLoaded(Buffer const * b) const
323 BufferStorage::const_iterator cit =
324 find(bstore.begin(), bstore.end(), b);
325 return cit != bstore.end();
329 Buffer * BufferList::getBuffer(string const & s)
331 BufferStorage::iterator it =
332 find_if(bstore.begin(), bstore.end(),
333 lyx::compare_memfun(&Buffer::fileName, s));
334 return it != bstore.end() ? (*it) : 0;
338 void BufferList::setCurrentAuthor(string const & name, string const & email)
340 BufferStorage::iterator it = bstore.begin();
341 BufferStorage::iterator end = bstore.end();
342 for (; it != end; ++it) {
343 (*it)->authors().record(0, Author(name, email));