]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.C
added a parseError signal to Buffer and use it
[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 "vc-backend.h"
20 #include "lyxlex.h"
21 #include "ParagraphList.h"
22 #include "paragraph.h"
23
24 #include "frontends/Alert.h"
25
26 #include "support/filetools.h"
27 #include "support/FileInfo.h"
28 #include "support/lyxlib.h"
29
30
31 extern BufferList bufferlist;
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                                         lyx::unlink(a);
100                                 }
101                         }
102                 }
103         }
104         // not sure if this is the correct place to begin LyXLex
105         LyXLex lex(0, 0);
106         lex.setFile(ts);
107
108         return b->readFile(lex, ts);
109 }
110
111
112 } // namespace anon
113
114
115
116 bool loadLyXFile(Buffer * b, string const & s)
117 {
118         switch (IsFileWriteable(s)) {
119         case 0:
120                 b->setReadonly(true);
121                 // Fall through
122         case 1:
123                 if (readFile(b, s)) {
124                         b->lyxvc.file_found_hook(s);
125                         return true;
126                 }
127                 break; 
128         case -1: 
129                 string const file = MakeDisplayPath(s, 20);
130                 // Here we probably should run
131                 if (LyXVC::file_not_found_hook(s)) {
132                         string text = bformat(_("Do you want to retrieve the document"
133                                 " %1$s from version control?"), file);
134                         int const ret = Alert::prompt(_("Retrieve from version control?"),
135                                 text, 0, 1, _("&Retrieve"), _("&Cancel"));
136
137                         if (ret == 0) {
138                                 // How can we know _how_ to do the checkout?
139                                 // With the current VC support it has to be,
140                                 // a RCS file since CVS do not have special ,v files.
141                                 RCS::retrieve(s);
142                                 return loadLyXFile(b, s);
143                         }
144                 }
145                 break;
146         }
147         return false;
148 }
149
150
151 Buffer * newFile(string const & filename, string const & templatename, 
152                  bool isNamed)
153 {
154         // get a free buffer
155         Buffer * b = bufferlist.newBuffer(filename);
156
157         string tname;
158         // use defaults.lyx as a default template if it exists.
159         if (templatename.empty())
160                 tname = LibFileSearch("templates", "defaults.lyx");
161         else
162                 tname = templatename;
163
164         if (!tname.empty()) {
165                 bool templateok = false;
166                 LyXLex lex(0, 0);
167                 lex.setFile(tname);
168                 if (lex.isOK()) {
169                         if (b->readFile(lex, tname)) {
170                                 templateok = true;
171                         }
172                 }
173                 if (!templateok) {
174                         string const file = MakeDisplayPath(tname, 50);
175                         string text  = bformat(_("The specified document template\n%1$s\n"
176                                 "could not be read."), file);
177                         Alert::error(_("Could not read template"), text);
178                         // no template, start with empty buffer
179                         b->paragraphs.push_back(Paragraph());
180                         b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
181                 }
182         } else {  // start with empty buffer
183                 b->paragraphs.push_back(Paragraph());
184                 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
185         }
186
187         if (!isNamed) {
188                 b->setUnnamed();
189                 b->setFileName(filename);
190         }
191
192         b->setReadonly(false);
193         b->updateDocLang(b->params.language);
194
195         return b;
196 }