]> git.lyx.org Git - lyx.git/blob - src/bufferlist.C
* filetools.[Ch]: Make functions that start with a capital
[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 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;
45
46 using boost::bind;
47
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;
58
59
60 BufferList::BufferList()
61 {}
62
63
64 bool BufferList::empty() const
65 {
66         return bstore.empty();
67 }
68
69
70 bool BufferList::quitWriteBuffer(Buffer * buf)
71 {
72         BOOST_ASSERT(buf);
73
74         string file;
75         if (buf->isUnnamed())
76                 file = onlyFilename(buf->fileName());
77         else
78                 file = makeDisplayPath(buf->fileName(), 30);
79
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"));
85
86         if (ret == 0) {
87                 // FIXME: WriteAs can be asynch !
88                 // but not right now...maybe we should remove that
89
90                 bool succeeded;
91
92                 if (buf->isUnnamed())
93                         succeeded = WriteAs(buf);
94                 else
95                         succeeded = MenuWrite(buf);
96
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());
105
106         } else {
107                 return false;
108         }
109
110         return true;
111 }
112
113
114 bool BufferList::quitWriteAll()
115 {
116         BufferStorage::iterator it = bstore.begin();
117         BufferStorage::iterator end = bstore.end();
118         for (; it != end; ++it) {
119                 if ((*it)->isClean())
120                         continue;
121
122                 if (!quitWriteBuffer(*it))
123                         return false;
124         }
125
126         return true;
127 }
128
129
130 void BufferList::release(Buffer * buf)
131 {
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         }
141 }
142
143
144 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
145 {
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();
152 }
153
154
155 void BufferList::closeAll()
156 {
157         while (!bstore.empty()) {
158                 close(bstore.front(), false);
159         }
160 }
161
162
163 bool BufferList::close(Buffer * buf, bool const ask)
164 {
165         BOOST_ASSERT(buf);
166
167         // FIXME: is the quitting check still necessary ?
168         if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
169                 release(buf);
170                 return true;
171         }
172
173         string fname;
174         if (buf->isUnnamed())
175                 fname = onlyFilename(buf->fileName());
176         else
177                 fname = makeDisplayPath(buf->fileName(), 30);
178
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"));
184
185         if (ret == 0) {
186                 if (buf->isUnnamed()) {
187                         if (!WriteAs(buf))
188                                 return false;
189                 } else if (buf->save()) {
190                         LyX::ref().session().addLastFile(buf->fileName());
191                 } else {
192                         return false;
193                 }
194         } else if (ret == 2) {
195                 return false;
196         }
197
198         if (buf->isUnnamed()) {
199                 removeAutosaveFile(buf->fileName());
200         }
201
202         release(buf);
203         return true;
204 }
205
206
207 vector<string> const BufferList::getFileNames() const
208 {
209         vector<string> nvec;
210         transform(bstore.begin(), bstore.end(),
211                   back_inserter(nvec),
212                   boost::bind(&Buffer::fileName, _1));
213         return nvec;
214 }
215
216
217 Buffer * BufferList::first()
218 {
219         if (bstore.empty())
220                 return 0;
221         return bstore.front();
222 }
223
224
225 Buffer * BufferList::getBuffer(unsigned int const choice)
226 {
227         if (choice >= bstore.size())
228                 return 0;
229         return bstore[choice];
230 }
231
232
233 Buffer * BufferList::next(Buffer const * buf) const
234 {
235         BOOST_ASSERT(buf);
236
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;
247 }
248
249
250 Buffer * BufferList::previous(Buffer const * buf) const
251 {
252         BOOST_ASSERT(buf);
253
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);
263 }
264
265
266 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
267                                         OutputParams const & runparams)
268 {
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         }
281 }
282
283
284 void BufferList::emergencyWriteAll()
285 {
286         for_each(bstore.begin(), bstore.end(),
287                  bind(&BufferList::emergencyWrite, this, _1));
288 }
289
290
291 void BufferList::emergencyWrite(Buffer * buf)
292 {
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);
296
297         // No need to save if the buffer has not changed.
298         if (buf->isClean())
299                 return;
300
301         string const doc = buf->isUnnamed()
302                 ? onlyFilename(buf->fileName()) : buf->fileName();
303
304         lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
305
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         }
320
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         }
330
331         lyxerr << _("  Save failed! Trying...") << endl;
332
333         // 3) In "/tmp" directory.
334         // MakeAbsPath to prepend the current
335         // drive letter on OS/2
336         s = addName(package().temp_dir(), 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;
345 }
346
347
348 bool BufferList::exists(string const & s) const
349 {
350         return find_if(bstore.begin(), bstore.end(),
351                        bind(equal_to<string>(),
352                             bind(&Buffer::fileName, _1),
353                             s))
354                 != bstore.end();
355 }
356
357
358 bool BufferList::isLoaded(Buffer const * b) const
359 {
360         BOOST_ASSERT(b);
361         BufferStorage::const_iterator cit =
362                 find(bstore.begin(), bstore.end(), b);
363         return cit != bstore.end();
364 }
365
366
367 Buffer * BufferList::getBuffer(string const & s)
368 {
369         BufferStorage::iterator it =
370                 find_if(bstore.begin(), bstore.end(),
371                         bind(equal_to<string>(),
372                              bind(&Buffer::fileName, _1),
373                              s));
374
375         return it != bstore.end() ? (*it) : 0;
376 }
377
378
379 Buffer * BufferList::getBufferFromTmp(string const & s)
380 {
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;
387 }
388
389
390 void BufferList::setCurrentAuthor(string const & name, string const & email)
391 {
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         }
397 }