]> git.lyx.org Git - lyx.git/blob - src/bufferlist.C
0ae822582e6718860291c1a7cacf929b2be2685a
[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 #include "lyx_main.h"
15 #include "lastfiles.h"
16 #include "buffer.h"
17 #include "debug.h"
18 #include "lyxrc.h"
19 #include "lyxtext.h"
20 #include "lyx_cb.h"
21 #include "bufferview_funcs.h"
22 #include "BufferView.h"
23 #include "gettext.h"
24 #include "frontends/LyXView.h"
25 #include "vc-backend.h"
26 #include "TextCache.h"
27 #include "lyxlex.h"
28
29 #include "frontends/Alert.h"
30
31 #include "support/FileInfo.h"
32 #include "support/filetools.h"
33 #include "support/lyxmanip.h"
34 #include "support/lyxfunctional.h"
35 #include "support/LAssert.h"
36
37 #include <boost/bind.hpp>
38
39 #include <cassert>
40 #include <algorithm>
41 #include <functional>
42
43
44 using namespace lyx::support;
45
46 using std::vector;
47 using std::find;
48 using std::endl;
49 using std::find_if;
50 using std::for_each;
51 using std::mem_fun;
52
53
54 BufferList::BufferList()
55 {}
56
57
58 bool BufferList::empty() const
59 {
60         return bstore.empty();
61 }
62
63
64 bool BufferList::quitWriteBuffer(Buffer * buf)
65 {
66         string file;
67         if (buf->isUnnamed())
68                 file = OnlyFilename(buf->fileName());
69         else
70                 file = MakeDisplayPath(buf->fileName(), 30);
71
72         string text = bformat(_("The document %1$s has unsaved changes.\n\n"
73                 "Do you want to save the document?"), file);
74         int const ret = Alert::prompt(_("Save changed document?"),
75                 text, 0, 2, _("&Save Changes"), _("&Discard Changes"), _("&Cancel"));
76
77         if (ret == 0) {
78                 // FIXME: WriteAs can be asynch !
79                 // but not right now...maybe we should remove that
80
81                 bool succeeded;
82
83                 if (buf->isUnnamed())
84                         succeeded = WriteAs(buf);
85                 else
86                         succeeded = MenuWrite(buf);
87
88                 if (!succeeded)
89                         return false;
90         } else if (ret == 1) {
91                 // if we crash after this we could
92                 // have no autosave file but I guess
93                 // this is really inprobable (Jug)
94                 if (buf->isUnnamed())
95                         removeAutosaveFile(buf->fileName());
96
97         } else {
98                 return false;
99         }
100
101         return true;
102 }
103
104
105 bool BufferList::quitWriteAll()
106 {
107         BufferStorage::iterator it = bstore.begin();
108         BufferStorage::iterator end = bstore.end();
109         for (; it != end; ++it) {
110                 if ((*it)->isClean())
111                         continue;
112
113                 if (!quitWriteBuffer(*it))
114                         return false;
115         }
116
117         return true;
118 }
119
120
121 void BufferList::release(Buffer * buf)
122 {
123         Assert(buf);
124         BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
125         if (it != bstore.end()) {
126                 // Make sure that we don't store a LyXText in
127                 // the textcache that points to the buffer
128                 // we just deleted.
129                 Buffer * tmp = (*it);
130                 bstore.erase(it);
131                 textcache.removeAllWithBuffer(tmp);
132                 delete tmp;
133         }
134 }
135
136
137 Buffer * BufferList::newBuffer(string const & s, bool ronly)
138 {
139         Buffer * tmpbuf = new Buffer(s, ronly);
140         tmpbuf->params.useClassDefaults();
141         lyxerr[Debug::INFO] << "Assigning to buffer "
142                             << bstore.size() << endl;
143         bstore.push_back(tmpbuf);
144         return tmpbuf;
145 }
146
147
148 void BufferList::closeAll()
149 {
150         // Since we are closing we can just as well delete all
151         // in the textcache this will also speed the closing/quiting up a bit.
152         textcache.clear();
153
154         while (!bstore.empty()) {
155                 close(bstore.front(), false);
156         }
157 }
158
159
160 bool BufferList::close(Buffer * buf, bool ask)
161 {
162         Assert(buf);
163
164         // FIXME: is the quitting check still necessary ?
165         if (!ask || buf->isClean() || quitting || buf->paragraphs.empty()) {
166                 release(buf);
167                 return true;
168         }
169
170         string fname;
171         if (buf->isUnnamed())
172                 fname = OnlyFilename(buf->fileName());
173         else
174                 fname = MakeDisplayPath(buf->fileName(), 30);
175
176         string text = bformat(_("The document %1$s has unsaved changes.\n\n"
177                 "Do you want to save the document?"), fname);
178         int const ret = Alert::prompt(_("Save changed document?"),
179                 text, 0, 2, _("&Save Changes"), _("&Discard Changes"), _("&Cancel"));
180
181         if (ret == 0) {
182                 if (buf->isUnnamed()) {
183                         if (!WriteAs(buf))
184                                 return false;
185                 } else if (buf->save()) {
186                         lastfiles->newFile(buf->fileName());
187                 } else {
188                         return false;
189                 }
190         } else if (ret == 2) {
191                 return false;
192         }
193
194         if (buf->isUnnamed()) {
195                 removeAutosaveFile(buf->fileName());
196         }
197
198         release(buf);
199         return true;
200 }
201
202
203 vector<string> const BufferList::getFileNames() const
204 {
205         vector<string> nvec;
206         std::copy(bstore.begin(), bstore.end(),
207                   lyx::back_inserter_fun(nvec, &Buffer::fileName));
208         return nvec;
209 }
210
211
212 Buffer * BufferList::first()
213 {
214         if (bstore.empty())
215                 return 0;
216         return bstore.front();
217 }
218
219
220 Buffer * BufferList::getBuffer(unsigned int choice)
221 {
222         if (choice >= bstore.size())
223                 return 0;
224         return bstore[choice];
225 }
226
227
228 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
229                                         LatexRunParams const & runparams)
230 {
231         BufferStorage::iterator it = bstore.begin();
232         BufferStorage::iterator end = bstore.end();
233         for (; it != end; ++it) {
234                 if (!(*it)->isDepClean(mastertmpdir)) {
235                         string writefile = mastertmpdir;
236                         writefile += '/';
237                         writefile += (*it)->getLatexName();
238                         (*it)->makeLaTeXFile(writefile, mastertmpdir,
239                                              runparams, true);
240                         (*it)->markDepClean(mastertmpdir);
241                 }
242         }
243 }
244
245
246 void BufferList::emergencyWriteAll()
247 {
248         for_each(bstore.begin(), bstore.end(),
249                  boost::bind(&BufferList::emergencyWrite, this, _1));
250 }
251
252
253 void BufferList::emergencyWrite(Buffer * buf)
254 {
255         // assert(buf) // this is not good since C assert takes an int
256                        // and a pointer is a long (JMarc)
257         assert(buf != 0); // use c assert to avoid a loop
258
259
260         // No need to save if the buffer has not changed.
261         if (buf->isClean())
262                 return;
263
264         string const doc = buf->isUnnamed()
265                 ? OnlyFilename(buf->fileName()) : buf->fileName();
266
267         lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
268
269         // We try to save three places:
270         // 1) Same place as document. Unless it is an unnamed doc.
271         if (!buf->isUnnamed()) {
272                 string s = buf->fileName();
273                 s += ".emergency";
274                 lyxerr << "  " << s << endl;
275                 if (buf->writeFile(s)) {
276                         buf->markClean();
277                         lyxerr << _("  Save seems successful. Phew.") << endl;
278                         return;
279                 } else {
280                         lyxerr << _("  Save failed! Trying...") << endl;
281                 }
282         }
283
284         // 2) In HOME directory.
285         string s = AddName(GetEnvPath("HOME"), buf->fileName());
286         s += ".emergency";
287         lyxerr << ' ' << s << endl;
288         if (buf->writeFile(s)) {
289                 buf->markClean();
290                 lyxerr << _("  Save seems successful. Phew.") << endl;
291                 return;
292         }
293
294         lyxerr << _("  Save failed! Trying...") << endl;
295
296         // 3) In "/tmp" directory.
297         // MakeAbsPath to prepend the current
298         // drive letter on OS/2
299         s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
300         s += ".emergency";
301         lyxerr << ' ' << s << endl;
302         if (buf->writeFile(s)) {
303                 buf->markClean();
304                 lyxerr << _("  Save seems successful. Phew.") << endl;
305                 return;
306         }
307         lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
308 }
309
310
311 bool BufferList::exists(string const & s) const
312 {
313         return find_if(bstore.begin(), bstore.end(),
314                        lyx::compare_memfun(&Buffer::fileName, s))
315                 != bstore.end();
316 }
317
318
319 bool BufferList::isLoaded(Buffer const * b) const
320 {
321         Assert(b);
322
323         BufferStorage::const_iterator cit =
324                 find(bstore.begin(), bstore.end(), b);
325         return cit != bstore.end();
326 }
327
328
329 Buffer * BufferList::getBuffer(string const & s)
330 {
331         BufferStorage::iterator it =
332                 find_if(bstore.begin(), bstore.end(),
333                         lyx::compare_memfun(&Buffer::fileName, s));
334         return it != bstore.end() ? (*it) : 0;
335 }
336
337
338 void BufferList::setCurrentAuthor(string const & name, string const & email)
339 {
340         BufferStorage::iterator it = bstore.begin();
341         BufferStorage::iterator end = bstore.end();
342         for (; it != end; ++it) {
343                 (*it)->authors().record(0, Author(name, email));
344         }
345 }