]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Tracking correctly available translations (take 2)
[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)
225 {
226         BufferStorage::iterator it = bstore.begin();
227         BufferStorage::iterator end = bstore.end();
228         for (; it != end; ++it) {
229                 if (!(*it)->isDepClean(masterTmpDir)) {
230                         string writefile = addName(masterTmpDir, (*it)->latexName());
231                         (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
232                                              runparams, Buffer::OnlyBody);
233                         (*it)->markDepClean(masterTmpDir);
234                 }
235         }
236 }
237
238
239 void BufferList::emergencyWriteAll()
240 {
241         BufferStorage::const_iterator it = bstore.begin();
242         BufferStorage::const_iterator const en = bstore.end();
243         for (; it != en; ++it)
244                  (*it)->emergencyWrite();
245 }
246
247
248 bool BufferList::exists(FileName const & fname) const
249 {
250         return getBuffer(fname) != 0;
251 }
252
253
254  bool BufferList::isLoaded(Buffer const * b) const
255 {
256         if (!b)
257                 return false;
258         BufferStorage::const_iterator cit =
259                 find(bstore.begin(), bstore.end(), b);
260         return cit != bstore.end();
261 }
262
263
264 namespace {
265 struct equivalent_to : public binary_function<FileName, FileName, bool>
266 {
267         bool operator()(FileName const & x, FileName const & y) const
268         { return equivalent(x, y); }
269 };
270 }
271
272
273 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
274 {
275         // 1) cheap test, using string comparison of file names
276         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
277                 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
278         if (it != bstore.end())
279                 return *it;
280         // 2) possibly expensive test, using equivalence test of file names
281         it = find_if(bstore.begin(), bstore.end(),
282                 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
283         if (it != bstore.end())
284                 return *it;
285
286         if (internal) {
287                 // 1) cheap test, using string comparison of file names
288                 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
289                         lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
290                 if (it != binternal.end())
291                         return *it;
292                 // 2) possibly expensive test, using equivalence test of file names
293                 it = find_if(binternal.begin(), binternal.end(),
294                              lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
295                 if (it != binternal.end())
296                         return *it;
297         }
298
299         return 0;
300 }
301
302
303 Buffer * BufferList::getBufferFromTmp(string const & s)
304 {
305         BufferStorage::iterator it = bstore.begin();
306         BufferStorage::iterator end = bstore.end();
307         for (; it < end; ++it) {
308                 if (prefixIs(s, (*it)->temppath())) {
309                         // check whether the filename matches the master
310                         string const master_name = (*it)->latexName();
311                         if (suffixIs(s, master_name))
312                                 return *it;
313                         // if not, try with the children
314                         ListOfBuffers clist = (*it)->getDescendents();
315                         ListOfBuffers::const_iterator cit = clist.begin();
316                         ListOfBuffers::const_iterator cend = clist.end();
317                         for (; cit != cend; ++cit) {
318                                 string const mangled_child_name = DocFileName(
319                                         changeExtension((*cit)->absFileName(),
320                                                 ".tex")).mangledFileName();
321                                 if (suffixIs(s, mangled_child_name))
322                                         return *cit;
323                         }
324                 }
325         }
326         return 0;
327 }
328
329
330 void BufferList::recordCurrentAuthor(Author const & author)
331 {
332         BufferStorage::iterator it = bstore.begin();
333         BufferStorage::iterator end = bstore.end();
334         for (; it != end; ++it)
335                 (*it)->params().authors().recordCurrentAuthor(author);
336 }
337
338
339 int BufferList::bufferNum(FileName const & fname) const
340 {
341         FileNameList const & buffers = fileNames();
342         FileNameList::const_iterator cit =
343                 find(buffers.begin(), buffers.end(), fname);
344         if (cit == buffers.end())
345                 return 0;
346         return int(cit - buffers.begin());
347 }
348
349
350 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
351 {
352         LASSERT(parent, return false);
353         LASSERT(child, return false);
354         LASSERT(parent->isChild(child), return false);
355
356         // Child document has a different parent, don't close it.
357         Buffer const * parent_ = child->parent();
358         if (parent_ && parent_ != parent)
359                 return false;
360
361         BufferStorage::iterator it = bstore.begin();
362         BufferStorage::iterator end = bstore.end();
363         for (; it != end; ++it) {
364                 Buffer * buf = *it;
365                 if (buf != parent && buf->isChild(child)) {
366                         child->setParent(0);
367                         return false;
368                 }
369         }
370         release(child);
371         return true;
372 }
373
374
375 void BufferList::changed(bool update_metrics) const
376 {
377         BufferStorage::const_iterator it = bstore.begin();
378         BufferStorage::const_iterator end = bstore.end();
379         for (; it != end; ++it)
380                 (*it)->changed(update_metrics);
381         it = binternal.begin();
382         end = binternal.end();
383         for (; it != end; ++it)
384                 (*it)->changed(update_metrics);
385 }
386
387
388 } // namespace lyx