]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
* add PreBabelPreamble to Language definition (fixes #4786).
[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         LASSERT(buf, /**/);
96         BufferStorage::iterator const it =
97                 find(bstore.begin(), bstore.end(), buf);
98         if (it != bstore.end()) {
99                 Buffer * tmp = (*it);
100                 LASSERT(tmp, /**/);
101                 bstore.erase(it);
102                 delete tmp;
103         }
104 }
105
106
107 Buffer * BufferList::newBuffer(string const & s, bool const ronly)
108 {
109         auto_ptr<Buffer> tmpbuf;
110         try {
111                 tmpbuf.reset(new Buffer(s, ronly));
112         } catch (ExceptionMessage const & message) {
113                 if (message.type_ == ErrorException) {
114                         Alert::error(message.title_, message.details_);
115                         exit(1);
116                 } else if (message.type_ == WarningException) {
117                         Alert::warning(message.title_, message.details_);
118                         return 0;
119                 }
120         }
121         tmpbuf->params().useClassDefaults();
122         if (tmpbuf->isInternal()) {
123                 binternal.push_back(tmpbuf.get());
124         } else {
125                 LYXERR(Debug::INFO, "Assigning to buffer " << bstore.size());
126                 bstore.push_back(tmpbuf.get());
127         }
128         return tmpbuf.release();
129 }
130
131
132 void BufferList::closeAll()
133 {
134         while (!bstore.empty())
135                 release(bstore.front());
136 }
137
138
139 FileNameList const & BufferList::fileNames() const
140 {
141         static FileNameList nvec;
142         nvec.clear();
143         BufferStorage::const_iterator it = bstore.begin();
144         BufferStorage::const_iterator end = bstore.end();
145         for (; it != end; ++it) {
146                 Buffer * buf = *it;
147                 nvec.push_back(buf->fileName());
148         }
149         return nvec;
150 }
151
152
153 Buffer * BufferList::first()
154 {
155         if (bstore.empty())
156                 return 0;
157         return bstore.front();
158 }
159
160
161 Buffer * BufferList::last()
162 {
163         if (bstore.empty())
164                 return 0;
165         return bstore.back();
166 }
167
168
169 Buffer * BufferList::getBuffer(unsigned int choice)
170 {
171         if (choice >= bstore.size())
172                 return 0;
173         return bstore[choice];
174 }
175
176
177 Buffer * BufferList::next(Buffer const * buf) const
178 {
179         LASSERT(buf, /**/);
180
181         if (bstore.empty())
182                 return 0;
183         BufferStorage::const_iterator it = find(bstore.begin(),
184                                                 bstore.end(), buf);
185         LASSERT(it != bstore.end(), /**/);
186         ++it;
187         Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
188         return nextbuf;
189 }
190
191
192 Buffer * BufferList::previous(Buffer const * buf) const
193 {
194         LASSERT(buf, /**/);
195
196         if (bstore.empty())
197                 return 0;
198         BufferStorage::const_iterator it = find(bstore.begin(),
199                                                 bstore.end(), buf);
200         LASSERT(it != bstore.end(), /**/);
201
202         Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
203         return previousbuf;
204 }
205
206
207 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
208                                         OutputParams const & runparams)
209 {
210         BufferStorage::iterator it = bstore.begin();
211         BufferStorage::iterator end = bstore.end();
212         for (; it != end; ++it) {
213                 if (!(*it)->isDepClean(masterTmpDir)) {
214                         string writefile = addName(masterTmpDir, (*it)->latexName());
215                         (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
216                                              runparams, false);
217                         (*it)->markDepClean(masterTmpDir);
218                 }
219         }
220 }
221
222
223 void BufferList::emergencyWriteAll()
224 {
225         BufferStorage::const_iterator it = bstore.begin();
226         BufferStorage::const_iterator const en = bstore.end();
227         for (; it != en; ++it)
228                  (*it)->emergencyWrite();
229 }
230
231
232 bool BufferList::exists(FileName const & fname) const
233 {
234         return getBuffer(fname) != 0;
235 }
236
237
238  bool BufferList::isLoaded(Buffer const * b) const
239 {
240         if (!b)
241                 return false;
242         BufferStorage::const_iterator cit =
243                 find(bstore.begin(), bstore.end(), b);
244         return cit != bstore.end();
245 }
246
247
248 namespace {
249 struct equivalent_to : public binary_function<FileName, FileName, bool>
250 {
251         bool operator()(FileName const & x, FileName const & y) const
252         { return equivalent(x, y); }
253 };
254 }
255
256
257 Buffer * BufferList::getBuffer(support::FileName const & fname) const
258 {
259         // 1) cheap test, using string comparison of file names
260         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
261                 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
262         if (it != bstore.end())
263                         return *it;
264         // 2) possibly expensive test, using equivalence test of file names
265         it = find_if(bstore.begin(), bstore.end(),
266                 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
267         return it != bstore.end() ? (*it) : 0;
268 }
269
270
271 Buffer * BufferList::getBufferFromTmp(string const & s)
272 {
273         BufferStorage::iterator it = bstore.begin();
274         BufferStorage::iterator end = bstore.end();
275         for (; it < end; ++it) {
276                 if (prefixIs(s, (*it)->temppath())) {
277                         // check whether the filename matches the master
278                         string const master_name = (*it)->latexName();
279                         if (suffixIs(s, master_name))
280                                 return *it;
281                         // if not, try with the children
282                         ListOfBuffers clist = (*it)->getDescendents();
283                         ListOfBuffers::const_iterator cit = clist.begin();
284                         ListOfBuffers::const_iterator cend = clist.end();
285                         for (; cit != cend; ++cit) {
286                                 string const mangled_child_name = DocFileName(
287                                         changeExtension((*cit)->absFileName(),
288                                                 ".tex")).mangledFileName();
289                                 if (suffixIs(s, mangled_child_name))
290                                         return *cit;
291                         }
292                 }
293         }
294         return 0;
295 }
296
297
298 void BufferList::recordCurrentAuthor(Author const & author)
299 {
300         BufferStorage::iterator it = bstore.begin();
301         BufferStorage::iterator end = bstore.end();
302         for (; it != end; ++it)
303                 (*it)->params().authors().recordCurrentAuthor(author);
304 }
305
306
307 int BufferList::bufferNum(FileName const & fname) const
308 {
309         FileNameList const & buffers = fileNames();
310         FileNameList::const_iterator cit =
311                 find(buffers.begin(), buffers.end(), fname);
312         if (cit == buffers.end())
313                 return 0;
314         return int(cit - buffers.begin());
315 }
316
317
318 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
319 {
320         LASSERT(parent, return false);
321         LASSERT(child, return false);
322         LASSERT(parent->isChild(child), return false);
323
324         // Child document has a different parent, don't close it.
325         Buffer const * parent_ = child->parent();
326         if (parent_ && parent_ != parent)
327                 return false;
328
329         BufferStorage::iterator it = bstore.begin();
330         BufferStorage::iterator end = bstore.end();
331         for (; it != end; ++it) {
332                 Buffer * buf = *it;
333                 if (buf != parent && buf->isChild(child)) {
334                         child->setParent(0);
335                         return false;
336                 }
337         }
338         release(child);
339         return true;
340 }
341
342
343 void BufferList::changed(bool update_metrics) const
344 {
345         BufferStorage::const_iterator it = bstore.begin();
346         BufferStorage::const_iterator end = bstore.end();
347         for (; it != end; ++it)
348                 (*it)->changed(update_metrics);
349         it = binternal.begin();
350         end = binternal.end();
351         for (; it != end; ++it)
352                 (*it)->changed(update_metrics);
353 }
354
355
356 } // namespace lyx