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