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