diff --git a/src/backends/maxima/maximacompletionobject.cpp b/src/backends/maxima/maximacompletionobject.cpp index b85eac22..9559307d 100644 --- a/src/backends/maxima/maximacompletionobject.cpp +++ b/src/backends/maxima/maximacompletionobject.cpp @@ -1,82 +1,79 @@ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2009-2012 Alexander Rieder */ #include "maximacompletionobject.h" #include #include "maximasession.h" #include "maximakeywords.h" #include "maximavariablemodel.h" MaximaCompletionObject::MaximaCompletionObject(const QString& command, int index,MaximaSession* session) : Cantor::CompletionObject(session) { qDebug() << "MaximaCompletionObject constructor"; setLine(command, index); } void MaximaCompletionObject::fetchIdentifierType() { - MaximaVariableModel* model=static_cast(session()->variableModel()); - QStringList userVariableNames=model->variableNames(); - QStringList userFunctionNames=model->functionNames(true); + QStringList userVariableNames=session()->variableModel()->variableNames(); + QStringList userFunctionNames=session()->variableModel()->functions(); if (qBinaryFind(userVariableNames.begin(), userVariableNames.end(), identifier()) != userVariableNames.end()) emit fetchingTypeDone(VariableType); else if (qBinaryFind(userFunctionNames.begin(), userFunctionNames.end(), identifier()) != userFunctionNames.end()) emit fetchingTypeDone(FunctionType); else if (qBinaryFind(MaximaKeywords::instance()->functions().begin(), MaximaKeywords::instance()->functions().end(), identifier()) != MaximaKeywords::instance()->functions().end()) emit fetchingTypeDone(FunctionType); else if (qBinaryFind(MaximaKeywords::instance()->keywords().begin(), MaximaKeywords::instance()->keywords().end(), identifier()) != MaximaKeywords::instance()->keywords().end()) emit fetchingTypeDone(KeywordType); else emit fetchingTypeDone(VariableType); } void MaximaCompletionObject::fetchCompletions() { - MaximaVariableModel* model=static_cast(session()->variableModel()); - QStringList allCompletions; allCompletions<variables(); allCompletions<functions(); allCompletions<keywords(); - allCompletions<variableNames(); - allCompletions<functionNames(true); + allCompletions<variableModel()->variableNames(); + allCompletions<variableModel()->functions(); setCompletions(allCompletions); emit fetchingDone(); } bool MaximaCompletionObject::mayIdentifierContain(QChar c) const { return c.isLetter() || c.isDigit() || c == QLatin1Char('_') || c == QLatin1Char('%'); } bool MaximaCompletionObject::mayIdentifierBeginWith(QChar c) const { return c.isLetter() || c == QLatin1Char('_') || c == QLatin1Char('%'); } diff --git a/src/backends/maxima/maximasession.cpp b/src/backends/maxima/maximasession.cpp index ddfbccd1..0ecdb8ad 100644 --- a/src/backends/maxima/maximasession.cpp +++ b/src/backends/maxima/maximasession.cpp @@ -1,333 +1,332 @@ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2009-2012 Alexander Rieder Copyright (C) 2017-2018 Alexander Semke (alexander.semke@web.de) */ #include "maximasession.h" #include "maximaexpression.h" #include "maximacompletionobject.h" #include "maximasyntaxhelpobject.h" #include "maximahighlighter.h" #include "maximavariablemodel.h" #include "result.h" #include "settings.h" #include #include #include #include #include #ifndef Q_OS_WIN #include #endif //NOTE: the \\s in the expressions is needed, because Maxima seems to sometimes insert newlines/spaces between the letters //maybe this is caused by some behaviour if the Prompt is split into multiple "readStdout" calls //the Expressions are encapsulated in () to allow capturing for the text const QRegExp MaximaSession::MaximaOutputPrompt=QRegExp(QLatin1String("(\\(\\s*%\\s*o\\s*[0-9\\s]*\\))")); //Text, maxima outputs, before any output const QRegExp MaximaSession::MaximaInputPrompt = QRegExp(QLatin1String("(\\(\\s*%\\s*i\\s*[0-9\\s]*\\))")); MaximaSession::MaximaSession( Cantor::Backend* backend ) : Session(backend), m_process(nullptr), m_justRestarted(false) { setVariableModel(new MaximaVariableModel(this)); } void MaximaSession::login() { qDebug()<<"login"; if (m_process) return; //TODO: why do we call login() again?!? emit loginStarted(); QStringList arguments; arguments << QLatin1String("--quiet"); //Suppress Maxima start-up message const QString initFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("cantor/maximabackend/cantor-initmaxima.lisp")); arguments << QLatin1String("--init-lisp=") + initFile; //Set the name of the Lisp initialization file m_process = new QProcess(this); m_process->start(MaximaSettings::self()->path().toLocalFile(), arguments); m_process->waitForStarted(); QString input; // Wait until first maxima prompt while (!input.contains(QLatin1String(""))) { m_process->waitForReadyRead(); input += QString::fromLatin1(m_process->readAllStandardOutput()); qDebug() << input; } connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(restartMaxima())); connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut())); connect(m_process, SIGNAL(readyReadStandardError()), this, SLOT(readStdErr())); connect(m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(reportProcessError(QProcess::ProcessError))); if(!MaximaSettings::self()->autorunScripts().isEmpty()){ QString autorunScripts = MaximaSettings::self()->autorunScripts().join(QLatin1String(";")); autorunScripts.append(QLatin1String(";kill(labels)")); // Reset labels after running autorun scripts evaluateExpression(autorunScripts, MaximaExpression::DeleteOnFinish, true); forceVariableUpdate(); } changeStatus(Session::Done); emit loginDone(); qDebug()<<"login done"; } void MaximaSession::logout() { qDebug()<<"logout"; if(!m_process) return; disconnect(m_process, nullptr, this, nullptr); // if(status()==Cantor::Session::Running) //TODO: terminate the running expressions first write(QLatin1String("quit();\n")); qDebug()<<"waiting for maxima to finish"; m_process->waitForFinished(); qDebug()<<"maxima exit finished"; if(m_process->state() != QProcess::NotRunning) { m_process->kill(); qDebug()<<"maxima still running, process kill enforced"; } expressionQueue().clear(); delete m_process; m_process = nullptr; - MaximaVariableModel* model = static_cast(variableModel()); - model->clearVariables(); - model->clearFunctions(); + variableModel()->clearVariables(); + variableModel()->clearFunctions(); changeStatus(Status::Disable); qDebug()<<"logout done"; } Cantor::Expression* MaximaSession::evaluateExpression(const QString& cmd, Cantor::Expression::FinishingBehavior behave, bool internal) { qDebug() << "evaluating: " << cmd; MaximaExpression* expr = new MaximaExpression(this, internal); expr->setFinishingBehavior(behave); expr->setCommand(cmd); expr->evaluate(); return expr; } void MaximaSession::readStdErr() { qDebug()<<"reading stdErr"; if (!m_process) return; QString out=QLatin1String(m_process->readAllStandardError()); if(expressionQueue().size()>0) { MaximaExpression* expr = static_cast(expressionQueue().first()); expr->parseError(out); } } void MaximaSession::readStdOut() { QString out = QLatin1String(m_process->readAllStandardOutput()); m_cache += out; //collect the multi-line output until Maxima has finished the calculation and returns a new promt if ( !out.contains(QLatin1String("")) ) return; if(expressionQueue().isEmpty()) { //queue is empty, interrupt was called, nothing to do here qDebug()<(expressionQueue().first()); if (!expr) return; //should never happen qDebug()<<"output: " << m_cache; expr->parseOutput(m_cache); m_cache.clear(); } void MaximaSession::reportProcessError(QProcess::ProcessError e) { qDebug()<<"process error"<command(); qDebug() << "expression status changed: command = " << expression->command() << ", status = " << status; switch (status) { case Cantor::Expression::Done: case Cantor::Expression::Error: qDebug()<<"################################## EXPRESSION END ###############################################"; disconnect(expression, SIGNAL(statusChanged(Cantor::Expression::Status)), this, SLOT(currentExpressionChangedStatus(Cantor::Expression::Status))); finishFirstExpression(); break; default: break; } } void MaximaSession::runFirstExpression() { qDebug()<<"running next expression"; if (!m_process) return; if(!expressionQueue().isEmpty()) { MaximaExpression* expr = static_cast(expressionQueue().first()); QString command=expr->internalCommand(); connect(expr, SIGNAL(statusChanged(Cantor::Expression::Status)), this, SLOT(currentExpressionChangedStatus(Cantor::Expression::Status))); expr->setStatus(Cantor::Expression::Computing); if(command.isEmpty()) { qDebug()<<"empty command"; expr->forceDone(); } else { m_cache.clear(); write(command + QLatin1Char('\n')); } } } void MaximaSession::interrupt() { if(!expressionQueue().isEmpty()) { qDebug()<<"interrupting " << expressionQueue().first()->command(); if(m_process->state() != QProcess::NotRunning) { #ifndef Q_OS_WIN const int pid=m_process->pid(); kill(pid, SIGINT); #else ; //TODO: interrupt the process on windows #endif } foreach (Cantor::Expression* expression, expressionQueue()) expression->setStatus(Cantor::Expression::Interrupted); expressionQueue().clear(); qDebug()<<"done interrupting"; } changeStatus(Cantor::Session::Done); m_cache.clear(); } void MaximaSession::sendInputToProcess(const QString& input) { write(input); } void MaximaSession::restartMaxima() { qDebug()<<"restarting maxima cooldown: "<write(exp.toUtf8()); } diff --git a/src/backends/maxima/maximavariablemodel.cpp b/src/backends/maxima/maximavariablemodel.cpp index abfd6a07..31436616 100644 --- a/src/backends/maxima/maximavariablemodel.cpp +++ b/src/backends/maxima/maximavariablemodel.cpp @@ -1,198 +1,182 @@ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2012 Alexander Rieder */ #include "maximavariablemodel.h" #include "maximasession.h" #include "maximaexpression.h" #include "textresult.h" #include "latexresult.h" #include #include #include "settings.h" //command used to inspect a maxima variable. %1 is the name of that variable const QString MaximaVariableModel::inspectCommand=QLatin1String(":lisp($disp $%1)"); const QString MaximaVariableModel::variableInspectCommand=QLatin1String(":lisp(cantor-inspect $%1)"); MaximaVariableModel::MaximaVariableModel( MaximaSession* session) : Cantor::DefaultVariableModel(session), m_variableExpression(nullptr), m_functionExpression(nullptr) { } void MaximaVariableModel::update() { if (!m_variableExpression) { qDebug()<<"checking for new variables"; const QString& cmd1=variableInspectCommand.arg(QLatin1String("values")); m_variableExpression = session()->evaluateExpression(cmd1, Cantor::Expression::FinishingBehavior::DoNotDelete, true); connect(m_variableExpression, &Cantor::Expression::statusChanged, this, &MaximaVariableModel::parseNewVariables); } if (!m_functionExpression) { qDebug()<<"checking for new functions"; const QString& cmd2=inspectCommand.arg(QLatin1String("functions")); m_functionExpression = session()->evaluateExpression(cmd2, Cantor::Expression::FinishingBehavior::DoNotDelete, true); connect(m_functionExpression, &Cantor::Expression::statusChanged, this, &MaximaVariableModel::parseNewFunctions); } } QList parse(MaximaExpression* expr) { if(!expr || (expr->status()!=Cantor::Expression::Done && expr->errorMessage() != QLatin1String("$DONE")) || expr->results().isEmpty()) { return QList(); } //for parsing of names and values below (old code) we need to combine multiple results back to one string QString text; for (auto* result : expr->results()) { if(result->type()==Cantor::TextResult::Type) text += dynamic_cast(result)->plain(); else if(expr->result()->type()==Cantor::LatexResult::Type) text += dynamic_cast(result)->plain(); } const int nameIndex=text.indexOf(QLatin1Char(']')); QString namesString=text.left(nameIndex); //namesString.chop(1); namesString=namesString.mid(1); namesString=namesString.trimmed(); qDebug()<<"variable names: "<(); QStringList variableNames; QString valuesString; bool hasValues = false; QStringList variableValues; if ( namesString.contains(QLatin1Char(')')) ) { //function definition(s): e.g //text = "[f1(x),f2(x,y),f3(x,y,z)]\n$DONE" //nameString = f1(x),f2(x,y),f3(x,y,z) //variableString = "\n$DONE" variableNames = namesString.split(QLatin1String("),")); } else { //variable definition(s): e.g. //text = "[a,b]\n1\n\"-cantor-value-separator-\"\n2\n\"-cantor-value-separator-\"\n($A $B)" //nameString = "[a,b]" //variableString = "\n1\n\"-cantor-value-separator-\"\n2\n\"-cantor-value-separator-\"\n($A $B)" variableNames = namesString.split(QLatin1Char(',')); if (MaximaSettings::self()->variableManagement()) { valuesString = text.mid(nameIndex+1).trimmed(); valuesString = valuesString.remove(QLatin1String("\n")); //lists with many elements have line breaks, remove them variableValues = valuesString.split(QLatin1String("\"-cantor-value-separator-\"")); hasValues = variableValues.isEmpty(); } } qDebug()< variables; variables.reserve(variableNames.size()); for(int i=0;ii) { var.value=variableValues.at(i).trimmed(); var.value=var.value.remove(QLatin1String("\n")); //lists with many elements have line breaks, remove them } else var.value=QLatin1String("unknown"); variables< newVars=parse(static_cast(m_variableExpression)); setVariables(newVars); //the expression is not needed anymore m_variableExpression->deleteLater(); m_variableExpression = nullptr; } void MaximaVariableModel::parseNewFunctions(Cantor::Expression::Status status) { if (status != Cantor::Expression::Done && status != Cantor::Expression::Error) return; qDebug()<<"parsing functions"; // List of variables? QList newFuncs=parse(static_cast(m_functionExpression)); QStringList functions; for (Variable var : newFuncs) functions << var.name.left(var.name.indexOf(QLatin1Char('('))); qDebug() << functions; setFunctions(functions); //the expression is not needed anymore m_functionExpression->deleteLater(); m_functionExpression = nullptr; } MaximaSession* MaximaVariableModel::maximaSession() { return static_cast (session()); } - -QStringList MaximaVariableModel::functionNames(bool stripParameters) -{ - QStringList names; - for (const QString func: functions()) - { - QString name=func; - if(stripParameters) - { - name=name.left(name.lastIndexOf(QLatin1Char('('))); - } - names< */ #ifndef _MAXIMAVARIABLEMODEL_H #define _MAXIMAVARIABLEMODEL_H #include "defaultvariablemodel.h" #include class MaximaSession; class MaximaVariableModel : public Cantor::DefaultVariableModel { Q_OBJECT public: static const QString inspectCommand; static const QString variableInspectCommand; explicit MaximaVariableModel( MaximaSession* session); ~MaximaVariableModel() override = default; - QStringList functionNames(bool stripParameters=false); - void update() override; private Q_SLOTS: void parseNewVariables(Cantor::Expression::Status status); void parseNewFunctions(Cantor::Expression::Status status); private: MaximaSession* maximaSession(); private: Cantor::Expression* m_variableExpression; Cantor::Expression* m_functionExpression; }; #endif /* _MAXIMAVARIABLEMODEL_H */