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;
49 BufferList::BufferList()
53 bool BufferList::empty() const
55 return bstore.empty();
59 bool BufferList::quitWriteBuffer(Buffer * buf)
63 file = OnlyFilename(buf->fileName());
65 file = MakeDisplayPath(buf->fileName(), 30);
67 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
68 "Do you want to save the document or discard the changes?"), file);
69 int const ret = Alert::prompt(_("Save changed document?"),
70 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
73 // FIXME: WriteAs can be asynch !
74 // but not right now...maybe we should remove that
79 succeeded = WriteAs(buf);
81 succeeded = MenuWrite(buf);
85 } else if (ret == 1) {
86 // if we crash after this we could
87 // have no autosave file but I guess
88 // this is really inprobable (Jug)
90 removeAutosaveFile(buf->fileName());
100 bool BufferList::quitWriteAll()
102 BufferStorage::iterator it = bstore.begin();
103 BufferStorage::iterator end = bstore.end();
104 for (; it != end; ++it) {
105 if ((*it)->isClean())
108 if (!quitWriteBuffer(*it))
116 void BufferList::release(Buffer * buf)
119 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
120 if (it != bstore.end()) {
121 Buffer * tmp = (*it);
128 Buffer * BufferList::newBuffer(string const & s, bool ronly)
130 Buffer * tmpbuf = new Buffer(s, ronly);
131 tmpbuf->params().useClassDefaults();
132 lyxerr[Debug::INFO] << "Assigning to buffer "
133 << bstore.size() << endl;
134 bstore.push_back(tmpbuf);
139 void BufferList::closeAll()
141 while (!bstore.empty()) {
142 close(bstore.front(), false);
147 bool BufferList::close(Buffer * buf, bool ask)
151 // FIXME: is the quitting check still necessary ?
152 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
158 if (buf->isUnnamed())
159 fname = OnlyFilename(buf->fileName());
161 fname = MakeDisplayPath(buf->fileName(), 30);
163 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
164 "Do you want to save the document or discard the changes?"), fname);
165 int const ret = Alert::prompt(_("Save changed document?"),
166 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
169 if (buf->isUnnamed()) {
172 } else if (buf->save()) {
173 LyX::ref().lastfiles().newFile(buf->fileName());
177 } else if (ret == 2) {
181 if (buf->isUnnamed()) {
182 removeAutosaveFile(buf->fileName());
190 vector<string> const BufferList::getFileNames() const
193 std::copy(bstore.begin(), bstore.end(),
194 lyx::back_inserter_fun(nvec, &Buffer::fileName));
199 Buffer * BufferList::first()
203 return bstore.front();
207 Buffer * BufferList::getBuffer(unsigned int choice)
209 if (choice >= bstore.size())
211 return bstore[choice];
215 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
216 LatexRunParams const & runparams)
218 BufferStorage::iterator it = bstore.begin();
219 BufferStorage::iterator end = bstore.end();
220 for (; it != end; ++it) {
221 if (!(*it)->isDepClean(mastertmpdir)) {
222 string writefile = mastertmpdir;
224 writefile += (*it)->getLatexName();
225 (*it)->makeLaTeXFile(writefile, mastertmpdir,
227 (*it)->markDepClean(mastertmpdir);
233 void BufferList::emergencyWriteAll()
235 for_each(bstore.begin(), bstore.end(),
236 boost::bind(&BufferList::emergencyWrite, this, _1));
240 void BufferList::emergencyWrite(Buffer * buf)
242 // assert(buf) // this is not good since C assert takes an int
243 // and a pointer is a long (JMarc)
244 assert(buf != 0); // use c assert to avoid a loop
247 // No need to save if the buffer has not changed.
251 string const doc = buf->isUnnamed()
252 ? OnlyFilename(buf->fileName()) : buf->fileName();
254 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
256 // We try to save three places:
257 // 1) Same place as document. Unless it is an unnamed doc.
258 if (!buf->isUnnamed()) {
259 string s = buf->fileName();
261 lyxerr << " " << s << endl;
262 if (buf->writeFile(s)) {
264 lyxerr << _(" Save seems successful. Phew.") << endl;
267 lyxerr << _(" Save failed! Trying...") << endl;
271 // 2) In HOME directory.
272 string s = AddName(GetEnvPath("HOME"), buf->fileName());
274 lyxerr << ' ' << s << endl;
275 if (buf->writeFile(s)) {
277 lyxerr << _(" Save seems successful. Phew.") << endl;
281 lyxerr << _(" Save failed! Trying...") << endl;
283 // 3) In "/tmp" directory.
284 // MakeAbsPath to prepend the current
285 // drive letter on OS/2
286 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
288 lyxerr << ' ' << s << endl;
289 if (buf->writeFile(s)) {
291 lyxerr << _(" Save seems successful. Phew.") << endl;
294 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
298 bool BufferList::exists(string const & s) const
300 return find_if(bstore.begin(), bstore.end(),
301 lyx::compare_memfun(&Buffer::fileName, s))
306 bool BufferList::isLoaded(Buffer const * b) const
309 BufferStorage::const_iterator cit =
310 find(bstore.begin(), bstore.end(), b);
311 return cit != bstore.end();
315 Buffer * BufferList::getBuffer(string const & s)
317 BufferStorage::iterator it =
318 find_if(bstore.begin(), bstore.end(),
319 lyx::compare_memfun(&Buffer::fileName, s));
320 return it != bstore.end() ? (*it) : 0;
324 Buffer * BufferList::getBufferFromTmp(string const & s)
326 BufferStorage::iterator it = bstore.begin();
327 BufferStorage::iterator end = bstore.end();
328 for (; it < end; ++it)
329 if (prefixIs(s, (*it)->temppath()))
335 void BufferList::setCurrentAuthor(string const & name, string const & email)
337 BufferStorage::iterator it = bstore.begin();
338 BufferStorage::iterator end = bstore.end();
339 for (; it != end; ++it) {
340 (*it)->params().authors().record(0, Author(name, email));