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