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"
44 //////////////////////////////////////////////////////////////////////////////
46 map<string, Format> Formats::formats;
47 vector<Command> Converter::commands;
49 //////////////////////////////////////////////////////////////////////////////
51 Format::Format(string const & n)
52 : name(n), in_degree(0)
56 char const * prettyname;
61 { "ps", "PostScript" },
69 for (int i = 0; items[i].name != 0; ++i)
70 if (items[i].name == n) {
71 prettyname = items[i].prettyname;
77 void Formats::Add(string const & name)
79 if (formats.find(name) == formats.end())
80 formats[name] = Format(name);
84 void Formats::SetViewer(string const & name, string const & command)
87 Format * f = GetFormat(name);
88 if (!f->viewer.empty())
89 lyxerr << "Error: a viewer for " << name
90 << " is already defined!" << endl;
96 bool Formats::View(string const & filename)
98 string extension = GetExtension(filename);
99 Format * format = GetFormat(extension);
100 if (!format || format->viewer.empty()) {
101 WriteAlert(_("Can not view file"),
102 _("No information for viewing ")
103 + Formats::PrettyName(extension));
107 string command = format->viewer;
108 command = subst(command, "$$FName", filename);
112 // OS/2 cmd.exe has another use for '&'
113 // This is not NLS safe, but it's OK, I think.
114 string sh = OnlyFilename(GetEnvPath("EMXSHELL"));
116 // COMSPEC is set, unless user unsets
117 sh = OnlyFilename(GetEnvPath("COMSPEC"));
122 if (contains(sh, "cmd.exe")
123 || contains(sh, "4os2.exe"))
124 command = "start /min/n " + command;
128 lyxerr << "Executing command: " << command << endl;
129 //ShowMessage(buffer, _("Executing command:"), command);
131 int res = one.startscript(Systemcalls::System, command);
134 WriteAlert(_("Can not view file"),
135 _("Error while executing"),
136 command.substr(0, 50));
143 Format * Formats::GetFormat(string const & name)
145 map<string, Format>::iterator it = formats.find(name);
146 if (it != formats.end())
147 return &(*it).second;
152 string const Formats::PrettyName(string const & name)
155 Converter::SplitFormat(name, format);
156 Format * f = GetFormat(format);
158 return f->prettyname;
164 //////////////////////////////////////////////////////////////////////////////
165 void Converter::Add(string const & from, string const & to,
166 string const & command, string const & flags)
168 for (vector<Command>::const_iterator cit = commands.begin();
169 cit != commands.end(); ++cit)
170 if ((*cit).from == from && (*cit).to == to) {
171 lyxerr << "Error: Convertor from " << from
173 << " already exists!" << endl;
176 bool original_dir = flags == "origdir";
177 commands.push_back(Command(from, to, command, original_dir));
181 ++Formats::GetFormat(to)->in_degree;
185 vector< pair<string,string> > const
186 Converter::GetReachable(string const & from, bool only_viewable)
188 vector< pair<string,string> > result;
189 Format * format = Formats::GetFormat(from);
194 if (!only_viewable || !format->viewer.empty()) {
195 result.push_back(pair<string,string>(from, format->prettyname));
199 queue< vector<Command>::iterator > Q;
200 for (vector<Command>::iterator it = commands.begin();
201 it != commands.end(); ++it)
202 if ((*it).from == from) {
204 (*it).visited = true;
206 (*it).visited = false;
209 vector<Command>::iterator it = Q.front();
210 format = Formats::GetFormat((*it).to);
211 string name = format->name;
212 string prettyname = format->prettyname;
213 if (format->in_degree > 1) {
214 name += ":" + (*it).from;
216 split((*it).command, tmp, ' ');
217 prettyname += _("(using ") + tmp + ")";
219 if (!only_viewable || !format->viewer.empty())
220 result.push_back(pair<string,string>(name, prettyname));
222 for (vector<Command>::iterator it2 = commands.begin();
223 it2 != commands.end(); ++it2)
224 if (!(*it2).visited && (*it).to == (*it2).from) {
226 (*it2).visited = true;
230 sort(result.begin() + sort_start, result.end());
235 bool Converter::convert(Buffer * buffer, string const & from_file,
236 string const & to_format)
240 string using_format = SplitFormat(to_format, format);
241 string from_format = GetExtension(from_file);
242 if (from_format == format)
245 queue< vector<Command>::iterator > Q;
246 for (vector<Command>::iterator it = commands.begin();
247 it != commands.end(); ++it)
248 if ((*it).from == from_format) {
250 (*it).visited = true;
251 (*it).previous = commands.end();
253 (*it).visited = false;
256 WriteAlert(_("Can not convert file"),
257 ("Unknown format ") + from_format);
262 vector<Command>::iterator it;
265 if ((*it).to == format &&
266 (using_format.empty() || using_format == (*it).from)) {
271 for (vector<Command>::iterator it2 = commands.begin();
272 it2 != commands.end(); ++it2)
273 if (!(*it2).visited && (*it).to == (*it2).from) {
275 (*it2).visited = true;
276 (*it2).previous = it;
281 WriteAlert(_("Can not convert file"),
282 _("No information for converting from ")
283 + Formats::PrettyName(from_format) + _(" to ")
284 + Formats::PrettyName(to_format));
288 stack< vector<Command>::iterator > S;
289 while (it != commands.end()) {
294 //Path p(OnlyPath(buffer->fileName()));
295 Path p(OnlyPath(from_file));
297 string basename = ChangeExtension(from_file, "");
301 lyxerr << "Converting from "
302 << (*it).from << " to " << (*it).to << endl;
304 if ((*it).from == "tex" &&
305 ( (*it).to == "dvi" || (*it).to == "pdf") ) {
306 lyxrc.pdf_mode = (*it).to == "pdf";
307 if (!runLaTeX(buffer, (*it).command))
310 string infile = ChangeExtension(from_file, (*it).from);
311 if (!(*it).original_dir)
312 infile = OnlyFilename(infile);
313 string outfile = ChangeExtension(infile, (*it).to);
315 string command = (*it).command;
316 command = subst(command, "$$FName", infile);
317 command = subst(command, "$$BaseName", basename);
318 command = subst(command, "$$OutName", outfile);
319 lyxerr << "Calling " << command << endl;
320 ShowMessage(buffer, _("Executing command:"), command);
324 if ((*it).original_dir) {
325 Path p(buffer->filepath);
326 res = one.startscript(Systemcalls::System, command);
328 res = one.startscript(Systemcalls::System, command);
330 WriteAlert(_("Can not convert file"),
331 "Error while executing",
332 command.substr(0, 50));
342 string const Converter::SplitFormat(string const & str, string & format)
344 string using_format = split(str, format, ':');
351 bool Converter::runLaTeX(Buffer * buffer, string const & command)
354 BufferView * bv = buffer->getUser();
356 if (!bv->text) return 0;
360 string name = buffer->getLatexName();
362 bv->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
364 // Remove all error insets
365 bool a = bv->removeAutoInsets();
367 // do the LaTex run(s)
369 LaTeX latex(command, name, buffer->filepath);
370 int result = latex.run(terr,
371 bv->owner()->getMiniBuffer()); // running latex
373 if ((result & LaTeX::ERRORS)) {
374 // Insert all errors as errors boxes
375 bv->insertErrors(terr);
378 // if we removed error insets before we ran LaTeX or if we inserted
379 // error insets after we ran LaTeX this must be run:
380 if (a || (result & LaTeX::ERRORS)){
383 //bv->updateScrollbar();
386 // check return value from latex.run().
387 if ((result & LaTeX::NO_LOGFILE)) {
388 WriteAlert(_("LaTeX did not work!"),
389 _("Missing log file:"), name);
390 } else if ((result & LaTeX::ERRORS)) {
391 int num_errors = latex.getNumErrors();
394 if (num_errors == 1) {
395 s = _("One error detected");
396 t = _("You should try to fix it.");
398 s = tostr(num_errors);
399 s += _(" errors detected.");
400 t = _("You should try to fix them.");
402 WriteAlert(_("There were errors during the LaTeX run."),
407 return (result & (LaTeX::NO_LOGFILE | LaTeX::ERRORS)) == 0;