]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Provide proper fallback if a bibliography processor is not found
[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->parent();
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::isInternal(Buffer const * b) const
283 {
284         if (!b)
285                 return false;
286         BufferStorage::const_iterator cit =
287                 find(binternal.begin(), binternal.end(), b);
288         return cit != binternal.end();
289 }
290
291
292 bool BufferList::isOthersChild(Buffer * parent, Buffer * child)
293 {
294         LASSERT(parent, return false);
295         LASSERT(child, return false);
296         LASSERT(parent->isChild(child), return false);
297
298         // Does child document have a different parent?
299         Buffer const * parent_ = child->parent();
300         if (parent_ && parent_ != parent)
301                 return true;
302
303         for(Buffer * buf : bstore)
304                 if (buf != parent && buf->isChild(child))
305                         return true;
306         return false;
307 }
308
309
310 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
311 {
312         // 1) cheap test, using string comparison of file names
313         for (Buffer * b : bstore)
314                 if (b->fileName() == fname)
315                         return b;
316         // 2) possibly expensive test, using equivalence test of file names
317         for (Buffer * b : bstore)
318                 if (equivalent(b->fileName(), fname))
319                         return b;
320         if (internal) {
321                 // 1) cheap test, using string comparison of file names
322                 for (Buffer * b : binternal)
323                         if (b->fileName() == fname)
324                                 return b;
325                 // 2) possibly expensive test, using equivalence test of file names
326                 for (Buffer * b : binternal)
327                         if (equivalent(b->fileName(), fname))
328                                 return b;
329         }
330         return 0;
331 }
332
333
334 Buffer * BufferList::getBufferFromTmp(string const & s)
335 {
336         BufferStorage::iterator it = bstore.begin();
337         BufferStorage::iterator end = bstore.end();
338         for (; it < end; ++it) {
339                 if (prefixIs(s, (*it)->temppath())) {
340                         // check whether the filename matches the master
341                         string const master_name = (*it)->latexName();
342                         if (suffixIs(s, master_name))
343                                 return *it;
344                         // if not, try with the children
345                         ListOfBuffers clist = (*it)->getDescendents();
346                         ListOfBuffers::const_iterator cit = clist.begin();
347                         ListOfBuffers::const_iterator cend = clist.end();
348                         for (; cit != cend; ++cit) {
349                                 string const mangled_child_name = DocFileName(
350                                         changeExtension((*cit)->absFileName(),
351                                                 ".tex")).mangledFileName();
352                                 if (suffixIs(s, mangled_child_name))
353                                         return *cit;
354                         }
355                 }
356         }
357         return 0;
358 }
359
360
361 void BufferList::recordCurrentAuthor(Author const & author)
362 {
363         BufferStorage::iterator it = bstore.begin();
364         BufferStorage::iterator end = bstore.end();
365         for (; it != end; ++it)
366                 (*it)->params().authors().recordCurrentAuthor(author);
367 }
368
369
370 void BufferList::updatePreviews()
371 {
372         BufferStorage::iterator it = bstore.begin();
373         BufferStorage::iterator end = bstore.end();
374         for (; it != end; ++it)
375                 (*it)->updatePreviews();
376 }
377
378
379 int BufferList::bufferNum(FileName const & fname) const
380 {
381         FileNameList const buffers(fileNames());
382         FileNameList::const_iterator cit =
383                 find(buffers.begin(), buffers.end(), fname);
384         if (cit == buffers.end())
385                 return 0;
386         return int(cit - buffers.begin());
387 }
388
389
390 void BufferList::changed(bool update_metrics) const
391 {
392         BufferStorage::const_iterator it = bstore.begin();
393         BufferStorage::const_iterator end = bstore.end();
394         for (; it != end; ++it)
395                 (*it)->changed(update_metrics);
396         it = binternal.begin();
397         end = binternal.end();
398         for (; it != end; ++it)
399                 (*it)->changed(update_metrics);
400 }
401
402
403 } // namespace lyx