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