]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.C
The std::string mammoth path.
[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 "errorlist.h"
21 #include "gettext.h"
22 #include "LaTeX.h"
23 #include "paragraph.h"
24 #include "lyxvc.h"
25 #include "texrow.h"
26 #include "vc-backend.h"
27
28 #include "frontends/Alert.h"
29
30 #include "support/FileInfo.h"
31 #include "support/filetools.h"
32 #include "support/lyxlib.h"
33
34 using lyx::support::bformat;
35 using lyx::support::FileInfo;
36 using lyx::support::IsFileWriteable;
37 using lyx::support::LibFileSearch;
38 using lyx::support::MakeDisplayPath;
39 using lyx::support::OnlyFilename;
40 using lyx::support::OnlyPath;
41 using lyx::support::unlink;
42
43 using std::string;
44
45
46 extern BufferList bufferlist;
47
48 namespace {
49
50 bool readFile(Buffer * b, string const & s)
51 {
52         string ts(s);
53         string e = OnlyPath(s);
54         string a = e;
55         // File information about normal file
56         FileInfo fileInfo(s);
57
58         if (!fileInfo.exist()) {
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         e += OnlyFilename(s) + ".emergency";
68         FileInfo fileInfoE(e);
69
70         bool use_emergency = false;
71
72         if (fileInfoE.exist() && fileInfo.exist()) {
73                 if (fileInfoE.getModificationTime()
74                     > fileInfo.getModificationTime()) {
75                         string const file = MakeDisplayPath(s, 20);
76                         string text = bformat(_("An emergency save of the document %1$s exists.\n"
77                                 "\nRecover emergency save?"), file);
78                         int const ret = Alert::prompt(_("Load emergency save?"),
79                                 text, 0, 1, _("&Recover"), _("&Load Original"));
80
81                         if (ret == 0) {
82                                 ts = e;
83                                 // the file is not saved if we load the
84                                 // emergency file.
85                                 b->markDirty();
86                                 use_emergency = true;
87                         }
88                 }
89         }
90
91         if (!use_emergency) {
92                 // Now check if autosave file is newer.
93                 a += '#';
94                 a += OnlyFilename(s);
95                 a += '#';
96                 FileInfo fileInfoA(a);
97                 if (fileInfoA.exist() && fileInfo.exist()) {
98                         if (fileInfoA.getModificationTime()
99                             > fileInfo.getModificationTime()) {
100                                 string const file = MakeDisplayPath(s, 20);
101                                 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
102                                         "Load the backup instead?"), file);
103                                 int const ret = Alert::prompt(_("Load backup?"),
104                                         text, 0, 1, _("&Load backup"), _("Load &original"));
105
106                                 if (ret == 0) {
107                                         ts = a;
108                                         // the file is not saved if we load the
109                                         // autosave file.
110                                         b->markDirty();
111                                 } else {
112                                         // Here, we should delete the autosave
113                                         unlink(a);
114                                 }
115                         }
116                 }
117         }
118         return b->readFile(ts);
119 }
120
121
122 } // namespace anon
123
124
125
126 bool loadLyXFile(Buffer * b, string const & s)
127 {
128         switch (IsFileWriteable(s)) {
129         case 0:
130                 b->setReadonly(true);
131                 // Fall through
132         case 1:
133                 if (readFile(b, s)) {
134                         b->lyxvc().file_found_hook(s);
135                         return true;
136                 }
137                 break;
138         case -1:
139                 string const file = MakeDisplayPath(s, 20);
140                 // Here we probably should run
141                 if (LyXVC::file_not_found_hook(s)) {
142                         string text = bformat(_("Do you want to retrieve the document"
143                                 " %1$s from version control?"), file);
144                         int const ret = Alert::prompt(_("Retrieve from version control?"),
145                                 text, 0, 1, _("&Retrieve"), _("&Cancel"));
146
147                         if (ret == 0) {
148                                 // How can we know _how_ to do the checkout?
149                                 // With the current VC support it has to be,
150                                 // a RCS file since CVS do not have special ,v files.
151                                 RCS::retrieve(s);
152                                 return loadLyXFile(b, s);
153                         }
154                 }
155                 break;
156         }
157         return false;
158 }
159
160
161 Buffer * newFile(string const & filename, string const & templatename,
162                  bool isNamed)
163 {
164         // get a free buffer
165         Buffer * b = bufferlist.newBuffer(filename);
166
167         string tname;
168         // use defaults.lyx as a default template if it exists.
169         if (templatename.empty())
170                 tname = LibFileSearch("templates", "defaults.lyx");
171         else
172                 tname = templatename;
173
174         if (!tname.empty()) {
175                 if (!b->readFile(tname)) {
176                         string const file = MakeDisplayPath(tname, 50);
177                         string const text  = bformat(_("The specified document template\n%1$s\ncould not be read."), file);
178                         Alert::error(_("Could not read template"), text);
179                         // no template, start with empty buffer
180                         b->paragraphs().push_back(Paragraph());
181                         b->paragraphs().begin()->layout(b->params().getLyXTextClass().defaultLayout());
182                 }
183         } else {  // start with empty buffer
184                 b->paragraphs().push_back(Paragraph());
185                 b->paragraphs().begin()->layout(b->params().getLyXTextClass().defaultLayout());
186         }
187
188         if (!isNamed) {
189                 b->setUnnamed();
190                 b->setFileName(filename);
191         }
192
193         b->setReadonly(false);
194         b->updateDocLang(b->params().language);
195
196         return b;
197 }
198
199
200 void bufferErrors(Buffer const & buf, TeXErrors const & terr)
201 {
202         TeXErrors::Errors::const_iterator cit = terr.begin();
203         TeXErrors::Errors::const_iterator end = terr.end();
204
205         for (; cit != end; ++cit) {
206                 int par_id = -1;
207                 int posstart = -1;
208                 int const errorrow = cit->error_in_line;
209                 buf.texrow().getIdFromRow(errorrow, par_id, posstart);
210                 int posend = -1;
211                 buf.texrow().getIdFromRow(errorrow + 1, par_id, posend);
212                 buf.error(ErrorItem(cit->error_desc,
213                                          cit->error_text,
214                                          par_id, posstart, posend));
215         }
216 }
217
218
219 void bufferErrors(Buffer const & buf, ErrorList const & el)
220 {
221         ErrorList::const_iterator it = el.begin();
222         ErrorList::const_iterator end = el.end();
223
224         for (; it != end; ++it)
225                 buf.error(*it);
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 }