3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
17 #include "GuiApplication.h"
18 #include "qt_helpers.h"
21 #include "frontends/Clipboard.h"
23 #include "support/docstring.h"
24 #include "support/FileName.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
28 #include <QDesktopServices>
29 #include <QTextBrowser>
30 #include <QSyntaxHighlighter>
38 using namespace lyx::support;
44 // Regular expressions needed at several places
45 // FIXME: These regexes are incomplete. It would be good if we could collect those used in LaTeX::scanLogFile
46 // and LaTeX::scanBlgFile and re-use them here!(spitz, 2013-05-27)
48 QRegExp exprInfo("^(Document Class:|LaTeX Font Info:|File:|Package:|Language:|Underfull|Overfull|.*> INFO - |\\(|\\\\).*$");
50 QRegExp exprWarning("^(LaTeX Warning|LaTeX Font Warning|Package [\\w\\.]+ Warning|Class \\w+ Warning|Warning--|.*> WARN - ).*$");
52 QRegExp exprError("^(!|.*---line [0-9]+ of file|.*> FATAL - |.*> ERROR - |Missing character: There is no ).*$");
55 /////////////////////////////////////////////////////////////////////
59 ////////////////////////////////////////////////////////////////////
61 class LogHighlighter : public QSyntaxHighlighter
64 LogHighlighter(QTextDocument * parent);
67 void highlightBlock(QString const & text);
70 QTextCharFormat infoFormat;
71 QTextCharFormat warningFormat;
72 QTextCharFormat errorFormat;
77 LogHighlighter::LogHighlighter(QTextDocument * parent)
78 : QSyntaxHighlighter(parent)
80 infoFormat.setForeground(Qt::darkGray);
81 warningFormat.setForeground(Qt::darkBlue);
82 errorFormat.setForeground(Qt::red);
86 void LogHighlighter::highlightBlock(QString const & text)
89 int index = exprInfo.indexIn(text);
91 int length = exprInfo.matchedLength();
92 setFormat(index, length, infoFormat);
93 index = exprInfo.indexIn(text, index + length);
96 index = exprWarning.indexIn(text);
98 int length = exprWarning.matchedLength();
99 setFormat(index, length, warningFormat);
100 index = exprWarning.indexIn(text, index + length);
103 index = exprError.indexIn(text);
105 int length = exprError.matchedLength();
106 setFormat(index, length, errorFormat);
107 index = exprError.indexIn(text, index + length);
112 /////////////////////////////////////////////////////////////////////
116 /////////////////////////////////////////////////////////////////////
118 GuiLog::GuiLog(GuiView & lv)
119 : GuiDialog(lv, "log", qt_("LaTeX Log")), type_(LatexLog)
123 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
124 connect(updatePB, SIGNAL(clicked()), this, SLOT(updateContents()));
125 connect(findPB, SIGNAL(clicked()), this, SLOT(find()));
126 // FIXME: find via returnPressed() does not work!
127 connect(findLE, SIGNAL(returnPressed()), this, SLOT(find()));
128 connect(logTypeCO, SIGNAL(activated(int)),
129 this, SLOT(typeChanged(int)));
131 bc().setPolicy(ButtonPolicy::OkCancelPolicy);
133 // set syntax highlighting
134 highlighter = new LogHighlighter(logTB->document());
136 logTB->setReadOnly(true);
137 logTB->setFont(guiApp->typewriterSystemFont());
141 void GuiLog::updateContents()
143 setTitle(toqstr(title()));
148 logTB->setPlainText(toqstr(ss.str()));
150 nextErrorPB->setEnabled(contains(exprError));
151 nextWarningPB->setEnabled(contains(exprWarning));
155 void GuiLog::typeChanged(int i)
158 fromqstr(logTypeCO->itemData(i).toString());
162 else if (type == "bibtex")
164 else if (type == "index")
168 logfile_.changeExtension(ext);
176 logTB->find(findLE->text());
180 void GuiLog::on_nextErrorPB_clicked()
186 void GuiLog::on_nextWarningPB_clicked()
192 void GuiLog::on_openDirPB_clicked()
194 support::FileName dir = logfile_.onlyPath();
197 QUrl qdir(QUrl::fromLocalFile(toqstr(from_utf8(dir.absFileName()))));
198 // Give hints in case of bugs
199 if (!qdir.isValid()) {
200 LYXERR0("QUrl is invalid!");
203 if (!QDesktopServices::openUrl(qdir))
204 LYXERR0("Unable to open QUrl even though dir exists!");
208 void GuiLog::goTo(QRegExp const & exp) const
210 QTextCursor const newc =
211 logTB->document()->find(exp, logTB->textCursor());
212 logTB->setTextCursor(newc);
216 bool GuiLog::contains(QRegExp const & exp) const
218 return !logTB->document()->find(exp, logTB->textCursor()).isNull();
222 bool GuiLog::initialiseParams(string const & data)
224 istringstream is(data);
228 string logtype, logfile;
232 logfile = lex.getString();
235 // Parsing of the data failed.
238 logTypeCO->setEnabled(logtype == "latex");
241 FileName log(logfile);
243 if (logtype == "latex") {
245 logTypeCO->addItem(qt_("LaTeX"), toqstr(logtype));
247 tmp.changeExtension("blg");
249 if (support::contains(tmp.fileContents("UTF-8"), from_ascii("This is Biber")))
250 logTypeCO->addItem(qt_("Biber"), QString("bibtex"));
252 logTypeCO->addItem(qt_("BibTeX"), QString("bibtex"));
254 tmp.changeExtension("ilg");
256 logTypeCO->addItem(qt_("Index"), QString("index"));
257 // FIXME: not sure "literate" still works.
258 } else if (logtype == "literate") {
260 logTypeCO->addItem(qt_("Literate"), toqstr(logtype));
261 } else if (logtype == "lyx2lyx") {
263 logTypeCO->addItem(qt_("LyX2LyX"), toqstr(logtype));
264 } else if (logtype == "vc") {
266 logTypeCO->addItem(qt_("Version Control"), toqstr(logtype));
278 void GuiLog::clearParams()
284 docstring GuiLog::title() const
288 return _("LaTeX Log");
290 return _("Literate Programming Build Log");
292 return _("lyx2lyx Error Log");
294 return _("Version Control Log");
301 void GuiLog::getContents(ostream & ss) const
303 ifstream in(logfile_.toFilesystemEncoding().c_str());
305 bool success = false;
308 // Our caller interprets the file contents as UTF8, but is that
320 ss << to_utf8(_("Log file not found."));
323 ss << to_utf8(_("No literate programming build log file found."));
326 ss << to_utf8(_("No lyx2lyx error log file found."));
329 ss << to_utf8(_("No version control log file found."));
335 Dialog * createGuiLog(GuiView & lv) { return new GuiLog(lv); }
338 } // namespace frontend
341 #include "moc_GuiLog.cpp"