]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Update bindfiles to latest fileformat
[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/Package.h"
33
34 #include "support/lassert.h"
35 #include "support/bind.h"
36
37 #include <algorithm>
38 #include <functional>
39 #include <iterator>
40 #include <memory>
41
42 using namespace std;
43 using namespace lyx::support;
44
45 namespace lyx {
46
47 namespace Alert = lyx::frontend::Alert;
48
49
50 BufferList::BufferList()
51 {}
52
53
54 BufferList::~BufferList()
55 {
56         BufferStorage::iterator it = binternal.begin();
57         BufferStorage::iterator end = binternal.end();
58         for (; it != end; ++it)
59                 delete (*it);
60 }
61
62
63 bool BufferList::empty() const
64 {
65         return bstore.empty();
66 }
67
68
69 BufferList::iterator BufferList::begin()
70 {
71         return bstore.begin();
72 }
73
74
75 BufferList::const_iterator BufferList::begin() const
76 {
77         return bstore.begin();
78 }
79
80
81 BufferList::iterator BufferList::end()
82 {
83         return bstore.end();
84 }
85
86
87 BufferList::const_iterator BufferList::end() const
88 {
89         return bstore.end();
90 }
91
92
93 void BufferList::release(Buffer * buf)
94 {
95         // We may leak here, but we probably do not need to
96         // shut down.
97         LASSERT(buf, return);
98         BufferStorage::iterator const it =
99                 find(bstore.begin(), bstore.end(), buf);
100         if (it != bstore.end()) {
101                 Buffer * tmp = (*it);
102                 bstore.erase(it);
103                 LASSERT(tmp, return);
104                 delete tmp;
105         }
106 }
107
108
109 Buffer * BufferList::newInternalBuffer(string const & s)
110 {
111         Buffer * const buf = createNewBuffer(s);
112         if (buf) {
113                 buf->setInternal(true);
114                 binternal.push_back(buf);
115         }
116         return buf;
117 }
118
119
120 Buffer * BufferList::newBuffer(string const & s)
121 {
122         Buffer * const buf = createNewBuffer(s);
123         if (buf) {
124                 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
125                 bstore.push_back(buf);
126         }
127         return buf;
128 }
129
130
131 Buffer * BufferList::createNewBuffer(string const & s)
132 {
133         auto_ptr<Buffer> tmpbuf;
134         try {
135                 tmpbuf.reset(new Buffer(s));
136         } catch (ExceptionMessage const & message) {
137                 if (message.type_ == ErrorException) {
138                         Alert::error(message.title_, message.details_);
139                         exit(1);
140                 } else if (message.type_ == WarningException) {
141                         Alert::warning(message.title_, message.details_);
142                         return 0;
143                 }
144         }
145         tmpbuf->params().useClassDefaults();
146         return tmpbuf.release();
147 }
148
149
150 void BufferList::closeAll()
151 {
152         while (!bstore.empty())
153                 release(bstore.front());
154 }
155
156
157 FileNameList const & BufferList::fileNames() const
158 {
159         static FileNameList nvec;
160         nvec.clear();
161         BufferStorage::const_iterator it = bstore.begin();
162         BufferStorage::const_iterator end = bstore.end();
163         for (; it != end; ++it) {
164                 Buffer * buf = *it;
165                 nvec.push_back(buf->fileName());
166         }
167         return nvec;
168 }
169
170
171 Buffer * BufferList::first()
172 {
173         if (bstore.empty())
174                 return 0;
175         return bstore.front();
176 }
177
178
179 Buffer * BufferList::last()
180 {
181         if (bstore.empty())
182                 return 0;
183         return bstore.back();
184 }
185
186
187 Buffer * BufferList::getBuffer(unsigned int choice)
188 {
189         if (choice >= bstore.size())
190                 return 0;
191         return bstore[choice];
192 }
193
194
195 Buffer * BufferList::next(Buffer const * buf) const
196 {
197         // Something is wrong, but we can probably survive it.
198         LASSERT(buf, return 0);
199
200         if (bstore.empty())
201                 return 0;
202         BufferStorage::const_iterator it = 
203                         find(bstore.begin(), bstore.end(), buf);
204         LASSERT(it != bstore.end(), return 0);
205         ++it;
206         Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
207         return nextbuf;
208 }
209
210
211 Buffer * BufferList::previous(Buffer const * buf) const
212 {
213         // Something is wrong, but we can probably survive it.
214         LASSERT(buf, return 0);
215
216         if (bstore.empty())
217                 return 0;
218         BufferStorage::const_iterator it = 
219                         find(bstore.begin(), bstore.end(), buf);
220         LASSERT(it != bstore.end(), return 0);
221
222         Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
223         return previousbuf;
224 }
225
226
227 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
228                                         OutputParams const & runparams_in)
229 {
230         OutputParams runparams = runparams_in;
231         runparams.is_child = true;
232         BufferStorage::iterator it = bstore.begin();
233         BufferStorage::iterator end = bstore.end();
234         for (; it != end; ++it) {
235                 if (!(*it)->isDepClean(masterTmpDir)) {
236                         string writefile = addName(masterTmpDir, (*it)->latexName());
237                         (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
238                                              runparams, Buffer::OnlyBody);
239                         (*it)->markDepClean(masterTmpDir);
240                 }
241         }
242         runparams.is_child = false;
243 }
244
245
246 void BufferList::emergencyWriteAll()
247 {
248         BufferStorage::const_iterator it = bstore.begin();
249         BufferStorage::const_iterator const en = bstore.end();
250         for (; it != en; ++it)
251                  (*it)->emergencyWrite();
252 }
253
254
255 bool BufferList::exists(FileName const & fname) const
256 {
257         return getBuffer(fname) != 0;
258 }
259
260
261  bool BufferList::isLoaded(Buffer const * b) const
262 {
263         if (!b)
264                 return false;
265         BufferStorage::const_iterator cit =
266                 find(bstore.begin(), bstore.end(), b);
267         return cit != bstore.end();
268 }
269
270
271 namespace {
272
273 struct equivalent_to : public binary_function<FileName, FileName, bool>
274 {
275         bool operator()(FileName const & x, FileName const & y) const
276         { return equivalent(x, y); }
277 };
278
279 }
280
281
282 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
283 {
284         // 1) cheap test, using string comparison of file names
285         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
286                 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
287         if (it != bstore.end())
288                 return *it;
289         // 2) possibly expensive test, using equivalence test of file names
290         it = find_if(bstore.begin(), bstore.end(),
291                 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
292         if (it != bstore.end())
293                 return *it;
294
295         if (internal) {
296                 // 1) cheap test, using string comparison of file names
297                 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
298                         lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
299                 if (it != binternal.end())
300                         return *it;
301                 // 2) possibly expensive test, using equivalence test of file names
302                 it = find_if(binternal.begin(), binternal.end(),
303                              lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
304                 if (it != binternal.end())
305                         return *it;
306         }
307
308         return 0;
309 }
310
311
312 Buffer * BufferList::getBufferFromTmp(string const & s)
313 {
314         BufferStorage::iterator it = bstore.begin();
315         BufferStorage::iterator end = bstore.end();
316         for (; it < end; ++it) {
317                 if (prefixIs(s, (*it)->temppath())) {
318                         // check whether the filename matches the master
319                         string const master_name = (*it)->latexName();
320                         if (suffixIs(s, master_name))
321                                 return *it;
322                         // if not, try with the children
323                         ListOfBuffers clist = (*it)->getDescendents();
324                         ListOfBuffers::const_iterator cit = clist.begin();
325                         ListOfBuffers::const_iterator cend = clist.end();
326                         for (; cit != cend; ++cit) {
327                                 string const mangled_child_name = DocFileName(
328                                         changeExtension((*cit)->absFileName(),
329                                                 ".tex")).mangledFileName();
330                                 if (suffixIs(s, mangled_child_name))
331                                         return *cit;
332                         }
333                 }
334         }
335         return 0;
336 }
337
338
339 void BufferList::recordCurrentAuthor(Author const & author)
340 {
341         BufferStorage::iterator it = bstore.begin();
342         BufferStorage::iterator end = bstore.end();
343         for (; it != end; ++it)
344                 (*it)->params().authors().recordCurrentAuthor(author);
345 }
346
347
348 int BufferList::bufferNum(FileName const & fname) const
349 {
350         FileNameList const & buffers = fileNames();
351         FileNameList::const_iterator cit =
352                 find(buffers.begin(), buffers.end(), fname);
353         if (cit == buffers.end())
354                 return 0;
355         return int(cit - buffers.begin());
356 }
357
358
359 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
360 {
361         LASSERT(parent, return false);
362         LASSERT(child, return false);
363         LASSERT(parent->isChild(child), return false);
364
365         // Child document has a different parent, don't close it.
366         Buffer const * parent_ = child->parent();
367         if (parent_ && parent_ != parent)
368                 return false;
369
370         BufferStorage::iterator it = bstore.begin();
371         BufferStorage::iterator end = bstore.end();
372         for (; it != end; ++it) {
373                 Buffer * buf = *it;
374                 if (buf != parent && buf->isChild(child)) {
375                         child->setParent(0);
376                         return false;
377                 }
378         }
379         release(child);
380         return true;
381 }
382
383
384 void BufferList::changed(bool update_metrics) const
385 {
386         BufferStorage::const_iterator it = bstore.begin();
387         BufferStorage::const_iterator end = bstore.end();
388         for (; it != end; ++it)
389                 (*it)->changed(update_metrics);
390         it = binternal.begin();
391         end = binternal.end();
392         for (; it != end; ++it)
393                 (*it)->changed(update_metrics);
394 }
395
396
397 } // namespace lyx