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