]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.C
- Link against qt-mt333.lib which is what the current qt3 cvs produces
[lyx.git] / src / buffer_funcs.C
1 /**
2  * \file buffer_funcs.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  * \author Alfredo Braunstein
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  */
12
13 #include <config.h>
14
15 #include "buffer_funcs.h"
16
17 #include "buffer.h"
18 #include "bufferlist.h"
19 #include "bufferparams.h"
20 #include "dociterator.h"
21 #include "errorlist.h"
22 #include "gettext.h"
23 #include "LaTeX.h"
24 #include "paragraph.h"
25 #include "lyxvc.h"
26 #include "texrow.h"
27 #include "vc-backend.h"
28
29 #include "frontends/Alert.h"
30
31 #include "support/filetools.h"
32 #include "support/fs_extras.h"
33 #include "support/lyxlib.h"
34
35 #include <boost/bind.hpp>
36 #include <boost/filesystem/operations.hpp>
37
38 using lyx::support::bformat;
39 using lyx::support::LibFileSearch;
40 using lyx::support::MakeDisplayPath;
41 using lyx::support::OnlyFilename;
42 using lyx::support::OnlyPath;
43 using lyx::support::unlink;
44
45 using std::string;
46
47 namespace fs = boost::filesystem;
48
49 extern BufferList bufferlist;
50
51 namespace {
52
53 bool readFile(Buffer * const b, string const & s)
54 {
55         BOOST_ASSERT(b);
56
57         // File information about normal file
58         if (!fs::exists(s)) {
59                 string const file = MakeDisplayPath(s, 50);
60                 string text = bformat(_("The specified document\n%1$s"
61                                         "\ncould not be read."), file);
62                 Alert::error(_("Could not read document"), text);
63                 return false;
64         }
65
66         // Check if emergency save file exists and is newer.
67         string const e = OnlyPath(s) + OnlyFilename(s) + ".emergency";
68
69         if (fs::exists(e) && fs::exists(s)
70             && fs::last_write_time(e) > fs::last_write_time(s))
71         {
72                 string const file = MakeDisplayPath(s, 20);
73                 string const text =
74                         bformat(_("An emergency save of the document "
75                                   "%1$s exists.\n\n"
76                                   "Recover emergency save?"), file);
77                 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
78                                       _("&Recover"),  _("&Load Original"),
79                                       _("&Cancel")))
80                 {
81                 case 0:
82                         // the file is not saved if we load the emergency file.
83                         b->markDirty();
84                         return b->readFile(e);
85                 case 1:
86                         break;
87                 default:
88                         return false;
89                 }
90         }
91
92         // Now check if autosave file is newer.
93         string const a = OnlyPath(s) + '#' + OnlyFilename(s) + '#';
94
95         if (fs::exists(a) && fs::exists(s)
96             && fs::last_write_time(a) > fs::last_write_time(s))
97         {
98                 string const file = MakeDisplayPath(s, 20);
99                 string const text =
100                         bformat(_("The backup of the document "
101                                   "%1$s is newer.\n\nLoad the "
102                                   "backup instead?"), file);
103                 switch (Alert::prompt(_("Load backup?"), text, 0, 2,
104                                       _("&Load backup"), _("Load &original"),
105                                       _("&Cancel") ))
106                 {
107                 case 0:
108                         // the file is not saved if we load the autosave file.
109                         b->markDirty();
110                         return b->readFile(a);
111                 case 1:
112                         // Here we delete the autosave
113                         unlink(a);
114                         break;
115                 default:
116                         return false;
117                 }
118         }
119         return b->readFile(s);
120 }
121
122
123 } // namespace anon
124
125
126
127 bool loadLyXFile(Buffer * b, string const & s)
128 {
129         BOOST_ASSERT(b);
130
131         if (fs::is_readable(s)) {
132                 if (readFile(b, s)) {
133                         b->lyxvc().file_found_hook(s);
134                         if (!fs::is_writable(s))
135                                 b->setReadonly(true);
136                         return true;
137                 }
138         } else {
139                 string const file = MakeDisplayPath(s, 20);
140                 // Here we probably should run
141                 if (LyXVC::file_not_found_hook(s)) {
142                         string const text =
143                                 bformat(_("Do you want to retrieve the document"
144                                           " %1$s from version control?"), file);
145                         int const ret = Alert::prompt(_("Retrieve from version control?"),
146                                 text, 0, 1, _("&Retrieve"), _("&Cancel"));
147
148                         if (ret == 0) {
149                                 // How can we know _how_ to do the checkout?
150                                 // With the current VC support it has to be,
151                                 // a RCS file since CVS do not have special ,v files.
152                                 RCS::retrieve(s);
153                                 return loadLyXFile(b, s);
154                         }
155                 }
156         }
157         return false;
158 }
159
160
161 Buffer * newFile(string const & filename, string const & templatename,
162                  bool const isNamed)
163 {
164         // get a free buffer
165         Buffer * b = bufferlist.newBuffer(filename);
166         BOOST_ASSERT(b);
167
168         string tname;
169         // use defaults.lyx as a default template if it exists.
170         if (templatename.empty())
171                 tname = LibFileSearch("templates", "defaults.lyx");
172         else
173                 tname = templatename;
174
175         if (!tname.empty()) {
176                 if (!b->readFile(tname)) {
177                         string const file = MakeDisplayPath(tname, 50);
178                         string const text  = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
179                         Alert::error(_("Could not read template"), text);
180                         // no template, start with empty buffer
181                 }
182         }
183
184         if (!isNamed) {
185                 b->setUnnamed();
186                 b->setFileName(filename);
187         }
188
189         b->setReadonly(false);
190         b->fully_loaded(true);
191         b->updateDocLang(b->params().language);
192
193         return b;
194 }
195
196
197 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
198 {
199         TeXErrors::Errors::const_iterator cit = terr.begin();
200         TeXErrors::Errors::const_iterator end = terr.end();
201
202         for (; cit != end; ++cit) {
203                 int par_id = -1;
204                 int posstart = -1;
205                 int const errorrow = cit->error_in_line;
206                 buf.texrow().getIdFromRow(errorrow, par_id, posstart);
207                 int posend = -1;
208                 buf.texrow().getIdFromRow(errorrow + 1, par_id, posend);
209                 buf.error(ErrorItem(cit->error_desc,
210                                          cit->error_text,
211                                          par_id, posstart, posend));
212         }
213 }
214
215
216 void bufferErrors(Buffer const & buf, ErrorList const & el)
217 {
218         for_each(el.begin(), el.end(), bind(ref(buf.error), _1));
219 }
220
221
222 string const BufferFormat(Buffer const & buffer)
223 {
224         if (buffer.isLinuxDoc())
225                 return "linuxdoc";
226         else if (buffer.isDocBook())
227                 return "docbook";
228         else if (buffer.isLiterate())
229                 return "literate";
230         else
231                 return "latex";
232 }
233
234
235 int countWords(DocIterator const & from, DocIterator const & to)
236 {
237         int count = 0;
238         bool inword = false;
239         for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
240                 // Copied and adapted from isLetter() in ControlSpellChecker
241                 if (dit.inTexted()
242                     && dit.pos() != dit.lastpos()
243                     && dit.paragraph().isLetter(dit.pos())
244                     && !isDeletedText(dit.paragraph(), dit.pos())) {
245                         if (!inword) {
246                                 ++count;
247                                 inword = true;
248                         }
249                 } else if (inword)
250                         inword = false;
251         }
252
253         return count;
254 }