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"
15 #include "lastfiles.h"
21 #include "bufferview_funcs.h"
22 #include "BufferView.h"
24 #include "frontends/LyXView.h"
25 #include "vc-backend.h"
26 #include "TextCache.h"
29 #include "frontends/Alert.h"
31 #include "support/FileInfo.h"
32 #include "support/filetools.h"
33 #include "support/lyxmanip.h"
34 #include "support/lyxfunctional.h"
35 #include "support/LAssert.h"
37 #include <boost/bind.hpp>
52 BufferList::BufferList()
56 bool BufferList::empty() const
58 return bstore.empty();
62 bool BufferList::quitWriteBuffer(Buffer * buf)
66 file = OnlyFilename(buf->fileName());
68 file = MakeDisplayPath(buf->fileName(), 30);
70 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
71 "Do you want to save the document?"), file);
72 int const ret = Alert::prompt(_("Save changed document?"),
73 text, 0, 2, _("&Save Changes"), _("&Discard Changes"), _("&Cancel"));
76 // FIXME: WriteAs can be asynch !
77 // but not right now...maybe we should remove that
82 succeeded = WriteAs(buf);
84 succeeded = MenuWrite(buf);
88 } else if (ret == 1) {
89 // if we crash after this we could
90 // have no autosave file but I guess
91 // this is really inprobable (Jug)
93 removeAutosaveFile(buf->fileName());
103 bool BufferList::quitWriteAll()
105 BufferStorage::iterator it = bstore.begin();
106 BufferStorage::iterator end = bstore.end();
107 for (; it != end; ++it) {
108 if ((*it)->isClean())
111 if (!quitWriteBuffer(*it))
119 void BufferList::release(Buffer * buf)
122 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
123 if (it != bstore.end()) {
124 // Make sure that we don't store a LyXText in
125 // the textcache that points to the buffer
127 Buffer * tmp = (*it);
129 textcache.removeAllWithBuffer(tmp);
135 Buffer * BufferList::newBuffer(string const & s, bool ronly)
137 Buffer * tmpbuf = new Buffer(s, ronly);
138 tmpbuf->params.useClassDefaults();
139 lyxerr[Debug::INFO] << "Assigning to buffer "
140 << bstore.size() << endl;
141 bstore.push_back(tmpbuf);
146 void BufferList::closeAll()
148 // Since we are closing we can just as well delete all
149 // in the textcache this will also speed the closing/quiting up a bit.
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?"), fname);
176 int const ret = Alert::prompt(_("Save changed document?"),
177 text, 0, 2, _("&Save Changes"), _("&Discard Changes"), _("&Cancel"));
180 if (buf->isUnnamed()) {
183 } else if (buf->save()) {
184 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 std::copy(bstore.begin(), bstore.end(),
205 lyx::back_inserter_fun(nvec, &Buffer::fileName));
210 Buffer * BufferList::first()
214 return bstore.front();
218 Buffer * BufferList::getBuffer(unsigned int choice)
220 if (choice >= bstore.size())
222 return bstore[choice];
226 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
227 LatexRunParams const & runparams)
229 BufferStorage::iterator it = bstore.begin();
230 BufferStorage::iterator end = bstore.end();
231 for (; it != end; ++it) {
232 if (!(*it)->isDepClean(mastertmpdir)) {
233 string writefile = mastertmpdir;
235 writefile += (*it)->getLatexName();
236 (*it)->makeLaTeXFile(writefile, mastertmpdir,
238 (*it)->markDepClean(mastertmpdir);
244 void BufferList::emergencyWriteAll()
246 for_each(bstore.begin(), bstore.end(),
247 boost::bind(&BufferList::emergencyWrite, this, _1));
251 void BufferList::emergencyWrite(Buffer * buf)
253 // assert(buf) // this is not good since C assert takes an int
254 // and a pointer is a long (JMarc)
255 assert(buf != 0); // use c assert to avoid a loop
258 // No need to save if the buffer has not changed.
262 string const doc = buf->isUnnamed()
263 ? OnlyFilename(buf->fileName()) : buf->fileName();
265 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
267 // We try to save three places:
268 // 1) Same place as document. Unless it is an unnamed doc.
269 if (!buf->isUnnamed()) {
270 string s = buf->fileName();
272 lyxerr << " " << s << endl;
273 if (buf->writeFile(s)) {
275 lyxerr << _(" Save seems successful. Phew.") << endl;
278 lyxerr << _(" Save failed! Trying...") << endl;
282 // 2) In HOME directory.
283 string s = AddName(GetEnvPath("HOME"), buf->fileName());
285 lyxerr << ' ' << s << endl;
286 if (buf->writeFile(s)) {
288 lyxerr << _(" Save seems successful. Phew.") << endl;
292 lyxerr << _(" Save failed! Trying...") << endl;
294 // 3) In "/tmp" directory.
295 // MakeAbsPath to prepend the current
296 // drive letter on OS/2
297 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
299 lyxerr << ' ' << s << endl;
300 if (buf->writeFile(s)) {
302 lyxerr << _(" Save seems successful. Phew.") << endl;
305 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
309 bool BufferList::exists(string const & s) const
311 return find_if(bstore.begin(), bstore.end(),
312 lyx::compare_memfun(&Buffer::fileName, s))
317 bool BufferList::isLoaded(Buffer const * b) const
321 BufferStorage::const_iterator cit =
322 find(bstore.begin(), bstore.end(), b);
323 return cit != bstore.end();
327 Buffer * BufferList::getBuffer(string const & s)
329 BufferStorage::iterator it =
330 find_if(bstore.begin(), bstore.end(),
331 lyx::compare_memfun(&Buffer::fileName, s));
332 return it != bstore.end() ? (*it) : 0;
336 void BufferList::setCurrentAuthor(string const & name, string const & email)
338 BufferStorage::iterator it = bstore.begin();
339 BufferStorage::iterator end = bstore.end();
340 for (; it != end; ++it) {
341 (*it)->authors().record(0, Author(name, email));