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