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"
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;
51 BufferList::BufferList()
55 bool BufferList::empty() const
57 return bstore.empty();
61 bool BufferList::quitWriteBuffer(Buffer * buf)
65 file = OnlyFilename(buf->fileName());
67 file = MakeDisplayPath(buf->fileName(), 30);
69 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
70 "Do you want to save the document or discard the changes?"), file);
71 int const ret = Alert::prompt(_("Save changed document?"),
72 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
75 // FIXME: WriteAs can be asynch !
76 // but not right now...maybe we should remove that
81 succeeded = WriteAs(buf);
83 succeeded = MenuWrite(buf);
87 } else if (ret == 1) {
88 // if we crash after this we could
89 // have no autosave file but I guess
90 // this is really inprobable (Jug)
92 removeAutosaveFile(buf->fileName());
102 bool BufferList::quitWriteAll()
104 BufferStorage::iterator it = bstore.begin();
105 BufferStorage::iterator end = bstore.end();
106 for (; it != end; ++it) {
107 if ((*it)->isClean())
110 if (!quitWriteBuffer(*it))
118 void BufferList::release(Buffer * buf)
121 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
122 if (it != bstore.end()) {
123 Buffer * tmp = (*it);
130 Buffer * BufferList::newBuffer(string const & s, bool ronly)
132 auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
133 tmpbuf->params().useClassDefaults();
134 lyxerr[Debug::INFO] << "Assigning to buffer "
135 << bstore.size() << endl;
136 bstore.push_back(tmpbuf.get());
137 return tmpbuf.release();
141 void BufferList::closeAll()
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 LyX::ref().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 OutputParams 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 Buffer * BufferList::getBufferFromTmp(string const & s)
328 BufferStorage::iterator it = bstore.begin();
329 BufferStorage::iterator end = bstore.end();
330 for (; it < end; ++it)
331 if (prefixIs(s, (*it)->temppath()))
337 void BufferList::setCurrentAuthor(string const & name, string const & email)
339 BufferStorage::iterator it = bstore.begin();
340 BufferStorage::iterator end = bstore.end();
341 for (; it != end; ++it) {
342 (*it)->params().authors().record(0, Author(name, email));