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