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