]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Update my email and status.
[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
269 struct equivalent_to : public binary_function<FileName, FileName, bool>
270 {
271         bool operator()(FileName const & x, FileName const & y) const
272         { return equivalent(x, y); }
273 };
274
275 }
276
277
278 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
279 {
280         // 1) cheap test, using string comparison of file names
281         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
282                 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
283         if (it != bstore.end())
284                 return *it;
285         // 2) possibly expensive test, using equivalence test of file names
286         it = find_if(bstore.begin(), bstore.end(),
287                 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
288         if (it != bstore.end())
289                 return *it;
290
291         if (internal) {
292                 // 1) cheap test, using string comparison of file names
293                 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
294                         lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
295                 if (it != binternal.end())
296                         return *it;
297                 // 2) possibly expensive test, using equivalence test of file names
298                 it = find_if(binternal.begin(), binternal.end(),
299                              lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
300                 if (it != binternal.end())
301                         return *it;
302         }
303
304         return 0;
305 }
306
307
308 Buffer * BufferList::getBufferFromTmp(string const & s)
309 {
310         BufferStorage::iterator it = bstore.begin();
311         BufferStorage::iterator end = bstore.end();
312         for (; it < end; ++it) {
313                 if (prefixIs(s, (*it)->temppath())) {
314                         // check whether the filename matches the master
315                         string const master_name = (*it)->latexName();
316                         if (suffixIs(s, master_name))
317                                 return *it;
318                         // if not, try with the children
319                         ListOfBuffers clist = (*it)->getDescendents();
320                         ListOfBuffers::const_iterator cit = clist.begin();
321                         ListOfBuffers::const_iterator cend = clist.end();
322                         for (; cit != cend; ++cit) {
323                                 string const mangled_child_name = DocFileName(
324                                         changeExtension((*cit)->absFileName(),
325                                                 ".tex")).mangledFileName();
326                                 if (suffixIs(s, mangled_child_name))
327                                         return *cit;
328                         }
329                 }
330         }
331         return 0;
332 }
333
334
335 void BufferList::recordCurrentAuthor(Author const & author)
336 {
337         BufferStorage::iterator it = bstore.begin();
338         BufferStorage::iterator end = bstore.end();
339         for (; it != end; ++it)
340                 (*it)->params().authors().recordCurrentAuthor(author);
341 }
342
343
344 int BufferList::bufferNum(FileName const & fname) const
345 {
346         FileNameList const & buffers = fileNames();
347         FileNameList::const_iterator cit =
348                 find(buffers.begin(), buffers.end(), fname);
349         if (cit == buffers.end())
350                 return 0;
351         return int(cit - buffers.begin());
352 }
353
354
355 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
356 {
357         LASSERT(parent, return false);
358         LASSERT(child, return false);
359         LASSERT(parent->isChild(child), return false);
360
361         // Child document has a different parent, don't close it.
362         Buffer const * parent_ = child->parent();
363         if (parent_ && parent_ != parent)
364                 return false;
365
366         BufferStorage::iterator it = bstore.begin();
367         BufferStorage::iterator end = bstore.end();
368         for (; it != end; ++it) {
369                 Buffer * buf = *it;
370                 if (buf != parent && buf->isChild(child)) {
371                         child->setParent(0);
372                         return false;
373                 }
374         }
375         release(child);
376         return true;
377 }
378
379
380 void BufferList::changed(bool update_metrics) const
381 {
382         BufferStorage::const_iterator it = bstore.begin();
383         BufferStorage::const_iterator end = bstore.end();
384         for (; it != end; ++it)
385                 (*it)->changed(update_metrics);
386         it = binternal.begin();
387         end = binternal.end();
388         for (; it != end; ++it)
389                 (*it)->changed(update_metrics);
390 }
391
392
393 } // namespace lyx