]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
43e81316a640be034ba73bcd83048b8d6495b2f8
[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
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         transform(bstore.begin(), bstore.end(),
144                   back_inserter(nvec),
145                   bind(&Buffer::fileName, _1));
146         return nvec;
147 }
148
149
150 Buffer * BufferList::first()
151 {
152         if (bstore.empty())
153                 return 0;
154         return bstore.front();
155 }
156
157
158 Buffer * BufferList::last()
159 {
160         if (bstore.empty())
161                 return 0;
162         return bstore.back();
163 }
164
165
166 Buffer * BufferList::getBuffer(unsigned int choice)
167 {
168         if (choice >= bstore.size())
169                 return 0;
170         return bstore[choice];
171 }
172
173
174 Buffer * BufferList::next(Buffer const * buf) const
175 {
176         LASSERT(buf, /**/);
177
178         if (bstore.empty())
179                 return 0;
180         BufferStorage::const_iterator it = find(bstore.begin(),
181                                                 bstore.end(), buf);
182         LASSERT(it != bstore.end(), /**/);
183         ++it;
184         Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
185         return nextbuf;
186 }
187
188
189 Buffer * BufferList::previous(Buffer const * buf) const
190 {
191         LASSERT(buf, /**/);
192
193         if (bstore.empty())
194                 return 0;
195         BufferStorage::const_iterator it = find(bstore.begin(),
196                                                 bstore.end(), buf);
197         LASSERT(it != bstore.end(), /**/);
198
199         Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
200         return previousbuf;
201 }
202
203
204 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
205                                         OutputParams const & runparams)
206 {
207         BufferStorage::iterator it = bstore.begin();
208         BufferStorage::iterator end = bstore.end();
209         for (; it != end; ++it) {
210                 if (!(*it)->isDepClean(masterTmpDir)) {
211                         string writefile = addName(masterTmpDir, (*it)->latexName());
212                         (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
213                                              runparams, false);
214                         (*it)->markDepClean(masterTmpDir);
215                 }
216         }
217 }
218
219
220 void BufferList::emergencyWriteAll()
221 {
222         BufferStorage::const_iterator it = bstore.begin();
223         BufferStorage::const_iterator const en = bstore.end();
224         for (; it != en; ++it)
225                  (*it)->emergencyWrite();
226 }
227
228
229 bool BufferList::exists(FileName const & fname) const
230 {
231         return getBuffer(fname) != 0;
232 }
233
234
235  bool BufferList::isLoaded(Buffer const * b) const
236 {
237         if (!b)
238                 return false;
239         BufferStorage::const_iterator cit =
240                 find(bstore.begin(), bstore.end(), b);
241         return cit != bstore.end();
242 }
243
244
245 namespace {
246 struct equivalent_to : public binary_function<FileName, FileName, bool>
247 {
248         bool operator()(FileName const & x, FileName const & y) const
249         { return equivalent(x, y); }
250 };
251 }
252
253
254 Buffer * BufferList::getBuffer(support::FileName const & fname) const
255 {
256         // 1) cheap test, using string comparison of file names
257         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
258                 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
259         if (it != bstore.end())
260                         return *it;
261         // 2) possibly expensive test, using equivalence test of file names
262         it = find_if(bstore.begin(), bstore.end(),
263                 bind(equivalent_to(), bind(&Buffer::fileName, _1), fname));
264         return it != bstore.end() ? (*it) : 0;
265 }
266
267
268 Buffer * BufferList::getBufferFromTmp(string const & s)
269 {
270         BufferStorage::iterator it = bstore.begin();
271         BufferStorage::iterator end = bstore.end();
272         for (; it < end; ++it) {
273                 if (prefixIs(s, (*it)->temppath())) {
274                         // check whether the filename matches the master
275                         string const master_name = (*it)->latexName();
276                         if (suffixIs(s, master_name))
277                                 return *it;
278                         // if not, try with the children
279                         vector<Buffer *> clist = (*it)->getChildren();
280                         vector<Buffer *>::const_iterator cit = clist.begin();
281                         vector<Buffer *>::const_iterator cend = clist.end();
282                         for (; cit < cend; ++cit) {
283                                 string const mangled_child_name = DocFileName(
284                                         changeExtension((*cit)->absFileName(),
285                                                 ".tex")).mangledFileName();
286                                 if (suffixIs(s, mangled_child_name))
287                                         return *cit;
288                         }
289                 }
290         }
291         return 0;
292 }
293
294
295 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
296 {
297         BufferStorage::iterator it = bstore.begin();
298         BufferStorage::iterator end = bstore.end();
299         for (; it != end; ++it)
300                 (*it)->params().authors().record(0, Author(name, email));
301 }
302
303
304 int BufferList::bufferNum(FileName const & fname) const
305 {
306         FileNameList const & buffers = fileNames();
307         FileNameList::const_iterator cit =
308                 find(buffers.begin(), buffers.end(), fname);
309         if (cit == buffers.end())
310                 return 0;
311         return int(cit - buffers.begin());
312 }
313
314
315 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
316 {
317         LASSERT(parent, return false);
318         LASSERT(child, return false);
319         LASSERT(parent->isChild(child), return false);
320
321         // Child document has a different parent, don't close it.
322         Buffer const * parent_ = child->parent();
323         if (parent_ && parent_ != parent)
324                 return false;
325
326         BufferStorage::iterator it = bstore.begin();
327         BufferStorage::iterator end = bstore.end();
328         for (; it != end; ++it) {
329                 Buffer * buf = *it;
330                 if (buf != parent && buf->isChild(child)) {
331                         child->setParent(0);
332                         return false;
333                 }
334         }
335         release(child);
336         return true;
337 }
338
339
340 void BufferList::changed(bool update_metrics) const
341 {
342         BufferStorage::const_iterator it = bstore.begin();
343         BufferStorage::const_iterator end = bstore.end();
344         for (; it != end; ++it)
345                 (*it)->changed(update_metrics);
346         it = binternal.begin();
347         end = binternal.end();
348         for (; it != end; ++it)
349                 (*it)->changed(update_metrics);
350 }
351
352
353 } // namespace lyx