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;
40 using lyx::support::prefixIs;
50 BufferList::BufferList()
54 bool BufferList::empty() const
56 return bstore.empty();
60 bool BufferList::quitWriteBuffer(Buffer * buf)
64 file = OnlyFilename(buf->fileName());
66 file = MakeDisplayPath(buf->fileName(), 30);
68 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
69 "Do you want to save the document or discard the changes?"), file);
70 int const ret = Alert::prompt(_("Save changed document?"),
71 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
74 // FIXME: WriteAs can be asynch !
75 // but not right now...maybe we should remove that
80 succeeded = WriteAs(buf);
82 succeeded = MenuWrite(buf);
86 } else if (ret == 1) {
87 // if we crash after this we could
88 // have no autosave file but I guess
89 // this is really inprobable (Jug)
91 removeAutosaveFile(buf->fileName());
101 bool BufferList::quitWriteAll()
103 BufferStorage::iterator it = bstore.begin();
104 BufferStorage::iterator end = bstore.end();
105 for (; it != end; ++it) {
106 if ((*it)->isClean())
109 if (!quitWriteBuffer(*it))
117 void BufferList::release(Buffer * buf)
120 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
121 if (it != bstore.end()) {
122 // Make sure that we don't store a LyXText in
123 // the textcache that points to the buffer
125 Buffer * tmp = (*it);
127 textcache.removeAllWithBuffer(tmp);
133 Buffer * BufferList::newBuffer(string const & s, bool ronly)
135 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);
144 void BufferList::closeAll()
146 // Since we are closing we can just as well delete all
147 // in the textcache this will also speed the closing/quiting up a bit.
150 while (!bstore.empty()) {
151 close(bstore.front(), false);
156 bool BufferList::close(Buffer * buf, bool ask)
160 // FIXME: is the quitting check still necessary ?
161 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
167 if (buf->isUnnamed())
168 fname = OnlyFilename(buf->fileName());
170 fname = MakeDisplayPath(buf->fileName(), 30);
172 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
173 "Do you want to save the document or discard the changes?"), fname);
174 int const ret = Alert::prompt(_("Save changed document?"),
175 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
178 if (buf->isUnnamed()) {
181 } else if (buf->save()) {
182 LyX::ref().lastfiles().newFile(buf->fileName());
186 } else if (ret == 2) {
190 if (buf->isUnnamed()) {
191 removeAutosaveFile(buf->fileName());
199 vector<string> const BufferList::getFileNames() const
202 std::copy(bstore.begin(), bstore.end(),
203 lyx::back_inserter_fun(nvec, &Buffer::fileName));
208 Buffer * BufferList::first()
212 return bstore.front();
216 Buffer * BufferList::getBuffer(unsigned int choice)
218 if (choice >= bstore.size())
220 return bstore[choice];
224 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
225 LatexRunParams const & runparams)
227 BufferStorage::iterator it = bstore.begin();
228 BufferStorage::iterator end = bstore.end();
229 for (; it != end; ++it) {
230 if (!(*it)->isDepClean(mastertmpdir)) {
231 string writefile = mastertmpdir;
233 writefile += (*it)->getLatexName();
234 (*it)->makeLaTeXFile(writefile, mastertmpdir,
236 (*it)->markDepClean(mastertmpdir);
242 void BufferList::emergencyWriteAll()
244 for_each(bstore.begin(), bstore.end(),
245 boost::bind(&BufferList::emergencyWrite, this, _1));
249 void BufferList::emergencyWrite(Buffer * buf)
251 // assert(buf) // this is not good since C assert takes an int
252 // and a pointer is a long (JMarc)
253 assert(buf != 0); // use c assert to avoid a loop
256 // No need to save if the buffer has not changed.
260 string const doc = buf->isUnnamed()
261 ? OnlyFilename(buf->fileName()) : buf->fileName();
263 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
265 // We try to save three places:
266 // 1) Same place as document. Unless it is an unnamed doc.
267 if (!buf->isUnnamed()) {
268 string s = buf->fileName();
270 lyxerr << " " << s << endl;
271 if (buf->writeFile(s)) {
273 lyxerr << _(" Save seems successful. Phew.") << endl;
276 lyxerr << _(" Save failed! Trying...") << endl;
280 // 2) In HOME directory.
281 string s = AddName(GetEnvPath("HOME"), buf->fileName());
283 lyxerr << ' ' << s << endl;
284 if (buf->writeFile(s)) {
286 lyxerr << _(" Save seems successful. Phew.") << endl;
290 lyxerr << _(" Save failed! Trying...") << endl;
292 // 3) In "/tmp" directory.
293 // MakeAbsPath to prepend the current
294 // drive letter on OS/2
295 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
297 lyxerr << ' ' << s << endl;
298 if (buf->writeFile(s)) {
300 lyxerr << _(" Save seems successful. Phew.") << endl;
303 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
307 bool BufferList::exists(string const & s) const
309 return find_if(bstore.begin(), bstore.end(),
310 lyx::compare_memfun(&Buffer::fileName, s))
315 bool BufferList::isLoaded(Buffer const * b) const
318 BufferStorage::const_iterator cit =
319 find(bstore.begin(), bstore.end(), b);
320 return cit != bstore.end();
324 Buffer * BufferList::getBuffer(string const & s)
326 BufferStorage::iterator it =
327 find_if(bstore.begin(), bstore.end(),
328 lyx::compare_memfun(&Buffer::fileName, s));
329 return it != bstore.end() ? (*it) : 0;
333 Buffer * BufferList::getBufferFromTmp(string const & s)
335 BufferStorage::iterator it = bstore.begin();
336 BufferStorage::iterator end = bstore.end();
337 for (; it < end; ++it)
338 if (prefixIs(s, (*it)->temppath()))
344 void BufferList::setCurrentAuthor(string const & name, string const & email)
346 BufferStorage::iterator it = bstore.begin();
347 BufferStorage::iterator end = bstore.end();
348 for (; it != end; ++it) {
349 (*it)->params().authors().record(0, Author(name, email));