1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
22 #include "converter.h"
24 #include "support/syscall.h"
25 #include "support/path.h"
29 #include "bufferview_funcs.h"
32 #include "minibuffer.h"
33 #include "lyx_gui_misc.h"
41 //////////////////////////////////////////////////////////////////////////////
43 map<string, Format> Formats::formats;
44 vector<Command> Converter::commands;
46 //////////////////////////////////////////////////////////////////////////////
48 Format::Format(string const & n)
49 : name(n), in_degree(0)
53 char const * prettyname;
58 { "ps", "PostScript" },
66 for (int i = 0; items[i].name != 0; ++i)
67 if (items[i].name == n) {
68 prettyname = items[i].prettyname;
74 void Formats::Add(string const & name)
76 if (formats.find(name) == formats.end())
77 formats[name] = Format(name);
81 void Formats::SetViewer(string const & name, string const & command)
84 Format * f = GetFormat(name);
85 if (!f->viewer.empty())
86 lyxerr << "Error: a viewer for " << name
87 << " is already defined!" << endl;
93 bool Formats::View(string const & filename)
95 string extension = GetExtension(filename);
96 Format * format = GetFormat(extension);
97 if (!format || format->viewer.empty()) {
98 WriteAlert(_("Can not view file"),
99 _("No information for viewing ")
100 + Formats::PrettyName(extension));
104 string command = format->viewer;
105 command = subst(command, "$$FName", filename);
109 // OS/2 cmd.exe has another use for '&'
110 // This is not NLS safe, but it's OK, I think.
111 string sh = OnlyFilename(GetEnvPath("EMXSHELL"));
113 // COMSPEC is set, unless user unsets
114 sh = OnlyFilename(GetEnvPath("COMSPEC"));
119 if (contains(sh, "cmd.exe")
120 || contains(sh, "4os2.exe"))
121 command = "start /min/n " + command;
125 lyxerr << "Executing command: " << command << endl;
126 //ShowMessage(buffer, _("Executing command:"), command);
128 int res = one.startscript(Systemcalls::System, command);
131 WriteAlert(_("Can not view file"),
132 _("Error while executing"),
133 command.substr(0, 50));
140 Format * Formats::GetFormat(string const & name)
142 map<string, Format>::iterator it = formats.find(name);
143 if (it != formats.end())
144 return &(*it).second;
149 string const Formats::PrettyName(string const & name)
152 Converter::SplitFormat(name, format);
153 Format * f = GetFormat(format);
155 return f->prettyname;
161 //////////////////////////////////////////////////////////////////////////////
162 void Converter::Add(string const & from, string const & to,
163 string const & command, string const & flags)
165 for (vector<Command>::const_iterator cit = commands.begin();
166 cit != commands.end(); ++cit)
167 if ((*cit).from == from && (*cit).to == to) {
168 lyxerr << "Error: Convertor from " << from
170 << " already exists!" << endl;
173 bool original_dir = flags == "origdir";
174 commands.push_back(Command(from, to, command, original_dir));
178 ++Formats::GetFormat(to)->in_degree;
182 vector< pair<string,string> > const
183 Converter::GetReachable(string const & from, bool only_viewable)
185 vector< pair<string,string> > result;
186 Format * format = Formats::GetFormat(from);
191 if (!only_viewable || !format->viewer.empty()) {
192 result.push_back(pair<string,string>(from, format->prettyname));
196 queue< vector<Command>::iterator > Q;
197 for (vector<Command>::iterator it = commands.begin();
198 it != commands.end(); ++it)
199 if ((*it).from == from) {
201 (*it).visited = true;
203 (*it).visited = false;
206 vector<Command>::iterator it = Q.front();
207 format = Formats::GetFormat((*it).to);
208 string name = format->name;
209 string prettyname = format->prettyname;
210 if (format->in_degree > 1) {
211 name += ":" + (*it).from;
213 split((*it).command, tmp, ' ');
214 prettyname += _("(using ") + tmp + ")";
216 if (!only_viewable || !format->viewer.empty())
217 result.push_back(pair<string,string>(name, prettyname));
219 for (vector<Command>::iterator it2 = commands.begin();
220 it2 != commands.end(); ++it2)
221 if (!(*it2).visited && (*it).to == (*it2).from) {
223 (*it2).visited = true;
227 sort(result.begin() + sort_start, result.end());
232 bool Converter::convert(Buffer * buffer, string const & from_file,
233 string const & to_format)
237 string using_format = SplitFormat(to_format, format);
238 string from_format = GetExtension(from_file);
239 if (from_format == format)
242 queue< vector<Command>::iterator > Q;
243 for (vector<Command>::iterator it = commands.begin();
244 it != commands.end(); ++it)
245 if ((*it).from == from_format) {
247 (*it).visited = true;
248 (*it).previous = commands.end();
250 (*it).visited = false;
253 WriteAlert(_("Can not convert file"),
254 ("Unknown format ") + from_format);
259 vector<Command>::iterator it;
262 if ((*it).to == format &&
263 (using_format.empty() || using_format == (*it).from)) {
268 for (vector<Command>::iterator it2 = commands.begin();
269 it2 != commands.end(); ++it2)
270 if (!(*it2).visited && (*it).to == (*it2).from) {
272 (*it2).visited = true;
273 (*it2).previous = it;
278 WriteAlert(_("Can not convert file"),
279 _("No information for converting from ")
280 + Formats::PrettyName(from_format) + _(" to ")
281 + Formats::PrettyName(to_format));
285 stack< vector<Command>::iterator > S;
286 while (it != commands.end()) {
291 //Path p(OnlyPath(buffer->fileName()));
292 Path p(OnlyPath(from_file));
294 string basename = ChangeExtension(from_file, "");
298 lyxerr << "Converting from "
299 << (*it).from << " to " << (*it).to << endl;
301 if ((*it).from == "tex" &&
302 ( (*it).to == "dvi" || (*it).to == "pdf") ) {
303 lyxrc.pdf_mode = (*it).to == "pdf";
304 if (!runLaTeX(buffer, (*it).command))
307 string infile = ChangeExtension(from_file, (*it).from);
308 if (!(*it).original_dir)
309 infile = OnlyFilename(infile);
310 string outfile = ChangeExtension(infile, (*it).to);
312 string command = (*it).command;
313 command = subst(command, "$$FName", infile);
314 command = subst(command, "$$BaseName", basename);
315 command = subst(command, "$$OutName", outfile);
316 lyxerr << "Calling " << command << endl;
317 ShowMessage(buffer, _("Executing command:"), command);
321 if ((*it).original_dir) {
322 Path p(buffer->filepath);
323 res = one.startscript(Systemcalls::System, command);
325 res = one.startscript(Systemcalls::System, command);
327 WriteAlert(_("Can not convert file"),
328 "Error while executing",
329 command.substr(0, 50));
339 string const Converter::SplitFormat(string const & str, string & format)
341 string using_format = split(str, format, ':');
348 bool Converter::runLaTeX(Buffer * buffer, string const & command)
351 BufferView * bv = buffer->getUser();
353 if (!bv->text) return 0;
357 string name = buffer->getLatexName();
359 bv->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
361 // Remove all error insets
362 bool a = bv->removeAutoInsets();
364 // do the LaTex run(s)
366 LaTeX latex(command, name, buffer->filepath);
367 int result = latex.run(terr,
368 bv->owner()->getMiniBuffer()); // running latex
370 if ((result & LaTeX::ERRORS)) {
371 // Insert all errors as errors boxes
372 bv->insertErrors(terr);
375 // if we removed error insets before we ran LaTeX or if we inserted
376 // error insets after we ran LaTeX this must be run:
377 if (a || (result & LaTeX::ERRORS)){
380 //bv->updateScrollbar();
383 // check return value from latex.run().
384 if ((result & LaTeX::NO_LOGFILE)) {
385 WriteAlert(_("LaTeX did not work!"),
386 _("Missing log file:"), name);
387 } else if ((result & LaTeX::ERRORS)) {
388 int num_errors = latex.getNumErrors();
391 if (num_errors == 1) {
392 s = _("One error detected");
393 t = _("You should try to fix it.");
395 s = tostr(num_errors);
396 s += _(" errors detected.");
397 t = _("You should try to fix them.");
399 WriteAlert(_("There were errors during the LaTeX run."),
404 return (result & (LaTeX::NO_LOGFILE | LaTeX::ERRORS)) == 0;