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