]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Check path of Qt tools if qtchooser is detected
[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 bool BufferList::exists(FileName const & fname) const
253 {
254         return getBuffer(fname) != 0;
255 }
256
257
258  bool BufferList::isLoaded(Buffer const * b) const
259 {
260         if (!b)
261                 return false;
262         BufferStorage::const_iterator cit =
263                 find(bstore.begin(), bstore.end(), b);
264         return cit != bstore.end();
265 }
266
267
268 bool BufferList::isOthersChild(Buffer * parent, Buffer * child)
269 {
270         LASSERT(parent, return false);
271         LASSERT(child, return false);
272         LASSERT(parent->isChild(child), return false);
273         
274         // Does child document have a different parent?
275         Buffer const * parent_ = child->parent();
276         if (parent_ && parent_ != parent)
277                 return true;
278
279         for(Buffer * buf : bstore)
280                 if (buf != parent && buf->isChild(child))
281                         return true;
282         return false;
283 }
284
285
286 Buffer * BufferList::getBuffer(support::FileName const & fname, bool internal) const
287 {
288         // 1) cheap test, using string comparison of file names
289         for (Buffer * b : bstore)
290                 if (b->fileName() == fname)
291                         return b;
292         // 2) possibly expensive test, using equivalence test of file names
293         for (Buffer * b : bstore)
294                 if (equivalent(b->fileName(), fname))
295                         return b;
296         if (internal) {
297                 // 1) cheap test, using string comparison of file names
298                 for (Buffer * b : binternal)
299                         if (b->fileName() == fname)
300                                 return b;
301                 // 2) possibly expensive test, using equivalence test of file names
302                 for (Buffer * b : binternal)
303                         if (equivalent(b->fileName(), fname))
304                                 return b;
305         }
306         return 0;
307 }
308
309
310 Buffer * BufferList::getBufferFromTmp(string const & s)
311 {
312         BufferStorage::iterator it = bstore.begin();
313         BufferStorage::iterator end = bstore.end();
314         for (; it < end; ++it) {
315                 if (prefixIs(s, (*it)->temppath())) {
316                         // check whether the filename matches the master
317                         string const master_name = (*it)->latexName();
318                         if (suffixIs(s, master_name))
319                                 return *it;
320                         // if not, try with the children
321                         ListOfBuffers clist = (*it)->getDescendents();
322                         ListOfBuffers::const_iterator cit = clist.begin();
323                         ListOfBuffers::const_iterator cend = clist.end();
324                         for (; cit != cend; ++cit) {
325                                 string const mangled_child_name = DocFileName(
326                                         changeExtension((*cit)->absFileName(),
327                                                 ".tex")).mangledFileName();
328                                 if (suffixIs(s, mangled_child_name))
329                                         return *cit;
330                         }
331                 }
332         }
333         return 0;
334 }
335
336
337 void BufferList::recordCurrentAuthor(Author const & author)
338 {
339         BufferStorage::iterator it = bstore.begin();
340         BufferStorage::iterator end = bstore.end();
341         for (; it != end; ++it)
342                 (*it)->params().authors().recordCurrentAuthor(author);
343 }
344
345
346 void BufferList::updatePreviews()
347 {
348         BufferStorage::iterator it = bstore.begin();
349         BufferStorage::iterator end = bstore.end();
350         for (; it != end; ++it)
351                 (*it)->updatePreviews();
352 }
353
354
355 int BufferList::bufferNum(FileName const & fname) const
356 {
357         FileNameList const buffers(fileNames());
358         FileNameList::const_iterator cit =
359                 find(buffers.begin(), buffers.end(), fname);
360         if (cit == buffers.end())
361                 return 0;
362         return int(cit - buffers.begin());
363 }
364
365
366 void BufferList::changed(bool update_metrics) const
367 {
368         BufferStorage::const_iterator it = bstore.begin();
369         BufferStorage::const_iterator end = bstore.end();
370         for (; it != end; ++it)
371                 (*it)->changed(update_metrics);
372         it = binternal.begin();
373         end = binternal.end();
374         for (; it != end; ++it)
375                 (*it)->changed(update_metrics);
376 }
377
378
379 } // namespace lyx