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"
25 #include "ParagraphList_fwd.h"
27 #include "frontends/Alert.h"
29 #include "support/filetools.h"
30 #include "support/lyxfunctional.h"
32 #include <boost/bind.hpp>
34 using lyx::support::AddName;
35 using lyx::support::bformat;
36 using lyx::support::GetEnvPath;
37 using lyx::support::MakeAbsPath;
38 using lyx::support::MakeDisplayPath;
39 using lyx::support::OnlyFilename;
40 using lyx::support::removeAutosaveFile;
41 using lyx::support::prefixIs;
54 BufferList::BufferList()
58 bool BufferList::empty() const
60 return bstore.empty();
64 bool BufferList::quitWriteBuffer(Buffer * buf)
68 file = OnlyFilename(buf->fileName());
70 file = MakeDisplayPath(buf->fileName(), 30);
72 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
73 "Do you want to save the document or discard the changes?"), file);
74 int const ret = Alert::prompt(_("Save changed document?"),
75 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
78 // FIXME: WriteAs can be asynch !
79 // but not right now...maybe we should remove that
84 succeeded = WriteAs(buf);
86 succeeded = MenuWrite(buf);
90 } else if (ret == 1) {
91 // if we crash after this we could
92 // have no autosave file but I guess
93 // this is really inprobable (Jug)
95 removeAutosaveFile(buf->fileName());
105 bool BufferList::quitWriteAll()
107 BufferStorage::iterator it = bstore.begin();
108 BufferStorage::iterator end = bstore.end();
109 for (; it != end; ++it) {
110 if ((*it)->isClean())
113 if (!quitWriteBuffer(*it))
121 void BufferList::release(Buffer * buf)
124 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
125 if (it != bstore.end()) {
126 Buffer * tmp = (*it);
133 Buffer * BufferList::newBuffer(string const & s, bool ronly)
135 auto_ptr<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.get());
140 return tmpbuf.release();
144 void BufferList::closeAll()
146 while (!bstore.empty()) {
147 close(bstore.front(), false);
152 bool BufferList::close(Buffer * buf, bool ask)
156 // FIXME: is the quitting check still necessary ?
157 if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
163 if (buf->isUnnamed())
164 fname = OnlyFilename(buf->fileName());
166 fname = MakeDisplayPath(buf->fileName(), 30);
168 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
169 "Do you want to save the document or discard the changes?"), fname);
170 int const ret = Alert::prompt(_("Save changed document?"),
171 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
174 if (buf->isUnnamed()) {
177 } else if (buf->save()) {
178 LyX::ref().lastfiles().newFile(buf->fileName());
182 } else if (ret == 2) {
186 if (buf->isUnnamed()) {
187 removeAutosaveFile(buf->fileName());
195 vector<string> const BufferList::getFileNames() const
198 std::copy(bstore.begin(), bstore.end(),
199 lyx::back_inserter_fun(nvec, &Buffer::fileName));
204 Buffer * BufferList::first()
208 return bstore.front();
212 Buffer * BufferList::getBuffer(unsigned int choice)
214 if (choice >= bstore.size())
216 return bstore[choice];
220 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
221 OutputParams const & runparams)
223 BufferStorage::iterator it = bstore.begin();
224 BufferStorage::iterator end = bstore.end();
225 for (; it != end; ++it) {
226 if (!(*it)->isDepClean(mastertmpdir)) {
227 string writefile = mastertmpdir;
229 writefile += (*it)->getLatexName();
230 (*it)->makeLaTeXFile(writefile, mastertmpdir,
232 (*it)->markDepClean(mastertmpdir);
238 void BufferList::emergencyWriteAll()
240 for_each(bstore.begin(), bstore.end(),
241 bind(&BufferList::emergencyWrite, this, _1));
245 void BufferList::emergencyWrite(Buffer * buf)
247 // assert(buf) // this is not good since C assert takes an int
248 // and a pointer is a long (JMarc)
249 assert(buf != 0); // use c assert to avoid a loop
252 // No need to save if the buffer has not changed.
256 string const doc = buf->isUnnamed()
257 ? OnlyFilename(buf->fileName()) : buf->fileName();
259 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
261 // We try to save three places:
262 // 1) Same place as document. Unless it is an unnamed doc.
263 if (!buf->isUnnamed()) {
264 string s = buf->fileName();
266 lyxerr << " " << s << endl;
267 if (buf->writeFile(s)) {
269 lyxerr << _(" Save seems successful. Phew.") << endl;
272 lyxerr << _(" Save failed! Trying...") << endl;
276 // 2) In HOME directory.
277 string s = AddName(GetEnvPath("HOME"), buf->fileName());
279 lyxerr << ' ' << s << endl;
280 if (buf->writeFile(s)) {
282 lyxerr << _(" Save seems successful. Phew.") << endl;
286 lyxerr << _(" Save failed! Trying...") << endl;
288 // 3) In "/tmp" directory.
289 // MakeAbsPath to prepend the current
290 // drive letter on OS/2
291 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
293 lyxerr << ' ' << s << endl;
294 if (buf->writeFile(s)) {
296 lyxerr << _(" Save seems successful. Phew.") << endl;
299 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
303 bool BufferList::exists(string const & s) const
305 return find_if(bstore.begin(), bstore.end(),
306 lyx::compare_memfun(&Buffer::fileName, s))
311 bool BufferList::isLoaded(Buffer const * b) const
314 BufferStorage::const_iterator cit =
315 find(bstore.begin(), bstore.end(), b);
316 return cit != bstore.end();
320 Buffer * BufferList::getBuffer(string const & s)
322 BufferStorage::iterator it =
323 find_if(bstore.begin(), bstore.end(),
324 lyx::compare_memfun(&Buffer::fileName, s));
325 return it != bstore.end() ? (*it) : 0;
329 Buffer * BufferList::getBufferFromTmp(string const & s)
331 BufferStorage::iterator it = bstore.begin();
332 BufferStorage::iterator end = bstore.end();
333 for (; it < end; ++it)
334 if (prefixIs(s, (*it)->temppath()))
340 void BufferList::setCurrentAuthor(string const & name, string const & email)
342 BufferStorage::iterator it = bstore.begin();
343 BufferStorage::iterator end = bstore.end();
344 for (; it != end; ++it) {
345 (*it)->params().authors().record(0, Author(name, email));