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