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