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