]> git.lyx.org Git - lyx.git/blob - src/bufferlist.C
implement buffer-next/previous (bug 515)
[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 #include "support/lyxfunctional.h"
31
32 #include <boost/bind.hpp>
33
34 using lyx::support::AddName;
35 using lyx::support::bformat;
36 using lyx::support::GetEnvPath;
37 using lyx::support::MakeAbsPath;
38 using lyx::support::MakeDisplayPath;
39 using lyx::support::OnlyFilename;
40 using lyx::support::removeAutosaveFile;
41 using lyx::support::prefixIs;
42
43 using boost::bind;
44
45 using std::auto_ptr;
46 using std::endl;
47 using std::find;
48 using std::find_if;
49 using std::for_each;
50 using std::string;
51 using std::vector;
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 or discard the changes?"), file);
74         int const ret = Alert::prompt(_("Save changed document?"),
75                 text, 0, 2, _("&Save"), _("&Discard"), _("&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         BOOST_ASSERT(buf);
124         BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
125         if (it != bstore.end()) {
126                 Buffer * tmp = (*it);
127                 bstore.erase(it);
128                 delete tmp;
129         }
130 }
131
132
133 Buffer * BufferList::newBuffer(string const & s, bool ronly)
134 {
135         auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
136         tmpbuf->params().useClassDefaults();
137         lyxerr[Debug::INFO] << "Assigning to buffer "
138                             << bstore.size() << endl;
139         bstore.push_back(tmpbuf.get());
140         return tmpbuf.release();
141 }
142
143
144 void BufferList::closeAll()
145 {
146         while (!bstore.empty()) {
147                 close(bstore.front(), false);
148         }
149 }
150
151
152 bool BufferList::close(Buffer * buf, bool ask)
153 {
154         BOOST_ASSERT(buf);
155
156         // FIXME: is the quitting check still necessary ?
157         if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
158                 release(buf);
159                 return true;
160         }
161
162         string fname;
163         if (buf->isUnnamed())
164                 fname = OnlyFilename(buf->fileName());
165         else
166                 fname = MakeDisplayPath(buf->fileName(), 30);
167
168         string text = bformat(_("The document %1$s has unsaved changes.\n\n"
169                 "Do you want to save the document or discard the changes?"), fname);
170         int const ret = Alert::prompt(_("Save changed document?"),
171                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
172
173         if (ret == 0) {
174                 if (buf->isUnnamed()) {
175                         if (!WriteAs(buf))
176                                 return false;
177                 } else if (buf->save()) {
178                         LyX::ref().lastfiles().newFile(buf->fileName());
179                 } else {
180                         return false;
181                 }
182         } else if (ret == 2) {
183                 return false;
184         }
185
186         if (buf->isUnnamed()) {
187                 removeAutosaveFile(buf->fileName());
188         }
189
190         release(buf);
191         return true;
192 }
193
194
195 vector<string> const BufferList::getFileNames() const
196 {
197         vector<string> nvec;
198         std::copy(bstore.begin(), bstore.end(),
199                   lyx::back_inserter_fun(nvec, &Buffer::fileName));
200         return nvec;
201 }
202
203
204 Buffer * BufferList::first()
205 {
206         if (bstore.empty())
207                 return 0;
208         return bstore.front();
209 }
210
211
212 Buffer * BufferList::getBuffer(unsigned int choice)
213 {
214         if (choice >= bstore.size())
215                 return 0;
216         return bstore[choice];
217 }
218
219
220 Buffer * BufferList::next(Buffer const * buf) const 
221 {
222         if (bstore.empty())
223                 return 0;
224         BufferStorage::const_iterator it = find(bstore.begin(), 
225                                                 bstore.end(), buf);
226         BOOST_ASSERT(it != bstore.end());
227         ++it;
228         if (it == bstore.end())
229                 return bstore.front();
230         else
231                 return *it;
232 }
233
234
235 Buffer * BufferList::previous(Buffer const * buf) const 
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         if (it == bstore.begin())
243                 return bstore.back();
244         else
245                 return *(it - 1);
246 }
247
248
249 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
250                                         OutputParams const & runparams)
251 {
252         BufferStorage::iterator it = bstore.begin();
253         BufferStorage::iterator end = bstore.end();
254         for (; it != end; ++it) {
255                 if (!(*it)->isDepClean(mastertmpdir)) {
256                         string writefile = mastertmpdir;
257                         writefile += '/';
258                         writefile += (*it)->getLatexName();
259                         (*it)->makeLaTeXFile(writefile, mastertmpdir,
260                                              runparams, false);
261                         (*it)->markDepClean(mastertmpdir);
262                 }
263         }
264 }
265
266
267 void BufferList::emergencyWriteAll()
268 {
269         for_each(bstore.begin(), bstore.end(),
270                  bind(&BufferList::emergencyWrite, this, _1));
271 }
272
273
274 void BufferList::emergencyWrite(Buffer * buf)
275 {
276         // assert(buf) // this is not good since C assert takes an int
277                        // and a pointer is a long (JMarc)
278         assert(buf != 0); // use c assert to avoid a loop
279
280
281         // No need to save if the buffer has not changed.
282         if (buf->isClean())
283                 return;
284
285         string const doc = buf->isUnnamed()
286                 ? OnlyFilename(buf->fileName()) : buf->fileName();
287
288         lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
289
290         // We try to save three places:
291         // 1) Same place as document. Unless it is an unnamed doc.
292         if (!buf->isUnnamed()) {
293                 string s = buf->fileName();
294                 s += ".emergency";
295                 lyxerr << "  " << s << endl;
296                 if (buf->writeFile(s)) {
297                         buf->markClean();
298                         lyxerr << _("  Save seems successful. Phew.") << endl;
299                         return;
300                 } else {
301                         lyxerr << _("  Save failed! Trying...") << endl;
302                 }
303         }
304
305         // 2) In HOME directory.
306         string s = AddName(GetEnvPath("HOME"), buf->fileName());
307         s += ".emergency";
308         lyxerr << ' ' << s << endl;
309         if (buf->writeFile(s)) {
310                 buf->markClean();
311                 lyxerr << _("  Save seems successful. Phew.") << endl;
312                 return;
313         }
314
315         lyxerr << _("  Save failed! Trying...") << endl;
316
317         // 3) In "/tmp" directory.
318         // MakeAbsPath to prepend the current
319         // drive letter on OS/2
320         s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
321         s += ".emergency";
322         lyxerr << ' ' << s << endl;
323         if (buf->writeFile(s)) {
324                 buf->markClean();
325                 lyxerr << _("  Save seems successful. Phew.") << endl;
326                 return;
327         }
328         lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
329 }
330
331
332 bool BufferList::exists(string const & s) const
333 {
334         return find_if(bstore.begin(), bstore.end(),
335                        lyx::compare_memfun(&Buffer::fileName, s))
336                 != bstore.end();
337 }
338
339
340 bool BufferList::isLoaded(Buffer const * b) const
341 {
342         BOOST_ASSERT(b);
343         BufferStorage::const_iterator cit =
344                 find(bstore.begin(), bstore.end(), b);
345         return cit != bstore.end();
346 }
347
348
349 Buffer * BufferList::getBuffer(string const & s)
350 {
351         BufferStorage::iterator it =
352                 find_if(bstore.begin(), bstore.end(),
353                         lyx::compare_memfun(&Buffer::fileName, s));
354         return it != bstore.end() ? (*it) : 0;
355 }
356
357
358 Buffer * BufferList::getBufferFromTmp(string const & s)
359 {
360         BufferStorage::iterator it = bstore.begin();
361         BufferStorage::iterator end = bstore.end();
362         for (; it < end; ++it)
363                 if (prefixIs(s, (*it)->temppath()))
364                         return *it;
365         return 0;
366 }
367
368
369 void BufferList::setCurrentAuthor(string const & name, string const & email)
370 {
371         BufferStorage::iterator it = bstore.begin();
372         BufferStorage::iterator end = bstore.end();
373         for (; it != end; ++it) {
374                 (*it)->params().authors().record(0, Author(name, email));
375         }
376 }