]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Merge branch 'master' of git.lyx.org:lyx
[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         unique_ptr<Buffer> tmpbuf;
134         try {
135                 tmpbuf = make_unique<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 BufferList::fileNames() const
158 {
159         FileNameList nvec;
160         BufferStorage::const_iterator it = bstore.begin();
161         BufferStorage::const_iterator end = bstore.end();
162         for (; it != end; ++it) {
163                 Buffer * buf = *it;
164                 nvec.push_back(buf->fileName());
165         }
166         return nvec;
167 }
168
169
170 Buffer * BufferList::first()
171 {
172         if (bstore.empty())
173                 return 0;
174         return bstore.front();
175 }
176
177
178 Buffer * BufferList::last()
179 {
180         if (bstore.empty())
181                 return 0;
182         return bstore.back();
183 }
184
185
186 Buffer * BufferList::getBuffer(unsigned int choice)
187 {
188         if (choice >= bstore.size())
189                 return 0;
190         return bstore[choice];
191 }
192
193
194 Buffer * BufferList::next(Buffer const * buf) const
195 {
196         // Something is wrong, but we can probably survive it.
197         LASSERT(buf, return 0);
198
199         if (bstore.empty())
200                 return 0;
201         BufferStorage::const_iterator it = 
202                         find(bstore.begin(), bstore.end(), buf);
203         LASSERT(it != bstore.end(), return 0);
204         ++it;
205         Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
206         return nextbuf;
207 }
208
209
210 Buffer * BufferList::previous(Buffer const * buf) const
211 {
212         // Something is wrong, but we can probably survive it.
213         LASSERT(buf, return 0);
214
215         if (bstore.empty())
216                 return 0;
217         BufferStorage::const_iterator it = 
218                         find(bstore.begin(), bstore.end(), buf);
219         LASSERT(it != bstore.end(), return 0);
220
221         Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
222         return previousbuf;
223 }
224
225
226 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
227                                         OutputParams const & runparams_in)
228 {
229         OutputParams runparams = runparams_in;
230         runparams.is_child = true;
231         BufferStorage::iterator it = bstore.begin();
232         BufferStorage::iterator end = bstore.end();
233         for (; it != end; ++it) {
234                 if (!(*it)->isDepClean(masterTmpDir)) {
235                         string writefile = addName(masterTmpDir, (*it)->latexName());
236                         (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
237                                              runparams, Buffer::OnlyBody);
238                         (*it)->markDepClean(masterTmpDir);
239                 }
240         }
241         runparams.is_child = false;
242 }
243
244
245 void BufferList::emergencyWriteAll()
246 {
247         BufferStorage::const_iterator it = bstore.begin();
248         BufferStorage::const_iterator const en = bstore.end();
249         for (; it != en; ++it)
250                  (*it)->emergencyWrite();
251 }
252
253
254 bool BufferList::exists(FileName const & fname) const
255 {
256         return getBuffer(fname) != 0;
257 }
258
259
260  bool BufferList::isLoaded(Buffer const * b) const
261 {
262         if (!b)
263                 return false;
264         BufferStorage::const_iterator cit =
265                 find(bstore.begin(), bstore.end(), b);
266         return cit != bstore.end();
267 }
268
269
270 bool BufferList::isOthersChild(Buffer * parent, Buffer * child)
271 {
272         LASSERT(parent, return false);
273         LASSERT(child, return false);
274         LASSERT(parent->isChild(child), return false);
275         
276         // Does child document have a different parent?
277         Buffer const * parent_ = child->parent();
278         if (parent_ && parent_ != parent)
279                 return true;
280         
281         BufferStorage::iterator it = bstore.begin();
282         BufferStorage::iterator end = bstore.end();
283         for (; it != end; ++it) {
284                 Buffer * buf = *it;
285                 if (buf != parent && buf->isChild(child))
286                         return true;
287         }
288         return false;
289 }
290
291
292 namespace {
293
294 struct equivalent_to : public binary_function<FileName, FileName, bool>
295 {
296         bool operator()(FileName const & x, FileName const & y) const
297         { return equivalent(x, y); }
298 };
299
300 }
301
302
303 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
304 {
305         // 1) cheap test, using string comparison of file names
306         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
307                 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
308         if (it != bstore.end())
309                 return *it;
310         // 2) possibly expensive test, using equivalence test of file names
311         it = find_if(bstore.begin(), bstore.end(),
312                 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
313         if (it != bstore.end())
314                 return *it;
315
316         if (internal) {
317                 // 1) cheap test, using string comparison of file names
318                 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
319                         lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
320                 if (it != binternal.end())
321                         return *it;
322                 // 2) possibly expensive test, using equivalence test of file names
323                 it = find_if(binternal.begin(), binternal.end(),
324                              lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
325                 if (it != binternal.end())
326                         return *it;
327         }
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