]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
d983e2925c780a64b75ee02f3ea974b64b99aeb2
[lyx.git] / src / BufferList.cpp
1 /**
2  * \file BufferList.cpp
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 "Session.h"
19 #include "LyX.h"
20 #include "output_latex.h"
21 #include "ParagraphList.h"
22
23 #include "frontends/alert.h"
24
25 #include "support/ExceptionMessage.h"
26 #include "support/debug.h"
27 #include "support/FileName.h"
28 #include "support/FileNameList.h"
29 #include "support/filetools.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/os.h"
33 #include "support/Package.h"
34
35 #include "support/lassert.h"
36 #include <boost/bind.hpp>
37
38 #include <algorithm>
39 #include <functional>
40
41 using boost::bind;
42
43 using namespace std;
44 using namespace lyx::support;
45
46 namespace lyx {
47
48 namespace Alert = lyx::frontend::Alert;
49
50
51 BufferList::BufferList()
52 {}
53
54
55 BufferList::~BufferList()
56 {
57         BufferStorage::iterator it = binternal.begin();
58         BufferStorage::iterator end = binternal.end();
59         for (; it != end; ++it)
60                 delete (*it);
61 }
62
63
64 bool BufferList::empty() const
65 {
66         return bstore.empty();
67 }
68
69
70 BufferList::iterator BufferList::begin()
71 {
72         return bstore.begin();
73 }
74
75
76 BufferList::const_iterator BufferList::begin() const
77 {
78         return bstore.begin();
79 }
80
81
82 BufferList::iterator BufferList::end()
83 {
84         return bstore.end();
85 }
86
87
88 BufferList::const_iterator BufferList::end() const
89 {
90         return bstore.end();
91 }
92
93
94 void BufferList::release(Buffer * buf)
95 {
96         LASSERT(buf, /**/);
97         BufferStorage::iterator const it =
98                 find(bstore.begin(), bstore.end(), buf);
99         if (it != bstore.end()) {
100                 Buffer * tmp = (*it);
101                 LASSERT(tmp, /**/);
102                 bstore.erase(it);
103                 delete tmp;
104         }
105 }
106
107
108 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
109 {
110         auto_ptr<Buffer> tmpbuf;
111         try {
112                 tmpbuf.reset(new Buffer(s, ronly));
113         } catch (ExceptionMessage const & message) {
114                 if (message.type_ == ErrorException) {
115                         Alert::error(message.title_, message.details_);
116                         exit(1);
117                 } else if (message.type_ == WarningException) {
118                         Alert::warning(message.title_, message.details_);
119                         return 0;
120                 }
121         }
122         tmpbuf->params().useClassDefaults();
123         if (tmpbuf->fileName().extension() == "internal") {
124                 binternal.push_back(tmpbuf.get());
125         } else {
126                 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
127                 bstore.push_back(tmpbuf.get());
128         }
129         return tmpbuf.release();
130 }
131
132
133 void BufferList::closeAll()
134 {
135         while (!bstore.empty())
136                 release(bstore.front());
137 }
138
139
140 FileNameList const & BufferList::fileNames() const
141 {
142         static FileNameList nvec;
143         nvec.clear();
144         transform(bstore.begin(), bstore.end(),
145                   back_inserter(nvec),
146                   boost::bind(&Buffer::fileName, _1));
147         return nvec;
148 }
149
150
151 Buffer * BufferList::first()
152 {
153         if (bstore.empty())
154                 return 0;
155         return bstore.front();
156 }
157
158
159 Buffer * BufferList::last()
160 {
161         if (bstore.empty())
162                 return 0;
163         return bstore.back();
164 }
165
166
167 Buffer * BufferList::getBuffer(unsigned int choice)
168 {
169         if (choice >= bstore.size())
170                 return 0;
171         return bstore[choice];
172 }
173
174
175 Buffer * BufferList::next(Buffer const * buf) const
176 {
177         LASSERT(buf, /**/);
178
179         if (bstore.empty())
180                 return 0;
181         BufferStorage::const_iterator it = find(bstore.begin(),
182                                                 bstore.end(), buf);
183         LASSERT(it != bstore.end(), /**/);
184         ++it;
185         Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
186         return nextbuf;
187 }
188
189
190 Buffer * BufferList::previous(Buffer const * buf) const
191 {
192         LASSERT(buf, /**/);
193
194         if (bstore.empty())
195                 return 0;
196         BufferStorage::const_iterator it = find(bstore.begin(),
197                                                 bstore.end(), buf);
198         LASSERT(it != bstore.end(), /**/);
199
200         Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
201         return previousbuf;
202 }
203
204
205 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
206                                         OutputParams const & runparams)
207 {
208         BufferStorage::iterator it = bstore.begin();
209         BufferStorage::iterator end = bstore.end();
210         for (; it != end; ++it) {
211                 if (!(*it)->isDepClean(masterTmpDir)) {
212                         string writefile = addName(masterTmpDir, (*it)->latexName());
213                         (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
214                                              runparams, false);
215                         (*it)->markDepClean(masterTmpDir);
216                 }
217         }
218 }
219
220
221 void BufferList::emergencyWriteAll()
222 {
223         for_each(bstore.begin(), bstore.end(),
224                  bind(&BufferList::emergencyWrite, this, _1));
225 }
226
227
228 docstring BufferList::emergencyWrite(Buffer * buf)
229 {
230         // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
231         // compare with 0 to avoid pointer/interger comparison
232         // ::assert(buf != 0);
233         if (!buf)
234                 return _("No file open!");
235
236         // No need to save if the buffer has not changed.
237         if (buf->isClean())
238                 return docstring();
239
240         string const doc = buf->isUnnamed()
241                 ? onlyFilename(buf->absFileName()) : buf->absFileName();
242
243         docstring user_message = bformat(
244                 _("LyX: Attempting to save document %1$s\n"), from_utf8(doc));
245
246         // We try to save three places:
247         // 1) Same place as document. Unless it is an unnamed doc.
248         if (!buf->isUnnamed()) {
249                 string s = buf->absFileName();
250                 s += ".emergency";
251                 lyxerr << "  " << s << endl;
252                 if (buf->writeFile(FileName(s))) {
253                         buf->markClean();
254                         user_message += _("  Save seems successful. Phew.\n");
255                         return user_message;
256                 } else {
257                         user_message += _("  Save failed! Trying...\n");
258                 }
259         }
260
261         // 2) In HOME directory.
262         string s = addName(package().home_dir().absFilename(), buf->absFileName());
263         s += ".emergency";
264         lyxerr << ' ' << s << endl;
265         if (buf->writeFile(FileName(s))) {
266                 buf->markClean();
267                 user_message += _("  Save seems successful. Phew.\n");
268                 return user_message;
269         }
270
271         user_message += _("  Save failed! Trying...\n");
272
273         // 3) In "/tmp" directory.
274         // MakeAbsPath to prepend the current
275         // drive letter on OS/2
276         s = addName(package().temp_dir().absFilename(), buf->absFileName());
277         s += ".emergency";
278         lyxerr << ' ' << s << endl;
279         if (buf->writeFile(FileName(s))) {
280                 buf->markClean();
281                 user_message += _("  Save seems successful. Phew.\n");
282                 return user_message;
283         }
284
285         user_message += _("  Save failed! Bummer. Document is lost.");
286         return user_message;
287 }
288
289
290 bool BufferList::exists(FileName const & fname) const
291 {
292         return getBuffer(fname) != 0;
293 }
294
295
296 bool BufferList::isLoaded(Buffer const * b) const
297 {
298         BufferStorage::const_iterator cit =
299                 find(bstore.begin(), bstore.end(), b);
300         return cit != bstore.end();
301 }
302
303
304 namespace {
305 struct equivalent_to : public binary_function<FileName, FileName, bool>
306 {
307         bool operator()(FileName const & x, FileName const & y) const
308         { return equivalent(x, y); }
309 };
310 }
311
312
313 Buffer * BufferList::getBuffer(support::FileName const & fname) const
314 {
315         // 1) cheap test, using string comparison of file names
316         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
317                 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
318         if (it != bstore.end())
319                         return *it;
320         // 2) possibly expensive test, using quivalence test of file names
321         it = find_if(bstore.begin(), bstore.end(),
322                 bind(equivalent_to(), bind(&Buffer::fileName, _1), fname));
323         return it != bstore.end() ? (*it) : 0;
324 }
325
326
327 Buffer * BufferList::getBufferFromTmp(string const & s)
328 {
329         BufferStorage::iterator it = bstore.begin();
330         BufferStorage::iterator end = bstore.end();
331         for (; it < end; ++it)
332                 if (os::path_prefix_is(s, (*it)->temppath()))
333                         return *it;
334         return 0;
335 }
336
337
338 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
339 {
340         BufferStorage::iterator it = bstore.begin();
341         BufferStorage::iterator end = bstore.end();
342         for (; it != end; ++it)
343                 (*it)->params().authors().record(0, Author(name, email));
344 }
345
346
347 int BufferList::bufferNum(FileName const & fname) const
348 {
349         FileNameList const & buffers = fileNames();
350         FileNameList::const_iterator cit =
351                 find(buffers.begin(), buffers.end(), fname);
352         if (cit == buffers.end())
353                 return 0;
354         return int(cit - buffers.begin());
355 }
356
357
358 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
359 {
360         LASSERT(parent, return false);
361         LASSERT(child, return false);
362         LASSERT(parent->isChild(child), return false);
363
364         // Child document has a different parent, don't close it.
365         if (child->parent() != parent)
366                 return false;
367
368         BufferStorage::iterator it = bstore.begin();
369         BufferStorage::iterator end = bstore.end();
370         for (; it != end; ++it) {
371                 Buffer * buf = *it;
372                 if (buf != parent && buf->isChild(child)) {
373                         child->setParent(0);
374                         return false;
375                 }
376         }
377         release(child);
378         return true;
379 }
380
381
382 } // namespace lyx