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