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