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