]> git.lyx.org Git - lyx.git/blob - src/bufferlist.C
* InsetMathNest.C (handleFont): avoid crash on undo when
[lyx.git] / src / bufferlist.C
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  */
10
11 #include <config.h>
12
13 #include "bufferlist.h"
14
15 #include "author.h"
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "debug.h"
19 #include "gettext.h"
20 #include "session.h"
21 #include "lyx_cb.h"
22 #include "lyx_main.h"
23 #include "output_latex.h"
24 #include "paragraph.h"
25 #include "ParagraphList.h"
26
27 #include "frontends/Alert.h"
28
29 #include "support/filetools.h"
30 #include "support/package.h"
31
32 #include <boost/bind.hpp>
33
34 #include <algorithm>
35 #include <functional>
36
37
38 namespace lyx {
39
40 using support::addName;
41 using support::bformat;
42 using support::FileName;
43 using support::makeDisplayPath;
44 using support::onlyFilename;
45 using support::removeAutosaveFile;
46 using support::package;
47 using support::prefixIs;
48
49 using boost::bind;
50
51 using std::auto_ptr;
52 using std::endl;
53 using std::equal_to;
54 using std::find;
55 using std::find_if;
56 using std::for_each;
57 using std::string;
58 using std::vector;
59 using std::back_inserter;
60 using std::transform;
61
62 namespace Alert = lyx::frontend::Alert;
63
64
65 BufferList::BufferList()
66 {}
67
68
69 bool BufferList::empty() const
70 {
71         return bstore.empty();
72 }
73
74
75 BufferList::iterator BufferList::begin()
76 {
77         return bstore.begin();
78 }
79
80
81 BufferList::const_iterator BufferList::begin() const
82 {
83         return bstore.begin();
84 }
85
86
87 BufferList::iterator BufferList::end()
88 {
89         return bstore.end();
90 }
91
92
93 BufferList::const_iterator BufferList::end() const
94 {
95         return bstore.end();
96 }
97
98
99 bool BufferList::quitWriteBuffer(Buffer * buf)
100 {
101         BOOST_ASSERT(buf);
102
103         docstring file;
104         if (buf->isUnnamed())
105                 file = from_utf8(onlyFilename(buf->fileName()));
106         else
107                 file = makeDisplayPath(buf->fileName(), 30);
108
109         docstring const text =
110                 bformat(_("The document %1$s has unsaved changes.\n\n"
111                                        "Do you want to save the document or discard the changes?"),
112                                            file);
113         int const ret = Alert::prompt(_("Save changed document?"),
114                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
115
116         if (ret == 0) {
117                 // FIXME: WriteAs can be asynch !
118                 // but not right now...maybe we should remove that
119
120                 bool succeeded;
121
122                 if (buf->isUnnamed())
123                         succeeded = writeAs(buf);
124                 else
125                         succeeded = menuWrite(buf);
126
127                 if (!succeeded)
128                         return false;
129         } else if (ret == 1) {
130                 // if we crash after this we could
131                 // have no autosave file but I guess
132                 // this is really inprobable (Jug)
133                 if (buf->isUnnamed())
134                         removeAutosaveFile(buf->fileName());
135
136         } else {
137                 return false;
138         }
139
140         return true;
141 }
142
143
144 bool BufferList::quitWriteAll()
145 {
146         BufferStorage::iterator it = bstore.begin();
147         BufferStorage::iterator end = bstore.end();
148         for (; it != end; ++it) {
149                 if ((*it)->isClean())
150                         continue;
151
152                 if (!quitWriteBuffer(*it))
153                         return false;
154         }
155         // now, all buffers have been written sucessfully
156         // save file names to .lyx/session
157         it = bstore.begin();
158         for (; it != end; ++it) {
159                 // if master/slave are both open, do not save slave since it
160                 // will be automatically loaded when the master is loaded
161                 if ((*it)->getMasterBuffer() == (*it))
162                         LyX::ref().session().lastOpened().add(FileName((*it)->fileName()));
163         }
164
165         return true;
166 }
167
168
169 void BufferList::release(Buffer * buf)
170 {
171         BOOST_ASSERT(buf);
172         BufferStorage::iterator const it =
173                 find(bstore.begin(), bstore.end(), buf);
174         if (it != bstore.end()) {
175                 Buffer * tmp = (*it);
176                 BOOST_ASSERT(tmp);
177                 bstore.erase(it);
178                 delete tmp;
179         }
180 }
181
182
183 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
184 {
185         auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
186         tmpbuf->params().useClassDefaults();
187         lyxerr[Debug::INFO] << "Assigning to buffer "
188                             << bstore.size() << endl;
189         bstore.push_back(tmpbuf.get());
190         return tmpbuf.release();
191 }
192
193
194 void BufferList::closeAll()
195 {
196         while (!bstore.empty()) {
197                 close(bstore.front(), false);
198         }
199 }
200
201
202 bool BufferList::close(Buffer * buf, bool const ask)
203 {
204         BOOST_ASSERT(buf);
205
206         if (!ask || buf->isClean() || buf->paragraphs().empty()) {
207                 release(buf);
208                 return true;
209         }
210
211         docstring fname;
212         if (buf->isUnnamed())
213                 fname = from_utf8(onlyFilename(buf->fileName()));
214         else
215                 fname = makeDisplayPath(buf->fileName(), 30);
216
217         docstring const text =
218                 bformat(_("The document %1$s has unsaved changes.\n\n"
219                                        "Do you want to save the document or discard the changes?"),
220                                            fname);
221         int const ret = Alert::prompt(_("Save changed document?"),
222                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
223
224         if (ret == 0) {
225                 if (buf->isUnnamed()) {
226                         if (!writeAs(buf))
227                                 return false;
228                 } else if (!menuWrite(buf))
229                         return false;
230                 else
231                         return false;
232         } else if (ret == 2)
233                 return false;
234
235         if (buf->isUnnamed()) {
236                 removeAutosaveFile(buf->fileName());
237         }
238
239         release(buf);
240         return true;
241 }
242
243
244 vector<string> const BufferList::getFileNames() const
245 {
246         vector<string> nvec;
247         transform(bstore.begin(), bstore.end(),
248                   back_inserter(nvec),
249                   boost::bind(&Buffer::fileName, _1));
250         return nvec;
251 }
252
253
254 Buffer * BufferList::first()
255 {
256         if (bstore.empty())
257                 return 0;
258         return bstore.front();
259 }
260
261
262 Buffer * BufferList::last()
263 {
264         if (bstore.empty())
265                 return 0;
266         return bstore.back();
267 }
268
269
270 Buffer * BufferList::getBuffer(unsigned int const choice)
271 {
272         if (choice >= bstore.size())
273                 return 0;
274         return bstore[choice];
275 }
276
277
278 Buffer * BufferList::next(Buffer const * buf) const
279 {
280         BOOST_ASSERT(buf);
281
282         if (bstore.empty())
283                 return 0;
284         BufferStorage::const_iterator it = find(bstore.begin(),
285                                                 bstore.end(), buf);
286         BOOST_ASSERT(it != bstore.end());
287         ++it;
288         if (it == bstore.end())
289                 return bstore.front();
290         else
291                 return *it;
292 }
293
294
295 Buffer * BufferList::previous(Buffer const * buf) const
296 {
297         BOOST_ASSERT(buf);
298
299         if (bstore.empty())
300                 return 0;
301         BufferStorage::const_iterator it = find(bstore.begin(),
302                                                 bstore.end(), buf);
303         BOOST_ASSERT(it != bstore.end());
304         if (it == bstore.begin())
305                 return bstore.back();
306         else
307                 return *(it - 1);
308 }
309
310
311 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
312                                         OutputParams const & runparams)
313 {
314         BufferStorage::iterator it = bstore.begin();
315         BufferStorage::iterator end = bstore.end();
316         for (; it != end; ++it) {
317                 if (!(*it)->isDepClean(mastertmpdir)) {
318                         string writefile = mastertmpdir;
319                         writefile += '/';
320                         writefile += (*it)->getLatexName();
321                         (*it)->makeLaTeXFile(FileName(writefile), mastertmpdir,
322                                              runparams, false);
323                         (*it)->markDepClean(mastertmpdir);
324                 }
325         }
326 }
327
328
329 void BufferList::emergencyWriteAll()
330 {
331         for_each(bstore.begin(), bstore.end(),
332                  bind(&BufferList::emergencyWrite, this, _1));
333 }
334
335
336 void BufferList::emergencyWrite(Buffer * buf)
337 {
338         // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
339         // compare with 0 to avoid pointer/interger comparison
340         assert(buf != 0);
341
342         // No need to save if the buffer has not changed.
343         if (buf->isClean())
344                 return;
345
346         string const doc = buf->isUnnamed()
347                 ? onlyFilename(buf->fileName()) : buf->fileName();
348
349         lyxerr << to_utf8(
350                 bformat(_("LyX: Attempting to save document %1$s"), from_utf8(doc)))
351                 << endl;
352
353         // We try to save three places:
354         // 1) Same place as document. Unless it is an unnamed doc.
355         if (!buf->isUnnamed()) {
356                 string s = buf->fileName();
357                 s += ".emergency";
358                 lyxerr << "  " << s << endl;
359                 if (buf->writeFile(FileName(s))) {
360                         buf->markClean();
361                         lyxerr << to_utf8(_("  Save seems successful. Phew.")) << endl;
362                         return;
363                 } else {
364                         lyxerr << to_utf8(_("  Save failed! Trying...")) << endl;
365                 }
366         }
367
368         // 2) In HOME directory.
369         string s = addName(package().home_dir(), buf->fileName());
370         s += ".emergency";
371         lyxerr << ' ' << s << endl;
372         if (buf->writeFile(FileName(s))) {
373                 buf->markClean();
374                 lyxerr << to_utf8(_("  Save seems successful. Phew.")) << endl;
375                 return;
376         }
377
378         lyxerr << to_utf8(_("  Save failed! Trying...")) << endl;
379
380         // 3) In "/tmp" directory.
381         // MakeAbsPath to prepend the current
382         // drive letter on OS/2
383         s = addName(package().temp_dir(), buf->fileName());
384         s += ".emergency";
385         lyxerr << ' ' << s << endl;
386         if (buf->writeFile(FileName(s))) {
387                 buf->markClean();
388                 lyxerr << to_utf8(_("  Save seems successful. Phew.")) << endl;
389                 return;
390         }
391         lyxerr << to_utf8(_("  Save failed! Bummer. Document is lost.")) << endl;
392 }
393
394
395 bool BufferList::exists(string const & s) const
396 {
397         return find_if(bstore.begin(), bstore.end(),
398                        bind(equal_to<string>(),
399                             bind(&Buffer::fileName, _1),
400                             s))
401                 != bstore.end();
402 }
403
404
405 bool BufferList::isLoaded(Buffer const * b) const
406 {
407         BOOST_ASSERT(b);
408         BufferStorage::const_iterator cit =
409                 find(bstore.begin(), bstore.end(), b);
410         return cit != bstore.end();
411 }
412
413
414 Buffer * BufferList::getBuffer(string const & s)
415 {
416         BufferStorage::iterator it =
417                 find_if(bstore.begin(), bstore.end(),
418                         bind(equal_to<string>(),
419                              bind(&Buffer::fileName, _1),
420                              s));
421
422         return it != bstore.end() ? (*it) : 0;
423 }
424
425
426 Buffer * BufferList::getBufferFromTmp(string const & s)
427 {
428         BufferStorage::iterator it = bstore.begin();
429         BufferStorage::iterator end = bstore.end();
430         for (; it < end; ++it)
431                 if (prefixIs(s, (*it)->temppath()))
432                         return *it;
433         return 0;
434 }
435
436
437 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
438 {
439         BufferStorage::iterator it = bstore.begin();
440         BufferStorage::iterator end = bstore.end();
441         for (; it != end; ++it) {
442                 (*it)->params().authors().record(0, Author(name, email));
443         }
444 }
445
446
447 } // namespace lyx