]> git.lyx.org Git - lyx.git/blob - src/BufferList.cpp
LogUi.ui: string redundancy.
[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 namespace {
304 struct equivalent_to : public binary_function<FileName, FileName, bool>
305 {
306         bool operator()(FileName const & x, FileName const & y) const
307         { return equivalent(x, y); }
308 };
309 }
310
311
312 Buffer * BufferList::getBuffer(support::FileName const & fname) const
313 {
314         // 1) cheap test, using string comparison of file names
315         BufferStorage::const_iterator it = find_if(bstore.begin(), bstore.end(),
316                 bind(equal_to<FileName>(), bind(&Buffer::fileName, _1), fname));
317         if (it != bstore.end())
318                         return *it;
319         // 2) possibly expensive test, using quivalence test of file names
320         it = find_if(bstore.begin(), bstore.end(),
321                 bind(equivalent_to(), bind(&Buffer::fileName, _1), fname));
322         return it != bstore.end() ? (*it) : 0;
323 }
324
325
326 Buffer * BufferList::getBufferFromTmp(string const & s)
327 {
328         BufferStorage::iterator it = bstore.begin();
329         BufferStorage::iterator end = bstore.end();
330         for (; it < end; ++it) {
331                 if (prefixIs(s, (*it)->temppath())) {
332                         // check whether the filename matches the master
333                         string const master_name = changeExtension(onlyFilename(
334                                                 (*it)->absFileName()), ".tex");
335                         if (suffixIs(s, master_name))
336                                 return *it;
337                         // if not, try with the children
338                         vector<Buffer *> clist = (*it)->getChildren();
339                         vector<Buffer *>::const_iterator cit = clist.begin();
340                         vector<Buffer *>::const_iterator cend = clist.end();
341                         for (; cit < cend; ++cit) {
342                                 string const mangled_child_name = DocFileName(
343                                         changeExtension((*cit)->absFileName(),
344                                                 ".tex")).mangledFilename();
345                                 if (suffixIs(s, mangled_child_name))
346                                         return *cit;
347                         }
348                 }
349         }
350         return 0;
351 }
352
353
354 void BufferList::setCurrentAuthor(docstring const & name, docstring const & email)
355 {
356         BufferStorage::iterator it = bstore.begin();
357         BufferStorage::iterator end = bstore.end();
358         for (; it != end; ++it)
359                 (*it)->params().authors().record(0, Author(name, email));
360 }
361
362
363 int BufferList::bufferNum(FileName const & fname) const
364 {
365         FileNameList const & buffers = fileNames();
366         FileNameList::const_iterator cit =
367                 find(buffers.begin(), buffers.end(), fname);
368         if (cit == buffers.end())
369                 return 0;
370         return int(cit - buffers.begin());
371 }
372
373
374 bool BufferList::releaseChild(Buffer * parent, Buffer * child)
375 {
376         LASSERT(parent, return false);
377         LASSERT(child, return false);
378         LASSERT(parent->isChild(child), return false);
379
380         // Child document has a different parent, don't close it.
381         if (child->parent() != parent)
382                 return false;
383
384         BufferStorage::iterator it = bstore.begin();
385         BufferStorage::iterator end = bstore.end();
386         for (; it != end; ++it) {
387                 Buffer * buf = *it;
388                 if (buf != parent && buf->isChild(child)) {
389                         child->setParent(0);
390                         return false;
391                 }
392         }
393         release(child);
394         return true;
395 }
396
397
398 } // namespace lyx