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 namespace lyx::support;
43 BufferList::BufferList()
47 bool BufferList::empty() const
49 return bstore.empty();
53 bool BufferList::quitWriteBuffer(Buffer * buf)
57 file = OnlyFilename(buf->fileName());
59 file = MakeDisplayPath(buf->fileName(), 30);
61 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
62 "Do you want to save the document or discard the changes?"), file);
63 int const ret = Alert::prompt(_("Save changed document?"),
64 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
67 // FIXME: WriteAs can be asynch !
68 // but not right now...maybe we should remove that
73 succeeded = WriteAs(buf);
75 succeeded = MenuWrite(buf);
79 } else if (ret == 1) {
80 // if we crash after this we could
81 // have no autosave file but I guess
82 // this is really inprobable (Jug)
84 removeAutosaveFile(buf->fileName());
94 bool BufferList::quitWriteAll()
96 BufferStorage::iterator it = bstore.begin();
97 BufferStorage::iterator end = bstore.end();
98 for (; it != end; ++it) {
102 if (!quitWriteBuffer(*it))
110 void BufferList::release(Buffer * buf)
113 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
114 if (it != bstore.end()) {
115 // Make sure that we don't store a LyXText in
116 // the textcache that points to the buffer
118 Buffer * tmp = (*it);
120 textcache.removeAllWithBuffer(tmp);
126 Buffer * BufferList::newBuffer(string const & s, bool ronly)
128 Buffer * tmpbuf = new Buffer(s, ronly);
129 tmpbuf->params().useClassDefaults();
130 lyxerr[Debug::INFO] << "Assigning to buffer "
131 << bstore.size() << endl;
132 bstore.push_back(tmpbuf);
137 void BufferList::closeAll()
139 // Since we are closing we can just as well delete all
140 // in the textcache this will also speed the closing/quiting up a bit.
143 while (!bstore.empty()) {
144 close(bstore.front(), false);
149 bool BufferList::close(Buffer * buf, bool ask)
153 // FIXME: is the quitting check still necessary ?
154 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
160 if (buf->isUnnamed())
161 fname = OnlyFilename(buf->fileName());
163 fname = MakeDisplayPath(buf->fileName(), 30);
165 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
166 "Do you want to save the document or discard the changes?"), fname);
167 int const ret = Alert::prompt(_("Save changed document?"),
168 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
171 if (buf->isUnnamed()) {
174 } else if (buf->save()) {
175 lastfiles->newFile(buf->fileName());
179 } else if (ret == 2) {
183 if (buf->isUnnamed()) {
184 removeAutosaveFile(buf->fileName());
192 vector<string> const BufferList::getFileNames() const
195 std::copy(bstore.begin(), bstore.end(),
196 lyx::back_inserter_fun(nvec, &Buffer::fileName));
201 Buffer * BufferList::first()
205 return bstore.front();
209 Buffer * BufferList::getBuffer(unsigned int choice)
211 if (choice >= bstore.size())
213 return bstore[choice];
217 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
218 LatexRunParams const & runparams)
220 BufferStorage::iterator it = bstore.begin();
221 BufferStorage::iterator end = bstore.end();
222 for (; it != end; ++it) {
223 if (!(*it)->isDepClean(mastertmpdir)) {
224 string writefile = mastertmpdir;
226 writefile += (*it)->getLatexName();
227 (*it)->makeLaTeXFile(writefile, mastertmpdir,
229 (*it)->markDepClean(mastertmpdir);
235 void BufferList::emergencyWriteAll()
237 for_each(bstore.begin(), bstore.end(),
238 boost::bind(&BufferList::emergencyWrite, this, _1));
242 void BufferList::emergencyWrite(Buffer * buf)
244 // assert(buf) // this is not good since C assert takes an int
245 // and a pointer is a long (JMarc)
246 assert(buf != 0); // use c assert to avoid a loop
249 // No need to save if the buffer has not changed.
253 string const doc = buf->isUnnamed()
254 ? OnlyFilename(buf->fileName()) : buf->fileName();
256 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
258 // We try to save three places:
259 // 1) Same place as document. Unless it is an unnamed doc.
260 if (!buf->isUnnamed()) {
261 string s = buf->fileName();
263 lyxerr << " " << s << endl;
264 if (buf->writeFile(s)) {
266 lyxerr << _(" Save seems successful. Phew.") << endl;
269 lyxerr << _(" Save failed! Trying...") << endl;
273 // 2) In HOME directory.
274 string s = AddName(GetEnvPath("HOME"), buf->fileName());
276 lyxerr << ' ' << s << endl;
277 if (buf->writeFile(s)) {
279 lyxerr << _(" Save seems successful. Phew.") << endl;
283 lyxerr << _(" Save failed! Trying...") << endl;
285 // 3) In "/tmp" directory.
286 // MakeAbsPath to prepend the current
287 // drive letter on OS/2
288 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
290 lyxerr << ' ' << s << endl;
291 if (buf->writeFile(s)) {
293 lyxerr << _(" Save seems successful. Phew.") << endl;
296 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
300 bool BufferList::exists(string const & s) const
302 return find_if(bstore.begin(), bstore.end(),
303 lyx::compare_memfun(&Buffer::fileName, s))
308 bool BufferList::isLoaded(Buffer const * b) const
311 BufferStorage::const_iterator cit =
312 find(bstore.begin(), bstore.end(), b);
313 return cit != bstore.end();
317 Buffer * BufferList::getBuffer(string const & s)
319 BufferStorage::iterator it =
320 find_if(bstore.begin(), bstore.end(),
321 lyx::compare_memfun(&Buffer::fileName, s));
322 return it != bstore.end() ? (*it) : 0;
326 void BufferList::setCurrentAuthor(string const & name, string const & email)
328 BufferStorage::iterator it = bstore.begin();
329 BufferStorage::iterator end = bstore.end();
330 for (; it != end; ++it) {
331 (*it)->params().authors().record(0, Author(name, email));