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