]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Remove hardcoded values
[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
36 #include <algorithm>
37 #include <iterator>
38 #include <memory>
39
40 using namespace std;
41 using namespace lyx::support;
42
43 namespace lyx {
44
45 namespace Alert = lyx::frontend::Alert;
46
47
48 BufferList::BufferList()
49 {}
50
51
52 BufferList::~BufferList()
53 {
54         BufferStorage::iterator it = binternal.begin();
55         BufferStorage::iterator end = binternal.end();
56         for (; it != end; ++it)
57                 delete (*it);
58 }
59
60
61 bool BufferList::empty() const
62 {
63         return bstore.empty();
64 }
65
66
67 BufferList::iterator BufferList::begin()
68 {
69         return bstore.begin();
70 }
71
72
73 BufferList::const_iterator BufferList::begin() const
74 {
75         return bstore.begin();
76 }
77
78
79 BufferList::iterator BufferList::end()
80 {
81         return bstore.end();
82 }
83
84
85 BufferList::const_iterator BufferList::end() const
86 {
87         return bstore.end();
88 }
89
90
91 void BufferList::release(Buffer * buf)
92 {
93         // We may leak here, but we probably do not need to
94         // shut down.
95         LASSERT(buf, return);
96         BufferStorage::iterator const it =
97                 find(bstore.begin(), bstore.end(), buf);
98         if (it != bstore.end()) {
99                 Buffer * tmp = (*it);
100                 bstore.erase(it);
101                 LASSERT(tmp, return);
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         unique_ptr<Buffer> tmpbuf;
132         try {
133                 tmpbuf = make_unique<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 BufferList::fileNames() const
156 {
157         FileNameList nvec;
158         BufferStorage::const_iterator it = bstore.begin();
159         BufferStorage::const_iterator end = bstore.end();
160         for (; it != end; ++it) {
161                 Buffer * buf = *it;
162                 nvec.push_back(buf->fileName());
163         }
164         return nvec;
165 }
166
167
168 Buffer * BufferList::first()
169 {
170         if (bstore.empty())
171                 return 0;
172         return bstore.front();
173 }
174
175
176 Buffer * BufferList::last()
177 {
178         if (bstore.empty())
179                 return 0;
180         return bstore.back();
181 }
182
183
184 Buffer * BufferList::getBuffer(unsigned int choice)
185 {
186         if (choice >= bstore.size())
187                 return 0;
188         return bstore[choice];
189 }
190
191
192 Buffer * BufferList::next(Buffer const * buf) const
193 {
194         // Something is wrong, but we can probably survive it.
195         LASSERT(buf, return 0);
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(), return 0);
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         // Something is wrong, but we can probably survive it.
211         LASSERT(buf, return 0);
212
213         if (bstore.empty())
214                 return 0;
215         BufferStorage::const_iterator it = 
216                         find(bstore.begin(), bstore.end(), buf);
217         LASSERT(it != bstore.end(), return 0);
218
219         Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
220         return previousbuf;
221 }
222
223
224 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
225                                         OutputParams const & runparams_in)
226 {
227         OutputParams runparams = runparams_in;
228         runparams.is_child = true;
229         BufferStorage::iterator it = bstore.begin();
230         BufferStorage::iterator end = bstore.end();
231         for (; it != end; ++it) {
232                 if (!(*it)->isDepClean(masterTmpDir)) {
233                         string writefile = addName(masterTmpDir, (*it)->latexName());
234                         (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
235                                              runparams, Buffer::OnlyBody);
236                         (*it)->markDepClean(masterTmpDir);
237                 }
238         }
239         runparams.is_child = false;
240 }
241
242
243 void BufferList::emergencyWriteAll()
244 {
245         BufferStorage::const_iterator it = bstore.begin();
246         BufferStorage::const_iterator const en = bstore.end();
247         for (; it != en; ++it)
248                  (*it)->emergencyWrite();
249 }
250
251
252 void BufferList::invalidateConverterCache() const
253 {
254         BufferStorage::const_iterator it = bstore.begin();
255         BufferStorage::const_iterator const en = bstore.end();
256         for (; it != en; ++it)
257                 (*it)->params().invalidateConverterCache();
258 }
259
260
261 bool BufferList::exists(FileName const & fname) const
262 {
263         return getBuffer(fname) != 0;
264 }
265
266
267  bool BufferList::isLoaded(Buffer const * b) const
268 {
269         if (!b)
270                 return false;
271         BufferStorage::const_iterator cit =
272                 find(bstore.begin(), bstore.end(), b);
273         return cit != bstore.end();
274 }
275
276
277 bool BufferList::isOthersChild(Buffer * parent, Buffer * child)
278 {
279         LASSERT(parent, return false);
280         LASSERT(child, return false);
281         LASSERT(parent->isChild(child), return false);
282         
283         // Does child document have a different parent?
284         Buffer const * parent_ = child->parent();
285         if (parent_ && parent_ != parent)
286                 return true;
287
288         for(Buffer * buf : bstore)
289                 if (buf != parent && buf->isChild(child))
290                         return true;
291         return false;
292 }
293
294
295 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
296 {
297         // 1) cheap test, using string comparison of file names
298         for (Buffer * b : bstore)
299                 if (b->fileName() == fname)
300                         return b;
301         // 2) possibly expensive test, using equivalence test of file names
302         for (Buffer * b : bstore)
303                 if (equivalent(b->fileName(), fname))
304                         return b;
305         if (internal) {
306                 // 1) cheap test, using string comparison of file names
307                 for (Buffer * b : binternal)
308                         if (b->fileName() == fname)
309                                 return b;
310                 // 2) possibly expensive test, using equivalence test of file names
311                 for (Buffer * b : binternal)
312                         if (equivalent(b->fileName(), fname))
313                                 return b;
314         }
315         return 0;
316 }
317
318
319 Buffer * BufferList::getBufferFromTmp(string const & s)
320 {
321         BufferStorage::iterator it = bstore.begin();
322         BufferStorage::iterator end = bstore.end();
323         for (; it < end; ++it) {
324                 if (prefixIs(s, (*it)->temppath())) {
325                         // check whether the filename matches the master
326                         string const master_name = (*it)->latexName();
327                         if (suffixIs(s, master_name))
328                                 return *it;
329                         // if not, try with the children
330                         ListOfBuffers clist = (*it)->getDescendents();
331                         ListOfBuffers::const_iterator cit = clist.begin();
332                         ListOfBuffers::const_iterator cend = clist.end();
333                         for (; cit != cend; ++cit) {
334                                 string const mangled_child_name = DocFileName(
335                                         changeExtension((*cit)->absFileName(),
336                                                 ".tex")).mangledFileName();
337                                 if (suffixIs(s, mangled_child_name))
338                                         return *cit;
339                         }
340                 }
341         }
342         return 0;
343 }
344
345
346 void BufferList::recordCurrentAuthor(Author const & author)
347 {
348         BufferStorage::iterator it = bstore.begin();
349         BufferStorage::iterator end = bstore.end();
350         for (; it != end; ++it)
351                 (*it)->params().authors().recordCurrentAuthor(author);
352 }
353
354
355 void BufferList::updatePreviews()
356 {
357         BufferStorage::iterator it = bstore.begin();
358         BufferStorage::iterator end = bstore.end();
359         for (; it != end; ++it)
360                 (*it)->updatePreviews();
361 }
362
363
364 int BufferList::bufferNum(FileName const & fname) const
365 {
366         FileNameList const buffers(fileNames());
367         FileNameList::const_iterator cit =
368                 find(buffers.begin(), buffers.end(), fname);
369         if (cit == buffers.end())
370                 return 0;
371         return int(cit - buffers.begin());
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