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/os.h"
32 #include <boost/bind.hpp>
37 using lyx::support::AddName;
38 using lyx::support::bformat;
39 using lyx::support::MakeAbsPath;
40 using lyx::support::MakeDisplayPath;
41 using lyx::support::OnlyFilename;
42 using lyx::support::removeAutosaveFile;
43 using lyx::support::prefixIs;
55 using std::back_inserter;
58 namespace os = lyx::support::os;
60 BufferList::BufferList()
64 bool BufferList::empty() const
66 return bstore.empty();
70 bool BufferList::quitWriteBuffer(Buffer * buf)
74 file = OnlyFilename(buf->fileName());
76 file = MakeDisplayPath(buf->fileName(), 30);
78 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
79 "Do you want to save the document or discard the changes?"), file);
80 int const ret = Alert::prompt(_("Save changed document?"),
81 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
84 // FIXME: WriteAs can be asynch !
85 // but not right now...maybe we should remove that
90 succeeded = WriteAs(buf);
92 succeeded = MenuWrite(buf);
96 } else if (ret == 1) {
97 // if we crash after this we could
98 // have no autosave file but I guess
99 // this is really inprobable (Jug)
100 if (buf->isUnnamed())
101 removeAutosaveFile(buf->fileName());
111 bool BufferList::quitWriteAll()
113 BufferStorage::iterator it = bstore.begin();
114 BufferStorage::iterator end = bstore.end();
115 for (; it != end; ++it) {
116 if ((*it)->isClean())
119 if (!quitWriteBuffer(*it))
127 void BufferList::release(Buffer * buf)
130 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
131 if (it != bstore.end()) {
132 Buffer * tmp = (*it);
139 Buffer * BufferList::newBuffer(string const & s, bool ronly)
141 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
142 tmpbuf->params().useClassDefaults();
143 lyxerr[Debug::INFO] << "Assigning to buffer "
144 << bstore.size() << endl;
145 bstore.push_back(tmpbuf.get());
146 return tmpbuf.release();
150 void BufferList::closeAll()
152 while (!bstore.empty()) {
153 close(bstore.front(), false);
158 bool BufferList::close(Buffer * buf, bool ask)
162 // FIXME: is the quitting check still necessary ?
163 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
169 if (buf->isUnnamed())
170 fname = OnlyFilename(buf->fileName());
172 fname = MakeDisplayPath(buf->fileName(), 30);
174 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
175 "Do you want to save the document or discard the changes?"), fname);
176 int const ret = Alert::prompt(_("Save changed document?"),
177 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
180 if (buf->isUnnamed()) {
183 } else if (buf->save()) {
184 LyX::ref().lastfiles().newFile(buf->fileName());
188 } else if (ret == 2) {
192 if (buf->isUnnamed()) {
193 removeAutosaveFile(buf->fileName());
201 vector<string> const BufferList::getFileNames() const
204 transform(bstore.begin(), bstore.end(),
206 boost::bind(&Buffer::fileName, _1));
211 Buffer * BufferList::first()
215 return bstore.front();
219 Buffer * BufferList::getBuffer(unsigned int choice)
221 if (choice >= bstore.size())
223 return bstore[choice];
227 Buffer * BufferList::next(Buffer const * buf) const
231 BufferStorage::const_iterator it = find(bstore.begin(),
233 BOOST_ASSERT(it != bstore.end());
235 if (it == bstore.end())
236 return bstore.front();
242 Buffer * BufferList::previous(Buffer const * buf) const
246 BufferStorage::const_iterator it = find(bstore.begin(),
248 BOOST_ASSERT(it != bstore.end());
249 if (it == bstore.begin())
250 return bstore.back();
256 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
257 OutputParams const & runparams)
259 BufferStorage::iterator it = bstore.begin();
260 BufferStorage::iterator end = bstore.end();
261 for (; it != end; ++it) {
262 if (!(*it)->isDepClean(mastertmpdir)) {
263 string writefile = mastertmpdir;
265 writefile += (*it)->getLatexName();
266 (*it)->makeLaTeXFile(writefile, mastertmpdir,
268 (*it)->markDepClean(mastertmpdir);
274 void BufferList::emergencyWriteAll()
276 for_each(bstore.begin(), bstore.end(),
277 bind(&BufferList::emergencyWrite, this, _1));
281 void BufferList::emergencyWrite(Buffer * buf)
283 // assert(buf) // this is not good since C assert takes an int
284 // and a pointer is a long (JMarc)
285 assert(buf != 0); // use c assert to avoid a loop
288 // No need to save if the buffer has not changed.
292 string const doc = buf->isUnnamed()
293 ? OnlyFilename(buf->fileName()) : buf->fileName();
295 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
297 // We try to save three places:
298 // 1) Same place as document. Unless it is an unnamed doc.
299 if (!buf->isUnnamed()) {
300 string s = buf->fileName();
302 lyxerr << " " << s << endl;
303 if (buf->writeFile(s)) {
305 lyxerr << _(" Save seems successful. Phew.") << endl;
308 lyxerr << _(" Save failed! Trying...") << endl;
312 // 2) In HOME directory.
313 string s = AddName(os::homepath(), buf->fileName());
315 lyxerr << ' ' << s << endl;
316 if (buf->writeFile(s)) {
318 lyxerr << _(" Save seems successful. Phew.") << endl;
322 lyxerr << _(" Save failed! Trying...") << endl;
324 // 3) In "/tmp" directory.
325 // MakeAbsPath to prepend the current
326 // drive letter on OS/2
327 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
329 lyxerr << ' ' << s << endl;
330 if (buf->writeFile(s)) {
332 lyxerr << _(" Save seems successful. Phew.") << endl;
335 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
339 bool BufferList::exists(string const & s) const
341 return find_if(bstore.begin(), bstore.end(),
342 bind(equal_to<string>(),
343 bind(&Buffer::fileName, _1),
349 bool BufferList::isLoaded(Buffer const * b) const
352 BufferStorage::const_iterator cit =
353 find(bstore.begin(), bstore.end(), b);
354 return cit != bstore.end();
358 Buffer * BufferList::getBuffer(string const & s)
360 BufferStorage::iterator it =
361 find_if(bstore.begin(), bstore.end(),
362 bind(equal_to<string>(),
363 bind(&Buffer::fileName, _1),
366 return it != bstore.end() ? (*it) : 0;
370 Buffer * BufferList::getBufferFromTmp(string const & s)
372 BufferStorage::iterator it = bstore.begin();
373 BufferStorage::iterator end = bstore.end();
374 for (; it < end; ++it)
375 if (prefixIs(s, (*it)->temppath()))
381 void BufferList::setCurrentAuthor(string const & name, string const & email)
383 BufferStorage::iterator it = bstore.begin();
384 BufferStorage::iterator end = bstore.end();
385 for (; it != end; ++it) {
386 (*it)->params().authors().record(0, Author(name, email));