]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Correction: The inset name is citation, not cite
[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::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->isInternal()) {
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         BufferStorage::const_iterator it = bstore.begin();
144         BufferStorage::const_iterator end = bstore.end();
145         for (; it != end; ++it) {
146                 Buffer * buf = *it;
147                 nvec.push_back(buf->fileName());
148         }
149         return nvec;
150 }
151
152
153 Buffer * BufferList::first()
154 {
155         if (bstore.empty())
156                 return 0;
157         return bstore.front();
158 }
159
160
161 Buffer * BufferList::last()
162 {
163         if (bstore.empty())
164                 return 0;
165         return bstore.back();
166 }
167
168
169 Buffer * BufferList::getBuffer(unsigned int choice)
170 {
171         if (choice >= bstore.size())
172                 return 0;
173         return bstore[choice];
174 }
175
176
177 Buffer * BufferList::next(Buffer const * buf) const
178 {
179         LASSERT(buf, /**/);
180
181         if (bstore.empty())
182                 return 0;
183         BufferStorage::const_iterator it = find(bstore.begin(),
184                                                 bstore.end(), buf);
185         LASSERT(it != bstore.end(), /**/);
186         ++it;
187         Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
188         return nextbuf;
189 }
190
191
192 Buffer * BufferList::previous(Buffer const * buf) const
193 {
194         LASSERT(buf, /**/);
195
196         if (bstore.empty())
197                 return 0;
198         BufferStorage::const_iterator it = find(bstore.begin(),
199                                                 bstore.end(), buf);
200         LASSERT(it != bstore.end(), /**/);
201
202         Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
203         return previousbuf;
204 }
205
206
207 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
208                                         OutputParams const & runparams)
209 {
210         BufferStorage::iterator it = bstore.begin();
211         BufferStorage::iterator end = bstore.end();
212         for (; it != end; ++it) {
213                 if (!(*it)->isDepClean(masterTmpDir)) {
214                         string writefile = addName(masterTmpDir, (*it)->latexName());
215                         (*it)->makeLaTeXFile(FileName(writefile), runparams, false);
216                         (*it)->markDepClean(masterTmpDir);
217                 }
218         }
219 }
220
221
222 void BufferList::emergencyWriteAll()
223 {
224         BufferStorage::const_iterator it = bstore.begin();
225         BufferStorage::const_iterator const en = bstore.end();
226         for (; it != en; ++it)
227                  (*it)->emergencyWrite();
228 }
229
230
231 bool BufferList::exists(FileName const & fname) const
232 {
233         return getBuffer(fname) != 0;
234 }
235
236
237  bool BufferList::isLoaded(Buffer const * b) const
238 {
239         if (!b)
240                 return false;
241         BufferStorage::const_iterator cit =
242                 find(bstore.begin(), bstore.end(), b);
243         return cit != bstore.end();
244 }
245
246
247 namespace {
248 struct equivalent_to : public binary_function<FileName, FileName, bool>
249 {
250         bool operator()(FileName const & x, FileName const & y) const
251         { return equivalent(x, y); }
252 };
253 }
254
255
256 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
257 {
258         // 1) cheap test, using string comparison of file names
259         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
260                 lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
261         if (it != bstore.end())
262                 return *it;
263         // 2) possibly expensive test, using equivalence test of file names
264         it = find_if(bstore.begin(), bstore.end(),
265                 lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
266         if (it != bstore.end())
267                 return *it;
268
269         if (internal) {
270                 // 1) cheap test, using string comparison of file names
271                 BufferStorage::const_iterator it = find_if(binternal.begin(), binternal.end(),
272                         lyx::bind(equal_to<FileName>(), lyx::bind(&Buffer::fileName, _1), fname));
273                 if (it != binternal.end())
274                         return *it;
275                 // 2) possibly expensive test, using equivalence test of file names
276                 it = find_if(binternal.begin(), binternal.end(),
277                              lyx::bind(equivalent_to(), lyx::bind(&Buffer::fileName, _1), fname));
278                 if (it != binternal.end())
279                         return *it;
280         }
281
282         return 0;
283 }
284
285
286 Buffer * BufferList::getBufferFromTmp(string const & s)
287 {
288         BufferStorage::iterator it = bstore.begin();
289         BufferStorage::iterator end = bstore.end();
290         for (; it < end; ++it) {
291                 if (prefixIs(s, (*it)->temppath())) {
292                         // check whether the filename matches the master
293                         string const master_name = (*it)->latexName();
294                         if (suffixIs(s, master_name))
295                                 return *it;
296                         // if not, try with the children
297                         ListOfBuffers clist = (*it)->getDescendents();
298                         ListOfBuffers::const_iterator cit = clist.begin();
299                         ListOfBuffers::const_iterator cend = clist.end();
300                         for (; cit != cend; ++cit) {
301                                 string const mangled_child_name = DocFileName(
302                                         changeExtension((*cit)->absFileName(),
303                                                 ".tex")).mangledFileName();
304                                 if (suffixIs(s, mangled_child_name))
305                                         return *cit;
306                         }
307                 }
308         }
309         return 0;
310 }
311
312
313 void BufferList::recordCurrentAuthor(Author const & author)
314 {
315         BufferStorage::iterator it = bstore.begin();
316         BufferStorage::iterator end = bstore.end();
317         for (; it != end; ++it)
318                 (*it)->params().authors().recordCurrentAuthor(author);
319 }
320
321
322 int BufferList::bufferNum(FileName const & fname) const
323 {
324         FileNameList const & buffers = fileNames();
325         FileNameList::const_iterator cit =
326                 find(buffers.begin(), buffers.end(), fname);
327         if (cit == buffers.end())
328                 return 0;
329         return int(cit - buffers.begin());
330 }
331
332
333 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
334 {
335         LASSERT(parent, return false);
336         LASSERT(child, return false);
337         LASSERT(parent->isChild(child), return false);
338
339         // Child document has a different parent, don't close it.
340         Buffer const * parent_ = child->parent();
341         if (parent_ && parent_ != parent)
342                 return false;
343
344         BufferStorage::iterator it = bstore.begin();
345         BufferStorage::iterator end = bstore.end();
346         for (; it != end; ++it) {
347                 Buffer * buf = *it;
348                 if (buf != parent && buf->isChild(child)) {
349                         child->setParent(0);
350                         return false;
351                 }
352         }
353         release(child);
354         return true;
355 }
356
357
358 void BufferList::changed(bool update_metrics) const
359 {
360         BufferStorage::const_iterator it = bstore.begin();
361         BufferStorage::const_iterator end = bstore.end();
362         for (; it != end; ++it)
363                 (*it)->changed(update_metrics);
364         it = binternal.begin();
365         end = binternal.end();
366         for (; it != end; ++it)
367                 (*it)->changed(update_metrics);
368 }
369
370
371 } // namespace lyx