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