1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
#include "help.h"
#include "texdocdialog.h"
#include "smallUsefulFunctions.h"
#include "utilsSystem.h"
#include "configmanager.h"
#include <QProcessEnvironment>
#include <QMutex>
Help *Help::m_Instance = 0;
Help::Help() :
QObject(0)
{
}
QStringList Help::getAdditionalCmdSearchPathList()
{
return ConfigManagerInterface::getInstance()->getOption("Tools/Search Paths").toString().split(getPathListSeparator());
}
void Help::execTexdocDialog(const QStringList &packages, const QString &defaultPackage)
{
TexdocDialog dialog;
dialog.setPackageNames(packages);
if (!defaultPackage.isEmpty()) {
dialog.setPreferredPackage(defaultPackage);
}
if (dialog.exec()) {
viewTexdoc(dialog.selectedPackage());
QString package = dialog.selectedPackage();
}
}
void Help::viewTexdoc(QString package)
{
if (package.isEmpty()) {
QAction *act = qobject_cast<QAction *>(sender());
if (!act) return;
package = act->data().toString();
}
if (!package.isEmpty()) {
if (texdocCommand().isEmpty()) txsWarning(tr("texdoc not found."));
QProcess *proc = new QProcess(this);
connect(proc, SIGNAL(finished(int)), this, SLOT(texdocProcessFinished()));
#ifdef Q_OS_OSX
QStringList paths;
paths.append(getEnvironmentPathList());
paths.append(getAdditionalCmdSearchPathList());
updatePathSettings(proc, paths.join(':'));
#endif
proc->start(texdocCommand(), QStringList() << "--view" << package);
if (isTexdocExpectedToFinish() && !proc->waitForFinished(2000)) {
txsWarning(QString(tr("texdoc took too long to open the documentation for the package:") + "\n%1").arg(package));
}
}
}
int Help::texDocSystem = 0;
bool Help::isMiktexTexdoc()
{
if (!texDocSystem && !texdocCommand().isEmpty()) {
QProcess proc;
proc.start(texdocCommand(), QStringList() << "--version");
proc.waitForFinished(1000);
QString answer = QString(proc.readAll());
texDocSystem = answer.startsWith("MiKTeX") ? 1 : 2;
}
return (texDocSystem == 1);
}
bool Help::isTexdocExpectedToFinish()
{
if (!isMiktexTexdoc()) return true;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
foreach (const QString &var, envKeys(env)) {
if (var.startsWith("MIKTEX_VIEW_")) {
// miktex texdoc will run as long as the viewer is opened when the MIKTEX_VIEW_* variables are set
// http://docs.miktex.org/manual/mthelp.html
return false;
}
}
return true;
}
QString Help::m_texdocCommand;
QString Help::texdocCommand()
{
if (m_texdocCommand.isEmpty()) {
QStringList paths;
paths.append(getEnvironmentPathList());
paths.append(getAdditionalCmdSearchPathList());
#ifdef Q_OS_WIN
QString cmd = findAbsoluteFilePath("texdoc", "exe", paths, "not_found");
#else
QString cmd = findAbsoluteFilePath("texdoc", "", paths, "not_found");
#endif
m_texdocCommand = cmd.startsWith("not_found") ? "" : cmd;
}
return m_texdocCommand;
}
QString Help::packageDocFile(const QString &package, bool silent)
{
QString cmd = texdocCommand();
if (cmd.isEmpty()) {
if (!silent) txsWarning(tr("texdoc not found."));
return QString();
}
QStringList args;
if (Help::isMiktexTexdoc()) {
args << "--list-only";
} else {
args << "--list" << "--machine";
}
args << package;
QProcess proc;
#ifdef Q_OS_OSX
QStringList paths;
paths.append(getEnvironmentPathList());
paths.append(getAdditionalCmdSearchPathList());
updatePathSettings(&proc, paths.join(':'));
#endif
proc.start(cmd, args);
if (!proc.waitForFinished(2000)) {
if (!silent) {
txsWarning(QString(tr("texdoc did not respond to query on package:") + "\n%1").arg(package));
}
return QString();
}
QString output = proc.readAllStandardOutput();
QStringList allFiles;
if (Help::isMiktexTexdoc()) {
allFiles = output.split("\r\n");
} else {
foreach (const QString &line, output.split("\n")) {
QStringList cols = line.simplified().split(" ");
if (cols.count() > 2)
allFiles << cols.at(2);
}
}
foreach (const QString &file, allFiles) {
if (file.endsWith(".pdf") || file.endsWith(".dvi"))
return file;
}
return QString();
}
void Help::texdocAvailableRequest(const QString &package)
{
if (package.isEmpty())
return;
if (texdocCommand().isEmpty()) {
emit texdocAvailableReply(package, false, tr("texdoc not found."));
return;
}
QStringList args;
if (isMiktexTexdoc()) {
args << "--print-only" << package;
} else {
args << "--list" << "--machine"; // --print-only does not exist in texlive 2012, actual is response is not used either ...
// TODO: not the right option: don't open the viewer here
// There seems to be no option yielding only the would be called command
// Alternative: texdoc --list -M and parse the first line for the package name
}
QProcess *proc = new QProcess(this);
proc->setProperty("package", package);
connect(proc, SIGNAL(finished(int)), SLOT(texdocAvailableRequestFinished(int)));
#ifdef Q_OS_OSX
QStringList paths;
paths.append(getEnvironmentPathList());
paths.append(getAdditionalCmdSearchPathList());
updatePathSettings(proc, paths.join(':'));
#endif
proc->start(texdocCommand(), args);
}
void Help::texdocAvailableRequestFinished(int exitCode)
{
Q_UNUSED(exitCode);
QProcess *proc = qobject_cast<QProcess *>(sender());
if (!proc) return;
QString package = proc->property("package").toString();
QString docCommand = proc->readAll();
emit texdocAvailableReply(package, !docCommand.isEmpty(), QString());
proc->deleteLater();
}
void Help::texdocProcessFinished()
{
QProcess *proc = qobject_cast<QProcess *>(sender());
if (proc) {
QString message(proc->readAllStandardError().trimmed());
if (!message.isEmpty())
txsWarning(message);
proc->deleteLater();
}
}
Help *Help::instance()
{
static QMutex mutex;
mutex.lock();
if (!m_Instance)
m_Instance = new Help();
mutex.unlock();
return m_Instance;
}
LatexReference::LatexReference(QObject *parent) : QObject(parent) {}
void LatexReference::setFile(QString filename)
{
m_filename = filename;
if (filename.isEmpty()) return;
QFile f(filename);
if (!f.open(QFile::ReadOnly | QFile::Text)) return;
QTime t;
t.start();
QTextStream stream(&f);
stream.setCodec("UTF-8");
m_htmltext = stream.readAll();
makeIndex();
}
bool LatexReference::contains(const QString &command)
{
return m_anchors.contains(command);
}
/* tries to generate a text of suitable length for display as a tooltip */
QString LatexReference::getTextForTooltip(const QString &command)
{
QString sectionText = getSectionText(command);
QString partialText;
if (sectionText.count('\n') > 30) { // tooltip would be very large: try to get a reasonable smaller string
if (command.startsWith("\\begin{")) {
return truncateLines(sectionText, 30);
} else {
partialText = getPartialText(command);
if (!partialText.isEmpty()) return partialText;
}
}
return sectionText;
}
/* get all the text in the section describing the command
* it starts with the first heading after the section anchor and ranges down to the next <hr>
*/
QString LatexReference::getSectionText(const QString &command)
{
Anchor sAnchor(m_sectionAnchors[command]);
if (sAnchor.name.isNull()) return QString();
if (sAnchor.start_pos < 0) {
sAnchor.start_pos = m_htmltext.indexOf(QString("<a name=\"%1\">").arg(sAnchor.name));
sAnchor.start_pos = m_htmltext.indexOf("<h", sAnchor.start_pos); // skip header div by going to next headline
}
if (sAnchor.start_pos < 0) return QString();
if (sAnchor.end_pos < 0) {
QString endTag("<hr>");
sAnchor.end_pos = m_htmltext.indexOf(endTag, sAnchor.start_pos);
m_sectionAnchors.insert(command, sAnchor); // save positions for a faster lookup next time
}
return m_htmltext.mid(sAnchor.start_pos, sAnchor.end_pos - sAnchor.start_pos);
}
/* get only a partial description for the command
* the serach looks for the following block types (sqare brackets mark the extrated text:
* [<dt>(anchor-in-here)</dt><dd></dd>]
* </div>[(anchor-in-here)]</a name=
*/
QString LatexReference::getPartialText(const QString &command)
{
static QRegExp startTag("<(dt|/div)>");
QString endTag;
int endOffset = 0;
Anchor anchor(m_anchors[command]);
if (anchor.name.isNull()) return QString();
if (anchor.start_pos < 0) {
anchor.start_pos = m_htmltext.indexOf(QString("<a name=\"%1\">").arg(anchor.name));
anchor.start_pos = m_htmltext.lastIndexOf(startTag, anchor.start_pos);
if (startTag.cap(1) == "dt") {
endTag = "</dd>";
endOffset = endTag.length();
} else {
anchor.start_pos += QString("</div>").length();
endTag = "</p>";
}
}
if (anchor.start_pos < 0) return QString();
if (anchor.end_pos < 0) {
anchor.end_pos = m_htmltext.indexOf(endTag, anchor.start_pos + 1);
if (anchor.end_pos >= 0) anchor.end_pos += endOffset;
int hrEnd = m_htmltext.indexOf("<hr>", anchor.start_pos + 1);
if (hrEnd >= 0 && hrEnd < anchor.end_pos) // don't go further than the next <hr>
anchor.end_pos = hrEnd;
m_anchors.insert(command, anchor); // save positions for a faster lookup next time
}
return m_htmltext.mid(anchor.start_pos, anchor.end_pos - anchor.start_pos);
}
/* parses the index of the reference manual and extracts the anchor names for the commands */
void LatexReference::makeIndex()
{
QString startTag("<table class=\"index-fn\"");
QString endTag("</table>");
int start = m_htmltext.indexOf(startTag);
if (start < 0) return;
int end = m_htmltext.indexOf(endTag, start);
int length = end < 0 ? -1 : end - start + endTag.length();
QString indexText = m_htmltext.mid(start, length);
QRegExp rx("<a href=\"#([^\"]+)\"><code>([^\\s<]+)[^\n]*<a href=\"#([^\"]+)\">([^<]+)</a>");
int pos = 0;
while (pos >= 0) {
pos = indexText.indexOf(rx, pos);
if (pos < 0) break;
QString anchorName(rx.cap(1));
QString word(rx.cap(2));
QString sectionAnchorName(rx.cap(3));
QString sectionTitle(rx.cap(4));
if (word.startsWith('\\')) { // a command
if (word == "\\begin" || word == "\\") {
// don't add these words to the index because they give a mess in the tooltips
pos += rx.matchedLength();
continue;
}
m_anchors.insert(word, Anchor(anchorName));
m_sectionAnchors.insert(word, Anchor(sectionAnchorName));
} else if (anchorName.contains("environment")) { // an environment
m_anchors.insert("\\begin{" + word, Anchor(anchorName));
m_sectionAnchors.insert("\\begin{" + word, Anchor(sectionAnchorName));
} else {
// TODO: anything useful in the rest?
//qDebug() << word << anchorName << sectionAnchorName << sectionTitle;
}
pos += rx.matchedLength();
}
//qDebug() << "Found entries in index:" << m_anchors.count();
}
|