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