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"
24 #include "TextCache.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;
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 // Make sure that we don't store a LyXText in
122 // the textcache that points to the buffer
124 Buffer * tmp = (*it);
126 textcache.removeAllWithBuffer(tmp);
132 Buffer * BufferList::newBuffer(string const & s, bool ronly)
134 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);
143 void BufferList::closeAll()
145 // Since we are closing we can just as well delete all
146 // in the textcache this will also speed the closing/quiting up a bit.
149 while (!bstore.empty()) {
150 close(bstore.front(), false);
155 bool BufferList::close(Buffer * buf, bool ask)
159 // FIXME: is the quitting check still necessary ?
160 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
166 if (buf->isUnnamed())
167 fname = OnlyFilename(buf->fileName());
169 fname = MakeDisplayPath(buf->fileName(), 30);
171 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
172 "Do you want to save the document or discard the changes?"), fname);
173 int const ret = Alert::prompt(_("Save changed document?"),
174 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
177 if (buf->isUnnamed()) {
180 } else if (buf->save()) {
181 lastfiles->newFile(buf->fileName());
185 } else if (ret == 2) {
189 if (buf->isUnnamed()) {
190 removeAutosaveFile(buf->fileName());
198 vector<string> const BufferList::getFileNames() const
201 std::copy(bstore.begin(), bstore.end(),
202 lyx::back_inserter_fun(nvec, &Buffer::fileName));
207 Buffer * BufferList::first()
211 return bstore.front();
215 Buffer * BufferList::getBuffer(unsigned int choice)
217 if (choice >= bstore.size())
219 return bstore[choice];
223 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
224 LatexRunParams const & runparams)
226 BufferStorage::iterator it = bstore.begin();
227 BufferStorage::iterator end = bstore.end();
228 for (; it != end; ++it) {
229 if (!(*it)->isDepClean(mastertmpdir)) {
230 string writefile = mastertmpdir;
232 writefile += (*it)->getLatexName();
233 (*it)->makeLaTeXFile(writefile, mastertmpdir,
235 (*it)->markDepClean(mastertmpdir);
241 void BufferList::emergencyWriteAll()
243 for_each(bstore.begin(), bstore.end(),
244 boost::bind(&BufferList::emergencyWrite, this, _1));
248 void BufferList::emergencyWrite(Buffer * buf)
250 // assert(buf) // this is not good since C assert takes an int
251 // and a pointer is a long (JMarc)
252 assert(buf != 0); // use c assert to avoid a loop
255 // No need to save if the buffer has not changed.
259 string const doc = buf->isUnnamed()
260 ? OnlyFilename(buf->fileName()) : buf->fileName();
262 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
264 // We try to save three places:
265 // 1) Same place as document. Unless it is an unnamed doc.
266 if (!buf->isUnnamed()) {
267 string s = buf->fileName();
269 lyxerr << " " << s << endl;
270 if (buf->writeFile(s)) {
272 lyxerr << _(" Save seems successful. Phew.") << endl;
275 lyxerr << _(" Save failed! Trying...") << endl;
279 // 2) In HOME directory.
280 string s = AddName(GetEnvPath("HOME"), buf->fileName());
282 lyxerr << ' ' << s << endl;
283 if (buf->writeFile(s)) {
285 lyxerr << _(" Save seems successful. Phew.") << endl;
289 lyxerr << _(" Save failed! Trying...") << endl;
291 // 3) In "/tmp" directory.
292 // MakeAbsPath to prepend the current
293 // drive letter on OS/2
294 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
296 lyxerr << ' ' << s << endl;
297 if (buf->writeFile(s)) {
299 lyxerr << _(" Save seems successful. Phew.") << endl;
302 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
306 bool BufferList::exists(string const & s) const
308 return find_if(bstore.begin(), bstore.end(),
309 lyx::compare_memfun(&Buffer::fileName, s))
314 bool BufferList::isLoaded(Buffer const * b) const
317 BufferStorage::const_iterator cit =
318 find(bstore.begin(), bstore.end(), b);
319 return cit != bstore.end();
323 Buffer * BufferList::getBuffer(string const & s)
325 BufferStorage::iterator it =
326 find_if(bstore.begin(), bstore.end(),
327 lyx::compare_memfun(&Buffer::fileName, s));
328 return it != bstore.end() ? (*it) : 0;
332 void BufferList::setCurrentAuthor(string const & name, string const & email)
334 BufferStorage::iterator it = bstore.begin();
335 BufferStorage::iterator end = bstore.end();
336 for (; it != end; ++it) {
337 (*it)->params().authors().record(0, Author(name, email));