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::GetEnvPath;
40 using lyx::support::MakeAbsPath;
41 using lyx::support::MakeDisplayPath;
42 using lyx::support::OnlyFilename;
43 using lyx::support::removeAutosaveFile;
44 using lyx::support::prefixIs;
56 using std::back_inserter;
59 namespace os = lyx::support::os;
61 BufferList::BufferList()
65 bool BufferList::empty() const
67 return bstore.empty();
71 bool BufferList::quitWriteBuffer(Buffer * buf)
75 file = OnlyFilename(buf->fileName());
77 file = MakeDisplayPath(buf->fileName(), 30);
79 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
80 "Do you want to save the document or discard the changes?"), file);
81 int const ret = Alert::prompt(_("Save changed document?"),
82 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
85 // FIXME: WriteAs can be asynch !
86 // but not right now...maybe we should remove that
91 succeeded = WriteAs(buf);
93 succeeded = MenuWrite(buf);
97 } else if (ret == 1) {
98 // if we crash after this we could
99 // have no autosave file but I guess
100 // this is really inprobable (Jug)
101 if (buf->isUnnamed())
102 removeAutosaveFile(buf->fileName());
112 bool BufferList::quitWriteAll()
114 BufferStorage::iterator it = bstore.begin();
115 BufferStorage::iterator end = bstore.end();
116 for (; it != end; ++it) {
117 if ((*it)->isClean())
120 if (!quitWriteBuffer(*it))
128 void BufferList::release(Buffer * buf)
131 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
132 if (it != bstore.end()) {
133 Buffer * tmp = (*it);
140 Buffer * BufferList::newBuffer(string const & s, bool ronly)
142 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
143 tmpbuf->params().useClassDefaults();
144 lyxerr[Debug::INFO] << "Assigning to buffer "
145 << bstore.size() << endl;
146 bstore.push_back(tmpbuf.get());
147 return tmpbuf.release();
151 void BufferList::closeAll()
153 while (!bstore.empty()) {
154 close(bstore.front(), false);
159 bool BufferList::close(Buffer * buf, bool ask)
163 // FIXME: is the quitting check still necessary ?
164 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
170 if (buf->isUnnamed())
171 fname = OnlyFilename(buf->fileName());
173 fname = MakeDisplayPath(buf->fileName(), 30);
175 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
176 "Do you want to save the document or discard the changes?"), fname);
177 int const ret = Alert::prompt(_("Save changed document?"),
178 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
181 if (buf->isUnnamed()) {
184 } else if (buf->save()) {
185 LyX::ref().lastfiles().newFile(buf->fileName());
189 } else if (ret == 2) {
193 if (buf->isUnnamed()) {
194 removeAutosaveFile(buf->fileName());
202 vector<string> const BufferList::getFileNames() const
205 transform(bstore.begin(), bstore.end(),
207 boost::bind(&Buffer::fileName, _1));
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 Buffer * BufferList::next(Buffer const * buf) const
232 BufferStorage::const_iterator it = find(bstore.begin(),
234 BOOST_ASSERT(it != bstore.end());
236 if (it == bstore.end())
237 return bstore.front();
243 Buffer * BufferList::previous(Buffer const * buf) const
247 BufferStorage::const_iterator it = find(bstore.begin(),
249 BOOST_ASSERT(it != bstore.end());
250 if (it == bstore.begin())
251 return bstore.back();
257 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
258 OutputParams const & runparams)
260 BufferStorage::iterator it = bstore.begin();
261 BufferStorage::iterator end = bstore.end();
262 for (; it != end; ++it) {
263 if (!(*it)->isDepClean(mastertmpdir)) {
264 string writefile = mastertmpdir;
266 writefile += (*it)->getLatexName();
267 (*it)->makeLaTeXFile(writefile, mastertmpdir,
269 (*it)->markDepClean(mastertmpdir);
275 void BufferList::emergencyWriteAll()
277 for_each(bstore.begin(), bstore.end(),
278 bind(&BufferList::emergencyWrite, this, _1));
282 void BufferList::emergencyWrite(Buffer * buf)
284 // assert(buf) // this is not good since C assert takes an int
285 // and a pointer is a long (JMarc)
286 assert(buf != 0); // use c assert to avoid a loop
289 // No need to save if the buffer has not changed.
293 string const doc = buf->isUnnamed()
294 ? OnlyFilename(buf->fileName()) : buf->fileName();
296 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
298 // We try to save three places:
299 // 1) Same place as document. Unless it is an unnamed doc.
300 if (!buf->isUnnamed()) {
301 string s = buf->fileName();
303 lyxerr << " " << s << endl;
304 if (buf->writeFile(s)) {
306 lyxerr << _(" Save seems successful. Phew.") << endl;
309 lyxerr << _(" Save failed! Trying...") << endl;
313 // 2) In HOME directory.
314 string s = AddName(os::homepath(), buf->fileName());
316 lyxerr << ' ' << s << endl;
317 if (buf->writeFile(s)) {
319 lyxerr << _(" Save seems successful. Phew.") << endl;
323 lyxerr << _(" Save failed! Trying...") << endl;
325 // 3) In "/tmp" directory.
326 // MakeAbsPath to prepend the current
327 // drive letter on OS/2
328 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
330 lyxerr << ' ' << s << endl;
331 if (buf->writeFile(s)) {
333 lyxerr << _(" Save seems successful. Phew.") << endl;
336 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
340 bool BufferList::exists(string const & s) const
342 return find_if(bstore.begin(), bstore.end(),
343 bind(equal_to<string>(),
344 bind(&Buffer::fileName, _1),
350 bool BufferList::isLoaded(Buffer const * b) const
353 BufferStorage::const_iterator cit =
354 find(bstore.begin(), bstore.end(), b);
355 return cit != bstore.end();
359 Buffer * BufferList::getBuffer(string const & s)
361 BufferStorage::iterator it =
362 find_if(bstore.begin(), bstore.end(),
363 bind(equal_to<string>(),
364 bind(&Buffer::fileName, _1),
367 return it != bstore.end() ? (*it) : 0;
371 Buffer * BufferList::getBufferFromTmp(string const & s)
373 BufferStorage::iterator it = bstore.begin();
374 BufferStorage::iterator end = bstore.end();
375 for (; it < end; ++it)
376 if (prefixIs(s, (*it)->temppath()))
382 void BufferList::setCurrentAuthor(string const & name, string const & email)
384 BufferStorage::iterator it = bstore.begin();
385 BufferStorage::iterator end = bstore.end();
386 for (; it != end; ++it) {
387 (*it)->params().authors().record(0, Author(name, email));