]> git.lyx.org Git - lyx.git/blob - src/bufferlist.C
ws changes only
[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 #include "TextCache.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
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                 // Make sure that we don't store a LyXText in
122                 // the textcache that points to the buffer
123                 // we just deleted.
124                 Buffer * tmp = (*it);
125                 bstore.erase(it);
126                 textcache.removeAllWithBuffer(tmp);
127                 delete tmp;
128         }
129 }
130
131
132 Buffer * BufferList::newBuffer(string const & s, bool ronly)
133 {
134         Buffer * tmpbuf = new Buffer(s, ronly);
135         tmpbuf->params().useClassDefaults();
136         lyxerr[Debug::INFO] << "Assigning to buffer "
137                             << bstore.size() << endl;
138         bstore.push_back(tmpbuf);
139         return tmpbuf;
140 }
141
142
143 void BufferList::closeAll()
144 {
145         // Since we are closing we can just as well delete all
146         // in the textcache this will also speed the closing/quiting up a bit.
147         textcache.clear();
148
149         while (!bstore.empty()) {
150                 close(bstore.front(), false);
151         }
152 }
153
154
155 bool BufferList::close(Buffer * buf, bool ask)
156 {
157         BOOST_ASSERT(buf);
158
159         // FIXME: is the quitting check still necessary ?
160         if (!ask || buf->isClean() || quitting || buf->paragraphs().empty()) {
161                 release(buf);
162                 return true;
163         }
164
165         string fname;
166         if (buf->isUnnamed())
167                 fname = OnlyFilename(buf->fileName());
168         else
169                 fname = MakeDisplayPath(buf->fileName(), 30);
170
171         string text = bformat(_("The document %1$s has unsaved changes.\n\n"
172                 "Do you want to save the document or discard the changes?"), fname);
173         int const ret = Alert::prompt(_("Save changed document?"),
174                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
175
176         if (ret == 0) {
177                 if (buf->isUnnamed()) {
178                         if (!WriteAs(buf))
179                                 return false;
180                 } else if (buf->save()) {
181                         lastfiles->newFile(buf->fileName());
182                 } else {
183                         return false;
184                 }
185         } else if (ret == 2) {
186                 return false;
187         }
188
189         if (buf->isUnnamed()) {
190                 removeAutosaveFile(buf->fileName());
191         }
192
193         release(buf);
194         return true;
195 }
196
197
198 vector<string> const BufferList::getFileNames() const
199 {
200         vector<string> nvec;
201         std::copy(bstore.begin(), bstore.end(),
202                   lyx::back_inserter_fun(nvec, &Buffer::fileName));
203         return nvec;
204 }
205
206
207 Buffer * BufferList::first()
208 {
209         if (bstore.empty())
210                 return 0;
211         return bstore.front();
212 }
213
214
215 Buffer * BufferList::getBuffer(unsigned int choice)
216 {
217         if (choice >= bstore.size())
218                 return 0;
219         return bstore[choice];
220 }
221
222
223 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
224                                         LatexRunParams const & runparams)
225 {
226         BufferStorage::iterator it = bstore.begin();
227         BufferStorage::iterator end = bstore.end();
228         for (; it != end; ++it) {
229                 if (!(*it)->isDepClean(mastertmpdir)) {
230                         string writefile = mastertmpdir;
231                         writefile += '/';
232                         writefile += (*it)->getLatexName();
233                         (*it)->makeLaTeXFile(writefile, mastertmpdir,
234                                              runparams, false);
235                         (*it)->markDepClean(mastertmpdir);
236                 }
237         }
238 }
239
240
241 void BufferList::emergencyWriteAll()
242 {
243         for_each(bstore.begin(), bstore.end(),
244                  boost::bind(&BufferList::emergencyWrite, this, _1));
245 }
246
247
248 void BufferList::emergencyWrite(Buffer * buf)
249 {
250         // assert(buf) // this is not good since C assert takes an int
251                        // and a pointer is a long (JMarc)
252         assert(buf != 0); // use c assert to avoid a loop
253
254
255         // No need to save if the buffer has not changed.
256         if (buf->isClean())
257                 return;
258
259         string const doc = buf->isUnnamed()
260                 ? OnlyFilename(buf->fileName()) : buf->fileName();
261
262         lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
263
264         // We try to save three places:
265         // 1) Same place as document. Unless it is an unnamed doc.
266         if (!buf->isUnnamed()) {
267                 string s = buf->fileName();
268                 s += ".emergency";
269                 lyxerr << "  " << s << endl;
270                 if (buf->writeFile(s)) {
271                         buf->markClean();
272                         lyxerr << _("  Save seems successful. Phew.") << endl;
273                         return;
274                 } else {
275                         lyxerr << _("  Save failed! Trying...") << endl;
276                 }
277         }
278
279         // 2) In HOME directory.
280         string s = AddName(GetEnvPath("HOME"), buf->fileName());
281         s += ".emergency";
282         lyxerr << ' ' << s << endl;
283         if (buf->writeFile(s)) {
284                 buf->markClean();
285                 lyxerr << _("  Save seems successful. Phew.") << endl;
286                 return;
287         }
288
289         lyxerr << _("  Save failed! Trying...") << endl;
290
291         // 3) In "/tmp" directory.
292         // MakeAbsPath to prepend the current
293         // drive letter on OS/2
294         s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
295         s += ".emergency";
296         lyxerr << ' ' << s << endl;
297         if (buf->writeFile(s)) {
298                 buf->markClean();
299                 lyxerr << _("  Save seems successful. Phew.") << endl;
300                 return;
301         }
302         lyxerr << _("  Save failed! Bummer. Document is lost.") << endl;
303 }
304
305
306 bool BufferList::exists(string const & s) const
307 {
308         return find_if(bstore.begin(), bstore.end(),
309                        lyx::compare_memfun(&Buffer::fileName, s))
310                 != bstore.end();
311 }
312
313
314 bool BufferList::isLoaded(Buffer const * b) const
315 {
316         BOOST_ASSERT(b);
317         BufferStorage::const_iterator cit =
318                 find(bstore.begin(), bstore.end(), b);
319         return cit != bstore.end();
320 }
321
322
323 Buffer * BufferList::getBuffer(string const & s)
324 {
325         BufferStorage::iterator it =
326                 find_if(bstore.begin(), bstore.end(),
327                         lyx::compare_memfun(&Buffer::fileName, s));
328         return it != bstore.end() ? (*it) : 0;
329 }
330
331
332 void BufferList::setCurrentAuthor(string const & name, string const & email)
333 {
334         BufferStorage::iterator it = bstore.begin();
335         BufferStorage::iterator end = bstore.end();
336         for (; it != end; ++it) {
337                 (*it)->params().authors().record(0, Author(name, email));
338         }
339 }