File indexing completed on 2024-04-28 11:20:33

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009-2012 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #include "maximasyntaxhelpobject.h"
0007 #include "maximakeywords.h"
0008 
0009 #include "maximasession.h"
0010 #include "maximaexpression.h"
0011 
0012 #include "result.h"
0013 
0014 #include <QDebug>
0015 
0016 MaximaSyntaxHelpObject::MaximaSyntaxHelpObject(const QString& cmd, MaximaSession* session) : Cantor::SyntaxHelpObject(cmd, session)
0017 {
0018     m_expression=nullptr;
0019 }
0020 
0021 void MaximaSyntaxHelpObject::fetchInformation()
0022 {
0023     bool isValid=false;
0024     for (const QString& func : MaximaKeywords::instance()->functions())
0025     {
0026         if(command()==func)
0027         {
0028             isValid=true;
0029             break;
0030         }
0031     }
0032 
0033     if(isValid)
0034     {
0035         if (session()->status() != Cantor::Session::Disable)
0036         {
0037             if (m_expression)
0038                 return;
0039 
0040             //use the lisp command, instead of directly calling the
0041             //maxima function "describe" to avoid generating a new
0042             //output label that would mess up history
0043             QString cmd=QLatin1String(":lisp(cl-info::info-exact \"%1\")");
0044 
0045             m_expression=session()->evaluateExpression(cmd.arg(command()), Cantor::Expression::FinishingBehavior::DoNotDelete, true);
0046 
0047             connect(m_expression, &Cantor::Expression::statusChanged, this, &MaximaSyntaxHelpObject::expressionChangedStatus);
0048         }
0049         else
0050             // We can't get function's detailed description, because session not login yet, so do nothing
0051             emit done();
0052 
0053     }else
0054     {
0055         qDebug()<<"invalid syntax request";
0056         emit done();
0057     }
0058 }
0059 
0060 void MaximaSyntaxHelpObject::expressionChangedStatus(Cantor::Expression::Status status)
0061 {
0062     switch(status)
0063     {
0064         case Cantor::Expression::Done:
0065         {
0066             qDebug()<<"expr done";
0067             QString text=m_expression->result()->data().toString();
0068             QStringList lines=text.split(QLatin1Char('\n'));
0069 
0070             QString syntax;
0071             for (QString line : lines)
0072             {
0073                 if(line.endsWith(QLatin1Char('\r')))
0074                     line.chop(1);
0075                 if(line.startsWith(QLatin1String("-- Function:")))
0076                 {
0077                     line.remove(QLatin1String("-- Function:"));
0078                     line.remove(QLatin1String("<br/>"));
0079                 }
0080                 syntax+=line;
0081                 qDebug() << "line: " << line;
0082             }
0083 
0084             setHtml(QLatin1String("<p style='white-space:pre'>")+syntax+QLatin1String("</p>"));
0085             emit done();
0086 
0087             m_expression->deleteLater();
0088             m_expression=nullptr;
0089             break;
0090         }
0091         case Cantor::Expression::Error:
0092         {
0093             qWarning() << "syntax object error" << m_expression->result()->toHtml();
0094             emit done();
0095 
0096             m_expression->deleteLater();
0097             m_expression=nullptr;
0098             break;
0099         }
0100         default:
0101             break;
0102     }
0103 }