]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Revert "Objective-C compililation support with cmake and C++11"
[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         // FIXME THREAD
160         static FileNameList nvec;
161         nvec.clear();
162         BufferStorage::const_iterator it = bstore.begin();
163         BufferStorage::const_iterator end = bstore.end();
164         for (; it != end; ++it) {
165                 Buffer * buf = *it;
166                 nvec.push_back(buf->fileName());
167         }
168         return nvec;
169 }
170
171
172 Buffer * BufferList::first()
173 {
174         if (bstore.empty())
175                 return 0;
176         return bstore.front();
177 }
178
179
180 Buffer * BufferList::last()
181 {
182         if (bstore.empty())
183                 return 0;
184         return bstore.back();
185 }
186
187
188 Buffer * BufferList::getBuffer(unsigned int choice)
189 {
190         if (choice >= bstore.size())
191                 return 0;
192         return bstore[choice];
193 }
194
195
196 Buffer * BufferList::next(Buffer const * buf) const
197 {
198         // Something is wrong, but we can probably survive it.
199         LASSERT(buf, return 0);
200
201         if (bstore.empty())
202                 return 0;
203         BufferStorage::const_iterator it = 
204                         find(bstore.begin(), bstore.end(), buf);
205         LASSERT(it != bstore.end(), return 0);
206         ++it;
207         Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
208         return nextbuf;
209 }
210
211
212 Buffer * BufferList::previous(Buffer const * buf) const
213 {
214         // Something is wrong, but we can probably survive it.
215         LASSERT(buf, return 0);
216
217         if (bstore.empty())
218                 return 0;
219         BufferStorage::const_iterator it = 
220                         find(bstore.begin(), bstore.end(), buf);
221         LASSERT(it != bstore.end(), return 0);
222
223         Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
224         return previousbuf;
225 }
226
227
228 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
229                                         OutputParams const & runparams_in)
230 {
231         OutputParams runparams = runparams_in;
232         runparams.is_child = true;
233         BufferStorage::iterator it = bstore.begin();
234         BufferStorage::iterator end = bstore.end();
235         for (; it != end; ++it) {
236                 if (!(*it)->isDepClean(masterTmpDir)) {
237                         string writefile = addName(masterTmpDir, (*it)->latexName());
238                         (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
239                                              runparams, Buffer::OnlyBody);
240                         (*it)->markDepClean(masterTmpDir);
241                 }
242         }
243         runparams.is_child = false;
244 }
245
246
247 void BufferList::emergencyWriteAll()
248 {
249         BufferStorage::const_iterator it = bstore.begin();
250         BufferStorage::const_iterator const en = bstore.end();
251         for (; it != en; ++it)
252                  (*it)->emergencyWrite();
253 }
254
255
256 bool BufferList::exists(FileName const & fname) const
257 {
258         return getBuffer(fname) != 0;
259 }
260
261
262  bool BufferList::isLoaded(Buffer const * b) const
263 {
264         if (!b)
265                 return false;
266         BufferStorage::const_iterator cit =
267                 find(bstore.begin(), bstore.end(), b);
268         return cit != bstore.end();
269 }
270
271
272 namespace {
273
274 struct equivalent_to : public binary_function<FileName, FileName, bool>
275 {
276         bool operator()(FileName const & x, FileName const & y) const
277         { return equivalent(x, y); }
278 };
279
280 }
281
282
283 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
284 {
285         // 1) cheap test, using string comparison of file names
286         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
287                 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
288         if (it != bstore.end())
289                 return *it;
290         // 2) possibly expensive test, using equivalence test of file names
291         it = find_if(bstore.begin(), bstore.end(),
292                 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
293         if (it != bstore.end())
294                 return *it;
295
296         if (internal) {
297                 // 1) cheap test, using string comparison of file names
298                 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
299                         lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
300                 if (it != binternal.end())
301                         return *it;
302                 // 2) possibly expensive test, using equivalence test of file names
303                 it = find_if(binternal.begin(), binternal.end(),
304                              lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
305                 if (it != binternal.end())
306                         return *it;
307         }
308
309         return 0;
310 }
311
312
313 Buffer * BufferList::getBufferFromTmp(string const & s)
314 {
315         BufferStorage::iterator it = bstore.begin();
316         BufferStorage::iterator end = bstore.end();
317         for (; it < end; ++it) {
318                 if (prefixIs(s, (*it)->temppath())) {
319                         // check whether the filename matches the master
320                         string const master_name = (*it)->latexName();
321                         if (suffixIs(s, master_name))
322                                 return *it;
323                         // if not, try with the children
324                         ListOfBuffers clist = (*it)->getDescendents();
325                         ListOfBuffers::const_iterator cit = clist.begin();
326                         ListOfBuffers::const_iterator cend = clist.end();
327                         for (; cit != cend; ++cit) {
328                                 string const mangled_child_name = DocFileName(
329                                         changeExtension((*cit)->absFileName(),
330                                                 ".tex")).mangledFileName();
331                                 if (suffixIs(s, mangled_child_name))
332                                         return *cit;
333                         }
334                 }
335         }
336         return 0;
337 }
338
339
340 void BufferList::recordCurrentAuthor(Author const & author)
341 {
342         BufferStorage::iterator it = bstore.begin();
343         BufferStorage::iterator end = bstore.end();
344         for (; it != end; ++it)
345                 (*it)->params().authors().recordCurrentAuthor(author);
346 }
347
348
349 int BufferList::bufferNum(FileName const & fname) const
350 {
351         FileNameList const & buffers = fileNames();
352         FileNameList::const_iterator cit =
353                 find(buffers.begin(), buffers.end(), fname);
354         if (cit == buffers.end())
355                 return 0;
356         return int(cit - buffers.begin());
357 }
358
359
360 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
361 {
362         LASSERT(parent, return false);
363         LASSERT(child, return false);
364         LASSERT(parent->isChild(child), return false);
365
366         // Child document has a different parent, don't close it.
367         Buffer const * parent_ = child->parent();
368         if (parent_ && parent_ != parent)
369                 return false;
370
371         BufferStorage::iterator it = bstore.begin();
372         BufferStorage::iterator end = bstore.end();
373         for (; it != end; ++it) {
374                 Buffer * buf = *it;
375                 if (buf != parent && buf->isChild(child)) {
376                         child->setParent(0);
377                         return false;
378                 }
379         }
380         release(child);
381         return true;
382 }
383
384
385 void BufferList::changed(bool update_metrics) const
386 {
387         BufferStorage::const_iterator it = bstore.begin();
388         BufferStorage::const_iterator end = bstore.end();
389         for (; it != end; ++it)
390                 (*it)->changed(update_metrics);
391         it = binternal.begin();
392         end = binternal.end();
393         for (; it != end; ++it)
394                 (*it)->changed(update_metrics);
395 }
396
397
398 } // namespace lyx