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"
25 #include "ParagraphList_fwd.h"
27 #include "frontends/Alert.h"
29 #include "support/filetools.h"
30 #include "support/lyxfunctional.h"
32 #include <boost/bind.hpp>
34 using lyx::support::AddName;
35 using lyx::support::bformat;
36 using lyx::support::GetEnvPath;
37 using lyx::support::MakeAbsPath;
38 using lyx::support::MakeDisplayPath;
39 using lyx::support::OnlyFilename;
40 using lyx::support::removeAutosaveFile;
41 using lyx::support::prefixIs;
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 Buffer * tmp = (*it);
133 Buffer * BufferList::newBuffer(string const & s, bool ronly)
135 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
136 tmpbuf->params().useClassDefaults();
137 lyxerr[Debug::INFO] << "Assigning to buffer "
138 << bstore.size() << endl;
139 bstore.push_back(tmpbuf.get());
140 return tmpbuf.release();
144 void BufferList::closeAll()
146 while (!bstore.empty()) {
147 close(bstore.front(), false);
152 bool BufferList::close(Buffer * buf, bool ask)
156 // FIXME: is the quitting check still necessary ?
157 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
163 if (buf->isUnnamed())
164 fname = OnlyFilename(buf->fileName());
166 fname = MakeDisplayPath(buf->fileName(), 30);
168 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
169 "Do you want to save the document or discard the changes?"), fname);
170 int const ret = Alert::prompt(_("Save changed document?"),
171 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
174 if (buf->isUnnamed()) {
177 } else if (buf->save()) {
178 LyX::ref().lastfiles().newFile(buf->fileName());
182 } else if (ret == 2) {
186 if (buf->isUnnamed()) {
187 removeAutosaveFile(buf->fileName());
195 vector<string> const BufferList::getFileNames() const
198 std::copy(bstore.begin(), bstore.end(),
199 lyx::back_inserter_fun(nvec, &Buffer::fileName));
204 Buffer * BufferList::first()
208 return bstore.front();
212 Buffer * BufferList::getBuffer(unsigned int choice)
214 if (choice >= bstore.size())
216 return bstore[choice];
220 Buffer * BufferList::next(Buffer const * buf) const
224 BufferStorage::const_iterator it = find(bstore.begin(),
226 BOOST_ASSERT(it != bstore.end());
228 if (it == bstore.end())
229 return bstore.front();
235 Buffer * BufferList::previous(Buffer const * buf) const
239 BufferStorage::const_iterator it = find(bstore.begin(),
241 BOOST_ASSERT(it != bstore.end());
242 if (it == bstore.begin())
243 return bstore.back();
249 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
250 OutputParams const & runparams)
252 BufferStorage::iterator it = bstore.begin();
253 BufferStorage::iterator end = bstore.end();
254 for (; it != end; ++it) {
255 if (!(*it)->isDepClean(mastertmpdir)) {
256 string writefile = mastertmpdir;
258 writefile += (*it)->getLatexName();
259 (*it)->makeLaTeXFile(writefile, mastertmpdir,
261 (*it)->markDepClean(mastertmpdir);
267 void BufferList::emergencyWriteAll()
269 for_each(bstore.begin(), bstore.end(),
270 bind(&BufferList::emergencyWrite, this, _1));
274 void BufferList::emergencyWrite(Buffer * buf)
276 // assert(buf) // this is not good since C assert takes an int
277 // and a pointer is a long (JMarc)
278 assert(buf != 0); // use c assert to avoid a loop
281 // No need to save if the buffer has not changed.
285 string const doc = buf->isUnnamed()
286 ? OnlyFilename(buf->fileName()) : buf->fileName();
288 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
290 // We try to save three places:
291 // 1) Same place as document. Unless it is an unnamed doc.
292 if (!buf->isUnnamed()) {
293 string s = buf->fileName();
295 lyxerr << " " << s << endl;
296 if (buf->writeFile(s)) {
298 lyxerr << _(" Save seems successful. Phew.") << endl;
301 lyxerr << _(" Save failed! Trying...") << endl;
305 // 2) In HOME directory.
306 string s = AddName(GetEnvPath("HOME"), buf->fileName());
308 lyxerr << ' ' << s << endl;
309 if (buf->writeFile(s)) {
311 lyxerr << _(" Save seems successful. Phew.") << endl;
315 lyxerr << _(" Save failed! Trying...") << endl;
317 // 3) In "/tmp" directory.
318 // MakeAbsPath to prepend the current
319 // drive letter on OS/2
320 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
322 lyxerr << ' ' << s << endl;
323 if (buf->writeFile(s)) {
325 lyxerr << _(" Save seems successful. Phew.") << endl;
328 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
332 bool BufferList::exists(string const & s) const
334 return find_if(bstore.begin(), bstore.end(),
335 lyx::compare_memfun(&Buffer::fileName, s))
340 bool BufferList::isLoaded(Buffer const * b) const
343 BufferStorage::const_iterator cit =
344 find(bstore.begin(), bstore.end(), b);
345 return cit != bstore.end();
349 Buffer * BufferList::getBuffer(string const & s)
351 BufferStorage::iterator it =
352 find_if(bstore.begin(), bstore.end(),
353 lyx::compare_memfun(&Buffer::fileName, s));
354 return it != bstore.end() ? (*it) : 0;
358 Buffer * BufferList::getBufferFromTmp(string const & s)
360 BufferStorage::iterator it = bstore.begin();
361 BufferStorage::iterator end = bstore.end();
362 for (; it < end; ++it)
363 if (prefixIs(s, (*it)->temppath()))
369 void BufferList::setCurrentAuthor(string const & name, string const & email)
371 BufferStorage::iterator it = bstore.begin();
372 BufferStorage::iterator end = bstore.end();
373 for (; it != end; ++it) {
374 (*it)->params().authors().record(0, Author(name, email));