Consider the case where there is not any layout name.
[lyx.git] / src / bufferlist.C
blob2ae3df66871419a7450d8f7a8e690c61e7c547fc
1 /**
2  * \file bufferlist.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
11 #include <config.h>
13 #include "bufferlist.h"
15 #include "author.h"
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "debug.h"
19 #include "gettext.h"
20 #include "lastfiles.h"
21 #include "lyx_cb.h"
22 #include "lyx_main.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/package.h"
32 #include <boost/bind.hpp>
34 #include <algorithm>
35 #include <functional>
37 using lyx::support::AddName;
38 using lyx::support::bformat;
39 using lyx::support::MakeAbsPath;
40 using lyx::support::MakeDisplayPath;
41 using lyx::support::OnlyFilename;
42 using lyx::support::removeAutosaveFile;
43 using lyx::support::package;
44 using lyx::support::prefixIs;
46 using boost::bind;
48 using std::auto_ptr;
49 using std::endl;
50 using std::equal_to;
51 using std::find;
52 using std::find_if;
53 using std::for_each;
54 using std::string;
55 using std::vector;
56 using std::back_inserter;
57 using std::transform;
60 BufferList::BufferList()
64 bool BufferList::empty() const
66         return bstore.empty();
70 bool BufferList::quitWriteBuffer(Buffer * buf)
72         BOOST_ASSERT(buf);
74         string file;
75         if (buf->isUnnamed())
76                 file = OnlyFilename(buf->fileName());
77         else
78                 file = MakeDisplayPath(buf->fileName(), 30);
80         string const text =
81                 bformat(_("The document %1$s has unsaved changes.\n\n"
82                           "Do you want to save the document or discard the changes?"), file);
83         int const ret = Alert::prompt(_("Save changed document?"),
84                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
86         if (ret == 0) {
87                 // FIXME: WriteAs can be asynch !
88                 // but not right now...maybe we should remove that
90                 bool succeeded;
92                 if (buf->isUnnamed())
93                         succeeded = WriteAs(buf);
94                 else
95                         succeeded = MenuWrite(buf);
97                 if (!succeeded)
98                         return false;
99         } else if (ret == 1) {
100                 // if we crash after this we could
101                 // have no autosave file but I guess
102                 // this is really inprobable (Jug)
103                 if (buf->isUnnamed())
104                         removeAutosaveFile(buf->fileName());
106         } else {
107                 return false;
108         }
110         return true;
114 bool BufferList::quitWriteAll()
116         BufferStorage::iterator it = bstore.begin();
117         BufferStorage::iterator end = bstore.end();
118         for (; it != end; ++it) {
119                 if ((*it)->isClean())
120                         continue;
122                 if (!quitWriteBuffer(*it))
123                         return false;
124         }
126         return true;
130 void BufferList::release(Buffer * buf)
132         BOOST_ASSERT(buf);
133         BufferStorage::iterator const it =
134                 find(bstore.begin(), bstore.end(), buf);
135         if (it != bstore.end()) {
136                 Buffer * tmp = (*it);
137                 BOOST_ASSERT(tmp);
138                 bstore.erase(it);
139                 delete tmp;
140         }
144 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
146         auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
147         tmpbuf->params().useClassDefaults();
148         lyxerr[Debug::INFO] << "Assigning to buffer "
149                             << bstore.size() << endl;
150         bstore.push_back(tmpbuf.get());
151         return tmpbuf.release();
155 void BufferList::closeAll()
157         while (!bstore.empty()) {
158                 close(bstore.front(), false);
159         }
163 bool BufferList::close(Buffer * buf, bool const ask)
165         BOOST_ASSERT(buf);
167         // FIXME: is the quitting check still necessary ?
168         if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
169                 release(buf);
170                 return true;
171         }
173         string fname;
174         if (buf->isUnnamed())
175                 fname = OnlyFilename(buf->fileName());
176         else
177                 fname = MakeDisplayPath(buf->fileName(), 30);
179         string const text =
180                 bformat(_("The document %1$s has unsaved changes.\n\n"
181                           "Do you want to save the document or discard the changes?"), fname);
182         int const ret = Alert::prompt(_("Save changed document?"),
183                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
185         if (ret == 0) {
186                 if (buf->isUnnamed()) {
187                         if (!WriteAs(buf))
188                                 return false;
189                 } else if (buf->save()) {
190                         LyX::ref().lastfiles().newFile(buf->fileName());
191                 } else {
192                         return false;
193                 }
194         } else if (ret == 2) {
195                 return false;
196         }
198         if (buf->isUnnamed()) {
199                 removeAutosaveFile(buf->fileName());
200         }
202         release(buf);
203         return true;
207 vector<string> const BufferList::getFileNames() const
209         vector<string> nvec;
210         transform(bstore.begin(), bstore.end(),
211                   back_inserter(nvec),
212                   boost::bind(&Buffer::fileName, _1));
213         return nvec;
217 Buffer * BufferList::first()
219         if (bstore.empty())
220                 return 0;
221         return bstore.front();
225 Buffer * BufferList::getBuffer(unsigned int const choice)
227         if (choice >= bstore.size())
228                 return 0;
229         return bstore[choice];
233 Buffer * BufferList::next(Buffer const * buf) const
235         BOOST_ASSERT(buf);
237         if (bstore.empty())
238                 return 0;
239         BufferStorage::const_iterator it = find(bstore.begin(),
240                                                 bstore.end(), buf);
241         BOOST_ASSERT(it != bstore.end());
242         ++it;
243         if (it == bstore.end())
244                 return bstore.front();
245         else
246                 return *it;
250 Buffer * BufferList::previous(Buffer const * buf) const
252         BOOST_ASSERT(buf);
254         if (bstore.empty())
255                 return 0;
256         BufferStorage::const_iterator it = find(bstore.begin(),
257                                                 bstore.end(), buf);
258         BOOST_ASSERT(it != bstore.end());
259         if (it == bstore.begin())
260                 return bstore.back();
261         else
262                 return *(it - 1);
266 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
267                                         OutputParams const & runparams)
269         BufferStorage::iterator it = bstore.begin();
270         BufferStorage::iterator end = bstore.end();
271         for (; it != end; ++it) {
272                 if (!(*it)->isDepClean(mastertmpdir)) {
273                         string writefile = mastertmpdir;
274                         writefile += '/';
275                         writefile += (*it)->getLatexName();
276                         (*it)->makeLaTeXFile(writefile, mastertmpdir,
277                                              runparams, false);
278                         (*it)->markDepClean(mastertmpdir);
279                 }
280         }
284 void BufferList::emergencyWriteAll()
286         for_each(bstore.begin(), bstore.end(),
287                  bind(&BufferList::emergencyWrite, this, _1));
291 void BufferList::emergencyWrite(Buffer * buf)
293         // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
294         // compare with 0 to avoid pointer/interger comparison
295         assert(buf != 0);
297         // No need to save if the buffer has not changed.
298         if (buf->isClean())
299                 return;
301         string const doc = buf->isUnnamed()
302                 ? OnlyFilename(buf->fileName()) : buf->fileName();
304         lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
306         // We try to save three places:
307         // 1) Same place as document. Unless it is an unnamed doc.
308         if (!buf->isUnnamed()) {
309                 string s = buf->fileName();
310                 s += ".emergency";
311                 lyxerr << "  " << s << endl;
312                 if (buf->writeFile(s)) {
313                         buf->markClean();
314                         lyxerr << _("  Save seems successful. Phew.") << endl;
315                         return;
316                 } else {
317                         lyxerr << _("  Save failed! Trying...") << endl;
318                 }
319         }
321         // 2) In HOME directory.
322         string s = AddName(package().home_dir(), buf->fileName());
323         s += ".emergency";
324         lyxerr << ' ' << s << endl;
325         if (buf->writeFile(s)) {
326                 buf->markClean();
327                 lyxerr << _("  Save seems successful. Phew.") << endl;
328                 return;
329         }
331         lyxerr << _("  Save failed! Trying...") << endl;
333         // 3) In "/tmp" directory.
334         // MakeAbsPath to prepend the current
335         // drive letter on OS/2
336         s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
337         s += ".emergency";
338         lyxerr << ' ' << s << endl;
339         if (buf->writeFile(s)) {
340                 buf->markClean();
341                 lyxerr << _("  Save seems successful. Phew.") << endl;
342                 return;
343         }
344         lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
348 bool BufferList::exists(string const & s) const
350         return find_if(bstore.begin(), bstore.end(),
351                        bind(equal_to<string>(),
352                             bind(&Buffer::fileName, _1),
353                             s))
354                 != bstore.end();
358 bool BufferList::isLoaded(Buffer const * b) const
360         BOOST_ASSERT(b);
361         BufferStorage::const_iterator cit =
362                 find(bstore.begin(), bstore.end(), b);
363         return cit != bstore.end();
367 Buffer * BufferList::getBuffer(string const & s)
369         BufferStorage::iterator it =
370                 find_if(bstore.begin(), bstore.end(),
371                         bind(equal_to<string>(),
372                              bind(&Buffer::fileName, _1),
373                              s));
375         return it != bstore.end() ? (*it) : 0;
379 Buffer * BufferList::getBufferFromTmp(string const & s)
381         BufferStorage::iterator it = bstore.begin();
382         BufferStorage::iterator end = bstore.end();
383         for (; it < end; ++it)
384                 if (prefixIs(s, (*it)->temppath()))
385                         return *it;
386         return 0;
390 void BufferList::setCurrentAuthor(string const & name, string const & email)
392         BufferStorage::iterator it = bstore.begin();
393         BufferStorage::iterator end = bstore.end();
394         for (; it != end; ++it) {
395                 (*it)->params().authors().record(0, Author(name, email));
396         }