]> git.lyx.org Git - lyx.git/blob - src/bufferlist.C
Fix a few issues with session/lastopenedfiles, Bo Peng (ben.bob@gmail.com)
[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::support::addName;
38 using lyx::support::bformat;
39 using lyx::support::makeAbsPath;
40 using lyx::support::makeDisplayPath;
41 using lyx::support::onlyFilename;
42 using lyx::support::removeAutosaveFile;
43 using lyx::support::package;
44 using lyx::support::prefixIs;
45
46 using boost::bind;
47
48 using std::auto_ptr;
49 using std::endl;
50 using std::equal_to;
51 using std::find;
52 using std::find_if;
53 using std::for_each;
54 using std::string;
55 using std::vector;
56 using std::back_inserter;
57 using std::transform;
58
59
60 BufferList::BufferList()
61 {}
62
63
64 bool BufferList::empty() const
65 {
66         return bstore.empty();
67 }
68
69
70 bool BufferList::quitWriteBuffer(Buffer * buf)
71 {
72         BOOST_ASSERT(buf);
73
74         string file;
75         if (buf->isUnnamed())
76                 file = onlyFilename(buf->fileName());
77         else
78                 file = makeDisplayPath(buf->fileName(), 30);
79
80         string const text =
81                 bformat(_("The document %1$s has unsaved changes.\n\n"
82                           "Do you want to save the document or discard the changes?"), file);
83         int const ret = Alert::prompt(_("Save changed document?"),
84                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
85
86         if (ret == 0) {
87                 // FIXME: WriteAs can be asynch !
88                 // but not right now...maybe we should remove that
89
90                 bool succeeded;
91
92                 if (buf->isUnnamed())
93                         succeeded = writeAs(buf);
94                 else
95                         succeeded = menuWrite(buf);
96
97                 if (!succeeded)
98                         return false;
99         } else if (ret == 1) {
100                 // if we crash after this we could
101                 // have no autosave file but I guess
102                 // this is really inprobable (Jug)
103                 if (buf->isUnnamed())
104                         removeAutosaveFile(buf->fileName());
105
106         } else {
107                 return false;
108         }
109
110         return true;
111 }
112
113
114 bool BufferList::quitWriteAll()
115 {
116         BufferStorage::iterator it = bstore.begin();
117         BufferStorage::iterator end = bstore.end();
118         for (; it != end; ++it) {
119                 if ((*it)->isClean())
120                         continue;
121
122                 if (!quitWriteBuffer(*it))
123                         return false;
124         }
125         // now, all buffers have been written sucessfully
126         // save file names to .lyx/session
127         it = bstore.begin();
128         for (; it != end; ++it) {
129                 // if master/slave are both open, do not save slave since it 
130                 // will be automatically loaded when the master is loaded
131                 if ((*it)->getMasterBuffer() == (*it))
132                         LyX::ref().session().addLastOpenedFile((*it)->fileName());
133         }
134
135         return true;
136 }
137
138
139 void BufferList::release(Buffer * buf)
140 {
141         BOOST_ASSERT(buf);
142         BufferStorage::iterator const it =
143                 find(bstore.begin(), bstore.end(), buf);
144         if (it != bstore.end()) {
145                 Buffer * tmp = (*it);
146                 BOOST_ASSERT(tmp);
147                 bstore.erase(it);
148                 delete tmp;
149         }
150 }
151
152
153 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
154 {
155         auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
156         tmpbuf->params().useClassDefaults();
157         lyxerr[Debug::INFO] << "Assigning to buffer "
158                             << bstore.size() << endl;
159         bstore.push_back(tmpbuf.get());
160         return tmpbuf.release();
161 }
162
163
164 void BufferList::closeAll()
165 {
166         while (!bstore.empty()) {
167                 close(bstore.front(), false);
168         }
169 }
170
171
172 bool BufferList::close(Buffer * buf, bool const ask)
173 {
174         BOOST_ASSERT(buf);
175
176         // FIXME: is the quitting check still necessary ?
177         if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
178                 release(buf);
179                 return true;
180         }
181
182         string fname;
183         if (buf->isUnnamed())
184                 fname = onlyFilename(buf->fileName());
185         else
186                 fname = makeDisplayPath(buf->fileName(), 30);
187
188         string const text =
189                 bformat(_("The document %1$s has unsaved changes.\n\n"
190                           "Do you want to save the document or discard the changes?"), fname);
191         int const ret = Alert::prompt(_("Save changed document?"),
192                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
193
194         if (ret == 0) {
195                 if (buf->isUnnamed()) {
196                         if (!writeAs(buf))
197                                 return false;
198                 } else if (buf->save()) {
199                         LyX::ref().session().addLastFile(buf->fileName());
200                 } else {
201                         return false;
202                 }
203         } else if (ret == 2) {
204                 return false;
205         }
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 << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
314
315         // We try to save three places:
316         // 1) Same place as document. Unless it is an unnamed doc.
317         if (!buf->isUnnamed()) {
318                 string s = buf->fileName();
319                 s += ".emergency";
320                 lyxerr << "  " << s << endl;
321                 if (buf->writeFile(s)) {
322                         buf->markClean();
323                         lyxerr << _("  Save seems successful. Phew.") << endl;
324                         return;
325                 } else {
326                         lyxerr << _("  Save failed! Trying...") << endl;
327                 }
328         }
329
330         // 2) In HOME directory.
331         string s = addName(package().home_dir(), buf->fileName());
332         s += ".emergency";
333         lyxerr << ' ' << s << endl;
334         if (buf->writeFile(s)) {
335                 buf->markClean();
336                 lyxerr << _("  Save seems successful. Phew.") << endl;
337                 return;
338         }
339
340         lyxerr << _("  Save failed! Trying...") << endl;
341
342         // 3) In "/tmp" directory.
343         // MakeAbsPath to prepend the current
344         // drive letter on OS/2
345         s = addName(package().temp_dir(), buf->fileName());
346         s += ".emergency";
347         lyxerr << ' ' << s << endl;
348         if (buf->writeFile(s)) {
349                 buf->markClean();
350                 lyxerr << _("  Save seems successful. Phew.") << endl;
351                 return;
352         }
353         lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
354 }
355
356
357 bool BufferList::exists(string const & s) const
358 {
359         return find_if(bstore.begin(), bstore.end(),
360                        bind(equal_to<string>(),
361                             bind(&Buffer::fileName, _1),
362                             s))
363                 != bstore.end();
364 }
365
366
367 bool BufferList::isLoaded(Buffer const * b) const
368 {
369         BOOST_ASSERT(b);
370         BufferStorage::const_iterator cit =
371                 find(bstore.begin(), bstore.end(), b);
372         return cit != bstore.end();
373 }
374
375
376 Buffer * BufferList::getBuffer(string const & s)
377 {
378         BufferStorage::iterator it =
379                 find_if(bstore.begin(), bstore.end(),
380                         bind(equal_to<string>(),
381                              bind(&Buffer::fileName, _1),
382                              s));
383
384         return it != bstore.end() ? (*it) : 0;
385 }
386
387
388 Buffer * BufferList::getBufferFromTmp(string const & s)
389 {
390         BufferStorage::iterator it = bstore.begin();
391         BufferStorage::iterator end = bstore.end();
392         for (; it < end; ++it)
393                 if (prefixIs(s, (*it)->temppath()))
394                         return *it;
395         return 0;
396 }
397
398
399 void BufferList::setCurrentAuthor(string const & name, string const & email)
400 {
401         BufferStorage::iterator it = bstore.begin();
402         BufferStorage::iterator end = bstore.end();
403         for (; it != end; ++it) {
404                 (*it)->params().authors().record(0, Author(name, email));
405         }
406 }