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 "output_latex.h"
24 #include "paragraph.h"
26 #include "frontends/Alert.h"
28 #include "support/filetools.h"
29 #include "support/lyxfunctional.h"
31 #include <boost/bind.hpp>
33 using lyx::support::AddName;
34 using lyx::support::bformat;
35 using lyx::support::GetEnvPath;
36 using lyx::support::MakeAbsPath;
37 using lyx::support::MakeDisplayPath;
38 using lyx::support::OnlyFilename;
39 using lyx::support::removeAutosaveFile;
40 using lyx::support::prefixIs;
53 BufferList::BufferList()
57 bool BufferList::empty() const
59 return bstore.empty();
63 bool BufferList::quitWriteBuffer(Buffer * buf)
67 file = OnlyFilename(buf->fileName());
69 file = MakeDisplayPath(buf->fileName(), 30);
71 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
72 "Do you want to save the document or discard the changes?"), file);
73 int const ret = Alert::prompt(_("Save changed document?"),
74 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
77 // FIXME: WriteAs can be asynch !
78 // but not right now...maybe we should remove that
83 succeeded = WriteAs(buf);
85 succeeded = MenuWrite(buf);
89 } else if (ret == 1) {
90 // if we crash after this we could
91 // have no autosave file but I guess
92 // this is really inprobable (Jug)
94 removeAutosaveFile(buf->fileName());
104 bool BufferList::quitWriteAll()
106 BufferStorage::iterator it = bstore.begin();
107 BufferStorage::iterator end = bstore.end();
108 for (; it != end; ++it) {
109 if ((*it)->isClean())
112 if (!quitWriteBuffer(*it))
120 void BufferList::release(Buffer * buf)
123 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
124 if (it != bstore.end()) {
125 Buffer * tmp = (*it);
132 Buffer * BufferList::newBuffer(string const & s, bool ronly)
134 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
135 tmpbuf->params().useClassDefaults();
136 lyxerr[Debug::INFO] << "Assigning to buffer "
137 << bstore.size() << endl;
138 bstore.push_back(tmpbuf.get());
139 return tmpbuf.release();
143 void BufferList::closeAll()
145 while (!bstore.empty()) {
146 close(bstore.front(), false);
151 bool BufferList::close(Buffer * buf, bool ask)
155 // FIXME: is the quitting check still necessary ?
156 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
162 if (buf->isUnnamed())
163 fname = OnlyFilename(buf->fileName());
165 fname = MakeDisplayPath(buf->fileName(), 30);
167 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
168 "Do you want to save the document or discard the changes?"), fname);
169 int const ret = Alert::prompt(_("Save changed document?"),
170 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
173 if (buf->isUnnamed()) {
176 } else if (buf->save()) {
177 LyX::ref().lastfiles().newFile(buf->fileName());
181 } else if (ret == 2) {
185 if (buf->isUnnamed()) {
186 removeAutosaveFile(buf->fileName());
194 vector<string> const BufferList::getFileNames() const
197 std::copy(bstore.begin(), bstore.end(),
198 lyx::back_inserter_fun(nvec, &Buffer::fileName));
203 Buffer * BufferList::first()
207 return bstore.front();
211 Buffer * BufferList::getBuffer(unsigned int choice)
213 if (choice >= bstore.size())
215 return bstore[choice];
219 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
220 OutputParams const & runparams)
222 BufferStorage::iterator it = bstore.begin();
223 BufferStorage::iterator end = bstore.end();
224 for (; it != end; ++it) {
225 if (!(*it)->isDepClean(mastertmpdir)) {
226 string writefile = mastertmpdir;
228 writefile += (*it)->getLatexName();
229 (*it)->makeLaTeXFile(writefile, mastertmpdir,
231 (*it)->markDepClean(mastertmpdir);
237 void BufferList::emergencyWriteAll()
239 for_each(bstore.begin(), bstore.end(),
240 bind(&BufferList::emergencyWrite, this, _1));
244 void BufferList::emergencyWrite(Buffer * buf)
246 // assert(buf) // this is not good since C assert takes an int
247 // and a pointer is a long (JMarc)
248 assert(buf != 0); // use c assert to avoid a loop
251 // No need to save if the buffer has not changed.
255 string const doc = buf->isUnnamed()
256 ? OnlyFilename(buf->fileName()) : buf->fileName();
258 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
260 // We try to save three places:
261 // 1) Same place as document. Unless it is an unnamed doc.
262 if (!buf->isUnnamed()) {
263 string s = buf->fileName();
265 lyxerr << " " << s << endl;
266 if (buf->writeFile(s)) {
268 lyxerr << _(" Save seems successful. Phew.") << endl;
271 lyxerr << _(" Save failed! Trying...") << endl;
275 // 2) In HOME directory.
276 string s = AddName(GetEnvPath("HOME"), buf->fileName());
278 lyxerr << ' ' << s << endl;
279 if (buf->writeFile(s)) {
281 lyxerr << _(" Save seems successful. Phew.") << endl;
285 lyxerr << _(" Save failed! Trying...") << endl;
287 // 3) In "/tmp" directory.
288 // MakeAbsPath to prepend the current
289 // drive letter on OS/2
290 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
292 lyxerr << ' ' << s << endl;
293 if (buf->writeFile(s)) {
295 lyxerr << _(" Save seems successful. Phew.") << endl;
298 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
302 bool BufferList::exists(string const & s) const
304 return find_if(bstore.begin(), bstore.end(),
305 lyx::compare_memfun(&Buffer::fileName, s))
310 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 Buffer * BufferList::getBufferFromTmp(string const & s)
330 BufferStorage::iterator it = bstore.begin();
331 BufferStorage::iterator end = bstore.end();
332 for (; it < end; ++it)
333 if (prefixIs(s, (*it)->temppath()))
339 void BufferList::setCurrentAuthor(string const & name, string const & email)
341 BufferStorage::iterator it = bstore.begin();
342 BufferStorage::iterator end = bstore.end();
343 for (; it != end; ++it) {
344 (*it)->params().authors().record(0, Author(name, email));