]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Release children of a master if the children don't have a default master. It was...
[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 <boost/bind.hpp>
36
37 #include <algorithm>
38 #include <functional>
39
40 using boost::bind;
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->fileName().extension() == "internal") {
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                   boost::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         BufferStorage::const_iterator cit =
238                 find(bstore.begin(), bstore.end(), b);
239         return cit != bstore.end();
240 }
241
242
243 namespace {
244 struct equivalent_to : public binary_function<FileName, FileName, bool>
245 {
246         bool operator()(FileName const & x, FileName const & y) const
247         { return equivalent(x, y); }
248 };
249 }
250
251
252 Buffer * BufferList::getBuffer(support::FileName const & fname) const
253 {
254         // 1) cheap test, using string comparison of file names
255         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
256                 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
257         if (it != bstore.end())
258                         return *it;
259         // 2) possibly expensive test, using equivalence test of file names
260         it = find_if(bstore.begin(), bstore.end(),
261                 bind(equivalent_to(), bind(&Buffer::fileName, _1), fname));
262         return it != bstore.end() ? (*it) : 0;
263 }
264
265
266 Buffer * BufferList::getBufferFromTmp(string const & s)
267 {
268         BufferStorage::iterator it = bstore.begin();
269         BufferStorage::iterator end = bstore.end();
270         for (; it < end; ++it) {
271                 if (prefixIs(s, (*it)->temppath())) {
272                         // check whether the filename matches the master
273                         string const master_name = changeExtension(onlyFilename(
274                                                 (*it)->absFileName()), ".tex");
275                         if (suffixIs(s, master_name))
276                                 return *it;
277                         // if not, try with the children
278                         vector<Buffer *> clist = (*it)->getChildren();
279                         vector<Buffer *>::const_iterator cit = clist.begin();
280                         vector<Buffer *>::const_iterator cend = clist.end();
281                         for (; cit < cend; ++cit) {
282                                 string const mangled_child_name = DocFileName(
283                                         changeExtension((*cit)->absFileName(),
284                                                 ".tex")).mangledFilename();
285                                 if (suffixIs(s, mangled_child_name))
286                                         return *cit;
287                         }
288                 }
289         }
290         return 0;
291 }
292
293
294 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
295 {
296         BufferStorage::iterator it = bstore.begin();
297         BufferStorage::iterator end = bstore.end();
298         for (; it != end; ++it)
299                 (*it)->params().authors().record(0, Author(name, email));
300 }
301
302
303 int BufferList::bufferNum(FileName const & fname) const
304 {
305         FileNameList const & buffers = fileNames();
306         FileNameList::const_iterator cit =
307                 find(buffers.begin(), buffers.end(), fname);
308         if (cit == buffers.end())
309                 return 0;
310         return int(cit - buffers.begin());
311 }
312
313
314 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
315 {
316         LASSERT(parent, return false);
317         LASSERT(child, return false);
318         LASSERT(parent->isChild(child), return false);
319
320         // Child document has a different parent, don't close it.
321         Buffer const * parent_ = child->parent();
322         if (parent_ && parent_ != parent)
323                 return false;
324
325         BufferStorage::iterator it = bstore.begin();
326         BufferStorage::iterator end = bstore.end();
327         for (; it != end; ++it) {
328                 Buffer * buf = *it;
329                 if (buf != parent && buf->isChild(child)) {
330                         child->setParent(0);
331                         return false;
332                 }
333         }
334         release(child);
335         return true;
336 }
337
338
339 } // namespace lyx