]> git.lyx.org Git - lyx.git/blob - src/bufferlist.C
some integer type changes for inset unification
[lyx.git] / src / bufferlist.C
1 /**
2  * \file bufferlist.C
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 "debug.h"
19 #include "gettext.h"
20 #include "lastfiles.h"
21 #include "lyx_cb.h"
22 #include "lyx_main.h"
23 #include "output_latex.h"
24 #include "paragraph.h"
25
26 #include "frontends/Alert.h"
27
28 #include "support/filetools.h"
29 #include "support/lyxfunctional.h"
30
31 #include <boost/bind.hpp>
32
33 using lyx::support::AddName;
34 using lyx::support::bformat;
35 using lyx::support::GetEnvPath;
36 using lyx::support::MakeAbsPath;
37 using lyx::support::MakeDisplayPath;
38 using lyx::support::OnlyFilename;
39 using lyx::support::removeAutosaveFile;
40 using lyx::support::prefixIs;
41
42 using std::auto_ptr;
43 using std::endl;
44 using std::find;
45 using std::find_if;
46 using std::for_each;
47 using std::string;
48 using std::vector;
49
50
51 BufferList::BufferList()
52 {}
53
54
55 bool BufferList::empty() const
56 {
57         return bstore.empty();
58 }
59
60
61 bool BufferList::quitWriteBuffer(Buffer * buf)
62 {
63         string file;
64         if (buf->isUnnamed())
65                 file = OnlyFilename(buf->fileName());
66         else
67                 file = MakeDisplayPath(buf->fileName(), 30);
68
69         string text = bformat(_("The document %1$s has unsaved changes.\n\n"
70                 "Do you want to save the document or discard the changes?"), file);
71         int const ret = Alert::prompt(_("Save changed document?"),
72                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
73
74         if (ret == 0) {
75                 // FIXME: WriteAs can be asynch !
76                 // but not right now...maybe we should remove that
77
78                 bool succeeded;
79
80                 if (buf->isUnnamed())
81                         succeeded = WriteAs(buf);
82                 else
83                         succeeded = MenuWrite(buf);
84
85                 if (!succeeded)
86                         return false;
87         } else if (ret == 1) {
88                 // if we crash after this we could
89                 // have no autosave file but I guess
90                 // this is really inprobable (Jug)
91                 if (buf->isUnnamed())
92                         removeAutosaveFile(buf->fileName());
93
94         } else {
95                 return false;
96         }
97
98         return true;
99 }
100
101
102 bool BufferList::quitWriteAll()
103 {
104         BufferStorage::iterator it = bstore.begin();
105         BufferStorage::iterator end = bstore.end();
106         for (; it != end; ++it) {
107                 if ((*it)->isClean())
108                         continue;
109
110                 if (!quitWriteBuffer(*it))
111                         return false;
112         }
113
114         return true;
115 }
116
117
118 void BufferList::release(Buffer * buf)
119 {
120         BOOST_ASSERT(buf);
121         BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
122         if (it != bstore.end()) {
123                 Buffer * tmp = (*it);
124                 bstore.erase(it);
125                 delete tmp;
126         }
127 }
128
129
130 Buffer * BufferList::newBuffer(string const & s, bool ronly)
131 {
132         auto_ptr<Buffer> tmpbuf(new Buffer(s, ronly));
133         tmpbuf->params().useClassDefaults();
134         lyxerr[Debug::INFO] << "Assigning to buffer "
135                             << bstore.size() << endl;
136         bstore.push_back(tmpbuf.get());
137         return tmpbuf.release();
138 }
139
140
141 void BufferList::closeAll()
142 {
143         while (!bstore.empty()) {
144                 close(bstore.front(), false);
145         }
146 }
147
148
149 bool BufferList::close(Buffer * buf, bool ask)
150 {
151         BOOST_ASSERT(buf);
152
153         // FIXME: is the quitting check still necessary ?
154         if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
155                 release(buf);
156                 return true;
157         }
158
159         string fname;
160         if (buf->isUnnamed())
161                 fname = OnlyFilename(buf->fileName());
162         else
163                 fname = MakeDisplayPath(buf->fileName(), 30);
164
165         string text = bformat(_("The document %1$s has unsaved changes.\n\n"
166                 "Do you want to save the document or discard the changes?"), fname);
167         int const ret = Alert::prompt(_("Save changed document?"),
168                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
169
170         if (ret == 0) {
171                 if (buf->isUnnamed()) {
172                         if (!WriteAs(buf))
173                                 return false;
174                 } else if (buf->save()) {
175                         LyX::ref().lastfiles().newFile(buf->fileName());
176                 } else {
177                         return false;
178                 }
179         } else if (ret == 2) {
180                 return false;
181         }
182
183         if (buf->isUnnamed()) {
184                 removeAutosaveFile(buf->fileName());
185         }
186
187         release(buf);
188         return true;
189 }
190
191
192 vector<string> const BufferList::getFileNames() const
193 {
194         vector<string> nvec;
195         std::copy(bstore.begin(), bstore.end(),
196                   lyx::back_inserter_fun(nvec, &Buffer::fileName));
197         return nvec;
198 }
199
200
201 Buffer * BufferList::first()
202 {
203         if (bstore.empty())
204                 return 0;
205         return bstore.front();
206 }
207
208
209 Buffer * BufferList::getBuffer(unsigned int choice)
210 {
211         if (choice >= bstore.size())
212                 return 0;
213         return bstore[choice];
214 }
215
216
217 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
218                                         OutputParams const & runparams)
219 {
220         BufferStorage::iterator it = bstore.begin();
221         BufferStorage::iterator end = bstore.end();
222         for (; it != end; ++it) {
223                 if (!(*it)->isDepClean(mastertmpdir)) {
224                         string writefile = mastertmpdir;
225                         writefile += '/';
226                         writefile += (*it)->getLatexName();
227                         (*it)->makeLaTeXFile(writefile, mastertmpdir,
228                                              runparams, false);
229                         (*it)->markDepClean(mastertmpdir);
230                 }
231         }
232 }
233
234
235 void BufferList::emergencyWriteAll()
236 {
237         for_each(bstore.begin(), bstore.end(),
238                  boost::bind(&BufferList::emergencyWrite, this, _1));
239 }
240
241
242 void BufferList::emergencyWrite(Buffer * buf)
243 {
244         // assert(buf) // this is not good since C assert takes an int
245                        // and a pointer is a long (JMarc)
246         assert(buf != 0); // use c assert to avoid a loop
247
248
249         // No need to save if the buffer has not changed.
250         if (buf->isClean())
251                 return;
252
253         string const doc = buf->isUnnamed()
254                 ? OnlyFilename(buf->fileName()) : buf->fileName();
255
256         lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
257
258         // We try to save three places:
259         // 1) Same place as document. Unless it is an unnamed doc.
260         if (!buf->isUnnamed()) {
261                 string s = buf->fileName();
262                 s += ".emergency";
263                 lyxerr << "  " << s << endl;
264                 if (buf->writeFile(s)) {
265                         buf->markClean();
266                         lyxerr << _("  Save seems successful. Phew.") << endl;
267                         return;
268                 } else {
269                         lyxerr << _("  Save failed! Trying...") << endl;
270                 }
271         }
272
273         // 2) In HOME directory.
274         string s = AddName(GetEnvPath("HOME"), buf->fileName());
275         s += ".emergency";
276         lyxerr << ' ' << s << endl;
277         if (buf->writeFile(s)) {
278                 buf->markClean();
279                 lyxerr << _("  Save seems successful. Phew.") << endl;
280                 return;
281         }
282
283         lyxerr << _("  Save failed! Trying...") << endl;
284
285         // 3) In "/tmp" directory.
286         // MakeAbsPath to prepend the current
287         // drive letter on OS/2
288         s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
289         s += ".emergency";
290         lyxerr << ' ' << s << endl;
291         if (buf->writeFile(s)) {
292                 buf->markClean();
293                 lyxerr << _("  Save seems successful. Phew.") << endl;
294                 return;
295         }
296         lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
297 }
298
299
300 bool BufferList::exists(string const & s) const
301 {
302         return find_if(bstore.begin(), bstore.end(),
303                        lyx::compare_memfun(&Buffer::fileName, s))
304                 != bstore.end();
305 }
306
307
308 bool BufferList::isLoaded(Buffer const * b) const
309 {
310         BOOST_ASSERT(b);
311         BufferStorage::const_iterator cit =
312                 find(bstore.begin(), bstore.end(), b);
313         return cit != bstore.end();
314 }
315
316
317 Buffer * BufferList::getBuffer(string const & s)
318 {
319         BufferStorage::iterator it =
320                 find_if(bstore.begin(), bstore.end(),
321                         lyx::compare_memfun(&Buffer::fileName, s));
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                         return *it;
333         return 0;
334 }
335
336
337 void BufferList::setCurrentAuthor(string const & name, string const & email)
338 {
339         BufferStorage::iterator it = bstore.begin();
340         BufferStorage::iterator end = bstore.end();
341         for (; it != end; ++it) {
342                 (*it)->params().authors().record(0, Author(name, email));
343         }
344 }