]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
Fix bug 4441. GuiRef: Ok button must be default.
[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 <boost/bind.hpp>
36
37 #include <algorithm>
38 #include <functional>
39
40 using boost::bind;
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->fileName().extension() == "internal") {
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         transform(bstore.begin(), bstore.end(),
144                   back_inserter(nvec),
145                   boost::bind(&Buffer::fileName, _1));
146         return nvec;
147 }
148
149
150 Buffer * BufferList::first()
151 {
152         if (bstore.empty())
153                 return 0;
154         return bstore.front();
155 }
156
157
158 Buffer * BufferList::last()
159 {
160         if (bstore.empty())
161                 return 0;
162         return bstore.back();
163 }
164
165
166 Buffer * BufferList::getBuffer(unsigned int choice)
167 {
168         if (choice >= bstore.size())
169                 return 0;
170         return bstore[choice];
171 }
172
173
174 Buffer * BufferList::next(Buffer const * buf) const
175 {
176         LASSERT(buf, /**/);
177
178         if (bstore.empty())
179                 return 0;
180         BufferStorage::const_iterator it = find(bstore.begin(),
181                                                 bstore.end(), buf);
182         LASSERT(it != bstore.end(), /**/);
183         ++it;
184         Buffer * nextbuf = (it == bstore.end()) ? bstore.front() : *it;
185         return nextbuf;
186 }
187
188
189 Buffer * BufferList::previous(Buffer const * buf) const
190 {
191         LASSERT(buf, /**/);
192
193         if (bstore.empty())
194                 return 0;
195         BufferStorage::const_iterator it = find(bstore.begin(),
196                                                 bstore.end(), buf);
197         LASSERT(it != bstore.end(), /**/);
198
199         Buffer * previousbuf = (it == bstore.begin()) ? bstore.back() : *(it - 1);
200         return previousbuf;
201 }
202
203
204 void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
205                                         OutputParams const & runparams)
206 {
207         BufferStorage::iterator it = bstore.begin();
208         BufferStorage::iterator end = bstore.end();
209         for (; it != end; ++it) {
210                 if (!(*it)->isDepClean(masterTmpDir)) {
211                         string writefile = addName(masterTmpDir, (*it)->latexName());
212                         (*it)->makeLaTeXFile(FileName(writefile), masterTmpDir,
213                                              runparams, false);
214                         (*it)->markDepClean(masterTmpDir);
215                 }
216         }
217 }
218
219
220 void BufferList::emergencyWriteAll()
221 {
222         for_each(bstore.begin(), bstore.end(),
223                  bind(&BufferList::emergencyWrite, this, _1));
224 }
225
226
227 docstring BufferList::emergencyWrite(Buffer * buf)
228 {
229         // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
230         // compare with 0 to avoid pointer/interger comparison
231         // ::assert(buf != 0);
232         if (!buf)
233                 return _("No file open!");
234
235         // No need to save if the buffer has not changed.
236         if (buf->isClean())
237                 return docstring();
238
239         string const doc = buf->isUnnamed()
240                 ? onlyFilename(buf->absFileName()) : buf->absFileName();
241
242         docstring user_message = bformat(
243                 _("LyX: Attempting to save document %1$s\n"), from_utf8(doc));
244
245         // We try to save three places:
246         // 1) Same place as document. Unless it is an unnamed doc.
247         if (!buf->isUnnamed()) {
248                 string s = buf->absFileName();
249                 s += ".emergency";
250                 lyxerr << "  " << s << endl;
251                 if (buf->writeFile(FileName(s))) {
252                         buf->markClean();
253                         user_message += _("  Save seems successful. Phew.\n");
254                         return user_message;
255                 } else {
256                         user_message += _("  Save failed! Trying...\n");
257                 }
258         }
259
260         // 2) In HOME directory.
261         string s = addName(package().home_dir().absFilename(), buf->absFileName());
262         s += ".emergency";
263         lyxerr << ' ' << s << endl;
264         if (buf->writeFile(FileName(s))) {
265                 buf->markClean();
266                 user_message += _("  Save seems successful. Phew.\n");
267                 return user_message;
268         }
269
270         user_message += _("  Save failed! Trying...\n");
271
272         // 3) In "/tmp" directory.
273         // MakeAbsPath to prepend the current
274         // drive letter on OS/2
275         s = addName(package().temp_dir().absFilename(), buf->absFileName());
276         s += ".emergency";
277         lyxerr << ' ' << s << endl;
278         if (buf->writeFile(FileName(s))) {
279                 buf->markClean();
280                 user_message += _("  Save seems successful. Phew.\n");
281                 return user_message;
282         }
283
284         user_message += _("  Save failed! Bummer. Document is lost.");
285         return user_message;
286 }
287
288
289 bool BufferList::exists(FileName const & fname) const
290 {
291         return getBuffer(fname) != 0;
292 }
293
294
295 bool BufferList::isLoaded(Buffer const * b) const
296 {
297         BufferStorage::const_iterator cit =
298                 find(bstore.begin(), bstore.end(), b);
299         return cit != bstore.end();
300 }
301
302
303 Buffer * BufferList::getBuffer(support::FileName const & fname) const
304 {
305         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
306                 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
307         return it != bstore.end() ? (*it) : 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                         return *it;
318         return 0;
319 }
320
321
322 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
323 {
324         BufferStorage::iterator it = bstore.begin();
325         BufferStorage::iterator end = bstore.end();
326         for (; it != end; ++it)
327                 (*it)->params().authors().record(0, Author(name, email));
328 }
329
330
331 int BufferList::bufferNum(FileName const & fname) const
332 {
333         FileNameList const & buffers = fileNames();
334         FileNameList::const_iterator cit =
335                 find(buffers.begin(), buffers.end(), fname);
336         if (cit == buffers.end())
337                 return 0;
338         return int(cit - buffers.begin());
339 }
340
341
342 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
343 {
344         LASSERT(parent, return false);
345         LASSERT(child, return false);
346         LASSERT(parent->isChild(child), return false);
347
348         // Child document has a different parent, don't close it.
349         if (child->parent() != parent)
350                 return false;
351
352         BufferStorage::iterator it = bstore.begin();
353         BufferStorage::iterator end = bstore.end();
354         for (; it != end; ++it) {
355                 Buffer * buf = *it;
356                 if (buf != parent && buf->isChild(child)) {
357                         child->setParent(0);
358                         return false;
359                 }
360         }
361         release(child);
362         return true;
363 }
364
365
366 } // namespace lyx