1999-09-27 18:44:28 +00:00
|
|
|
/*
|
2001-02-14 19:35:25 +00:00
|
|
|
* File: formula.C
|
|
|
|
* Purpose: Implementation of formula inset
|
|
|
|
* Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
|
|
|
|
* Created: January 1996
|
|
|
|
* Description: Allows the edition of math paragraphs inside Lyx.
|
|
|
|
*
|
|
|
|
* Copyright: 1996-1998 Alejandro Aguilar Sierra
|
|
|
|
*
|
|
|
|
* Version: 0.4, Lyx project.
|
|
|
|
*
|
|
|
|
* You are free to use and modify this code under the terms of
|
|
|
|
* the GNU General Public Licence version 2 or later.
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
2001-02-15 12:22:01 +00:00
|
|
|
#pragma implementation
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
|
2001-07-13 09:54:32 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "formula.h"
|
|
|
|
#include "commandtags.h"
|
|
|
|
#include "math_cursor.h"
|
|
|
|
#include "math_parser.h"
|
2001-10-18 13:21:21 +00:00
|
|
|
#include "math_charinset.h"
|
2001-11-07 13:15:59 +00:00
|
|
|
#include "math_arrayinset.h"
|
|
|
|
#include "math_deliminset.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "lyx_main.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "gettext.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "lyx_gui_misc.h"
|
1999-12-07 00:44:53 +00:00
|
|
|
#include "support/LOstream.h"
|
2001-10-12 12:02:49 +00:00
|
|
|
#include "support/LAssert.h"
|
2001-04-25 15:43:57 +00:00
|
|
|
#include "support/lyxlib.h"
|
|
|
|
#include "support/syscall.h"
|
2001-07-29 17:39:01 +00:00
|
|
|
#include "support/lstrings.h"
|
2001-10-24 16:10:38 +00:00
|
|
|
#include "support/filetools.h" // LibFileSearch
|
1999-12-19 22:35:36 +00:00
|
|
|
#include "LyXView.h"
|
2000-02-10 17:53:36 +00:00
|
|
|
#include "Painter.h"
|
2000-12-11 09:46:09 +00:00
|
|
|
#include "lyxrc.h"
|
2001-11-08 12:28:33 +00:00
|
|
|
#include "math_hullinset.h"
|
2001-11-08 12:15:12 +00:00
|
|
|
#include "math_support.h"
|
2001-11-08 12:06:56 +00:00
|
|
|
#include "math_mathmlstream.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
using std::ostream;
|
2001-04-25 15:43:57 +00:00
|
|
|
using std::ifstream;
|
2000-04-04 00:19:15 +00:00
|
|
|
using std::istream;
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::pair;
|
|
|
|
using std::endl;
|
2000-05-19 16:46:01 +00:00
|
|
|
using std::vector;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
2001-10-22 15:37:49 +00:00
|
|
|
namespace {
|
|
|
|
|
2001-11-07 08:51:35 +00:00
|
|
|
string captureOutput(string const & cmd, string const & data)
|
|
|
|
{
|
|
|
|
string outfile = lyx::tempName(string(), "mathextern");
|
2001-11-07 14:14:04 +00:00
|
|
|
string full = "echo '" + data + "' | (" + cmd + ") > " + outfile;
|
2001-11-07 08:51:35 +00:00
|
|
|
lyxerr << "calling: " << full << "\n";
|
|
|
|
Systemcalls dummy(Systemcalls::System, full, 0);
|
|
|
|
string out = GetFileContents(outfile);
|
2001-11-15 09:51:57 +00:00
|
|
|
lyx::unlink(outfile);
|
2001-11-07 14:14:04 +00:00
|
|
|
lyxerr << "result: '" << out << "'\n";
|
2001-11-07 08:51:35 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-07 13:15:59 +00:00
|
|
|
MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
|
2001-11-07 08:51:35 +00:00
|
|
|
{
|
2001-11-07 17:30:26 +00:00
|
|
|
string header = "readlib(latex):\n";
|
|
|
|
|
|
|
|
// remove the \\it for variable names
|
2001-11-07 08:51:35 +00:00
|
|
|
//"#`latex/csname_font` := `\\it `:"
|
2001-11-07 17:30:26 +00:00
|
|
|
header +=
|
|
|
|
"`latex/csname_font` := ``:\n";
|
|
|
|
|
|
|
|
// export matrices in (...) instead of [...]
|
|
|
|
header +=
|
|
|
|
"`latex/latex/matrix` := "
|
|
|
|
"subs(`[`=`(`, `]`=`)`,"
|
|
|
|
"eval(`latex/latex/matrix`)):\n";
|
|
|
|
|
|
|
|
// replace \\cdots with proper '*'
|
|
|
|
header +=
|
|
|
|
"`latex/latex/*` := "
|
|
|
|
"subs(`\\,`=`\\cdot `,"
|
|
|
|
"eval(`latex/latex/*`)):\n";
|
|
|
|
|
2001-11-09 08:35:57 +00:00
|
|
|
// remove spurious \\noalign{\\medskip} in matrix output
|
2001-11-08 12:06:56 +00:00
|
|
|
header +=
|
|
|
|
"`latex/latex/matrix`:= "
|
|
|
|
"subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
|
|
|
|
"eval(`latex/latex/matrix`)):\n";
|
|
|
|
|
2001-11-07 08:51:35 +00:00
|
|
|
//"#`latex/latex/symbol` "
|
|
|
|
// " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
|
|
|
|
|
|
|
|
string trailer = "quit;";
|
2001-11-07 17:30:26 +00:00
|
|
|
ostringstream os;
|
|
|
|
MapleStream ms(os);
|
|
|
|
ms << ar;
|
2001-11-08 15:12:33 +00:00
|
|
|
string expr = os.str().c_str();
|
2001-11-07 08:51:35 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) { // at most 100 attempts
|
|
|
|
// try to fix missing '*' the hard way by using mint
|
|
|
|
//
|
|
|
|
// ... > echo "1A;" | mint -i 1 -S -s -q
|
|
|
|
// on line 1: 1A;
|
|
|
|
// ^ syntax error -
|
|
|
|
// Probably missing an operator such as * p
|
|
|
|
//
|
|
|
|
lyxerr << "checking expr: '" << expr << "'\n";
|
|
|
|
string out = captureOutput("mint -i 1 -S -s -q -q", expr + ";");
|
|
|
|
if (out.empty())
|
|
|
|
break; // expression syntax is ok
|
2001-11-08 15:12:33 +00:00
|
|
|
istringstream is(out.c_str());
|
2001-11-07 08:51:35 +00:00
|
|
|
string line;
|
|
|
|
getline(is, line);
|
|
|
|
if (line.find("on line") != 0)
|
|
|
|
break; // error message not identified
|
|
|
|
getline(is, line);
|
|
|
|
string::size_type pos = line.find('^');
|
|
|
|
if (pos == string::npos || pos < 15)
|
|
|
|
break; // caret position not found
|
2001-11-07 14:14:04 +00:00
|
|
|
pos -= 15; // skip the "on line ..." part
|
2001-11-09 12:01:10 +00:00
|
|
|
if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
|
2001-11-07 14:14:04 +00:00
|
|
|
break; // two '*' in a row are definitely bad
|
|
|
|
expr.insert(pos, "*");
|
2001-11-07 08:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string full = "latex(" + extra + '(' + expr + "));";
|
2001-11-07 13:15:59 +00:00
|
|
|
string out = captureOutput("maple -q", header + full + trailer);
|
2001-11-07 08:51:35 +00:00
|
|
|
|
|
|
|
// change \_ into _
|
2001-11-07 13:15:59 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
MathArray res;
|
|
|
|
mathed_parse_cell(res, out);
|
2001-11-07 08:51:35 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-11-07 13:15:59 +00:00
|
|
|
|
|
|
|
MathArray pipeThroughOctave(string const &, MathArray const & ar)
|
|
|
|
{
|
2001-11-07 17:30:26 +00:00
|
|
|
ostringstream os;
|
|
|
|
OctaveStream vs(os);
|
|
|
|
vs << ar;
|
2001-11-08 15:12:33 +00:00
|
|
|
string expr = os.str().c_str();
|
2001-11-07 14:14:04 +00:00
|
|
|
string out;
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) { // at most 100 attempts
|
|
|
|
//
|
|
|
|
// try to fix missing '*' the hard way
|
|
|
|
// parse error:
|
|
|
|
// >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
|
|
|
|
// ^
|
|
|
|
//
|
|
|
|
lyxerr << "checking expr: '" << expr << "'\n";
|
|
|
|
out = captureOutput("octave -q 2>&1", expr);
|
|
|
|
lyxerr << "checking out: '" << out << "'\n";
|
|
|
|
|
|
|
|
// leave loop if expression syntax is probably ok
|
|
|
|
if (out.find("parse error:") == string::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// search line with single caret
|
2001-11-08 15:12:33 +00:00
|
|
|
istringstream is(out.c_str());
|
2001-11-07 14:14:04 +00:00
|
|
|
string line;
|
|
|
|
while (is) {
|
|
|
|
getline(is, line);
|
|
|
|
lyxerr << "skipping line: '" << line << "'\n";
|
|
|
|
if (line.find(">>> ") != string::npos)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// found line with error, next line is the one with caret
|
|
|
|
getline(is, line);
|
|
|
|
string::size_type pos = line.find('^');
|
|
|
|
lyxerr << "caret line: '" << line << "'\n";
|
|
|
|
lyxerr << "found caret at pos: '" << pos << "'\n";
|
|
|
|
if (pos == string::npos || pos < 4)
|
|
|
|
break; // caret position not found
|
|
|
|
pos -= 4; // skip the ">>> " part
|
|
|
|
if (expr[pos] == '*')
|
|
|
|
break; // two '*' in a row are definitely bad
|
|
|
|
expr.insert(pos, "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out.size() < 6)
|
|
|
|
return MathArray();
|
|
|
|
|
|
|
|
// remove 'ans = '
|
|
|
|
out = out.substr(6);
|
2001-11-07 13:15:59 +00:00
|
|
|
|
|
|
|
// parse output as matrix or single number
|
|
|
|
MathAtom at(new MathArrayInset(out));
|
|
|
|
MathArrayInset const * mat = at.nucleus()->asArrayInset();
|
|
|
|
MathArray res;
|
|
|
|
if (mat->ncols() == 1 && mat->nrows() == 1)
|
|
|
|
res.push_back(mat->cell(0));
|
|
|
|
else {
|
|
|
|
res.push_back(MathAtom(new MathDelimInset("(", ")")));
|
|
|
|
res.back()->cell(0).push_back(at);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2001-11-07 08:51:35 +00:00
|
|
|
|
|
|
|
|
2001-11-12 16:45:09 +00:00
|
|
|
MathArray pipeThroughExtern(string const & lang, string const & extra,
|
|
|
|
MathArray const & ar)
|
2001-10-24 16:10:38 +00:00
|
|
|
{
|
2001-11-07 13:15:59 +00:00
|
|
|
if (lang == "octave")
|
|
|
|
return pipeThroughOctave(extra, ar);
|
2001-11-07 08:51:35 +00:00
|
|
|
|
2001-11-07 13:15:59 +00:00
|
|
|
if (lang == "maple")
|
|
|
|
return pipeThroughMaple(extra, ar);
|
2001-11-07 08:51:35 +00:00
|
|
|
|
2001-11-07 13:15:59 +00:00
|
|
|
// create normalized expression
|
|
|
|
ostringstream os;
|
2001-11-08 12:06:56 +00:00
|
|
|
NormalStream ns(os);
|
2001-11-07 13:15:59 +00:00
|
|
|
os << "[" << extra << ' ';
|
2001-11-09 08:35:57 +00:00
|
|
|
ns << ar;
|
2001-11-07 13:15:59 +00:00
|
|
|
os << "]";
|
|
|
|
string data = os.str().c_str();
|
2001-11-07 08:51:35 +00:00
|
|
|
|
2001-11-07 13:15:59 +00:00
|
|
|
// search external script
|
|
|
|
string file = LibFileSearch("mathed", "extern_" + lang);
|
|
|
|
if (file.empty()) {
|
|
|
|
lyxerr << "converter to '" << lang << "' not found\n";
|
|
|
|
return MathArray();
|
2001-10-24 16:10:38 +00:00
|
|
|
}
|
2001-11-07 13:15:59 +00:00
|
|
|
|
|
|
|
// run external sript
|
|
|
|
string out = captureOutput(file, data);
|
|
|
|
MathArray res;
|
|
|
|
mathed_parse_cell(res, out);
|
2001-10-24 16:10:38 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-10-22 15:37:49 +00:00
|
|
|
}
|
|
|
|
|
2001-10-18 13:21:21 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
InsetFormula::InsetFormula()
|
2001-11-08 12:28:33 +00:00
|
|
|
: par_(MathAtom(new MathHullInset))
|
2001-08-06 17:20:26 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
InsetFormula::InsetFormula(MathInsetTypes t)
|
2001-11-08 12:28:33 +00:00
|
|
|
: par_(MathAtom(new MathHullInset(t)))
|
2001-06-25 00:06:33 +00:00
|
|
|
{}
|
1999-11-24 22:14:46 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-08-03 09:54:48 +00:00
|
|
|
InsetFormula::InsetFormula(string const & s)
|
2001-07-06 12:09:32 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
if (s.size()) {
|
|
|
|
bool res = mathed_parse_normal(par_, s);
|
2001-08-14 07:46:11 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
if (!res)
|
|
|
|
res = mathed_parse_normal(par_, "$" + s + "$");
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
lyxerr << "cannot interpret '" << s << "' as math\n";
|
2001-11-08 12:28:33 +00:00
|
|
|
par_ = MathAtom(new MathHullInset(LM_OT_SIMPLE));
|
2001-10-12 12:02:49 +00:00
|
|
|
}
|
2001-08-14 07:46:11 +00:00
|
|
|
}
|
2001-07-26 06:56:43 +00:00
|
|
|
metrics();
|
2001-07-06 12:09:32 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-07-06 15:57:54 +00:00
|
|
|
Inset * InsetFormula::clone(Buffer const &, bool) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return new InsetFormula(*this);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-09-24 13:38:52 +00:00
|
|
|
void InsetFormula::write(Buffer const * buf, ostream & os) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-07 00:44:53 +00:00
|
|
|
os << "Formula ";
|
2001-09-24 13:38:52 +00:00
|
|
|
latex(buf, os, false, false);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-10-19 11:25:48 +00:00
|
|
|
int InsetFormula::latex(Buffer const * buf, ostream & os, bool fragil, bool)
|
|
|
|
const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-11-09 08:35:57 +00:00
|
|
|
WriteStream wi(buf, os, fragil);
|
2001-10-19 11:25:48 +00:00
|
|
|
par_->write(wi);
|
2001-06-25 00:06:33 +00:00
|
|
|
return 1;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-19 11:25:48 +00:00
|
|
|
int InsetFormula::ascii(Buffer const * buf, ostream & os, int) const
|
2000-04-24 20:58:23 +00:00
|
|
|
{
|
2001-11-09 08:35:57 +00:00
|
|
|
WriteStream wi(buf, os, false);
|
2001-10-19 11:25:48 +00:00
|
|
|
par_->write(wi);
|
2001-06-25 00:06:33 +00:00
|
|
|
return 1;
|
2000-04-24 20:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-24 13:38:52 +00:00
|
|
|
int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
|
2000-03-06 02:42:40 +00:00
|
|
|
{
|
2001-11-07 17:30:26 +00:00
|
|
|
return docbook(buf, os);
|
2000-03-06 02:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-24 13:38:52 +00:00
|
|
|
int InsetFormula::docbook(Buffer const * buf, ostream & os) const
|
2000-03-06 02:42:40 +00:00
|
|
|
{
|
2001-11-07 17:30:26 +00:00
|
|
|
MathMLStream ms(os);
|
2001-11-08 12:06:56 +00:00
|
|
|
ms << MTag("equation") << MTag("alt");
|
2001-11-07 17:30:26 +00:00
|
|
|
int res = ascii(buf, ms.os_, 0);
|
2001-11-08 12:06:56 +00:00
|
|
|
ms << ETag("alt") << MTag("math");
|
|
|
|
ms << par_.nucleus();
|
|
|
|
ms << ETag("math") << ETag("equation");
|
|
|
|
return ms.line_ + res;
|
2000-03-06 02:42:40 +00:00
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-09-24 13:38:52 +00:00
|
|
|
void InsetFormula::read(Buffer const *, LyXLex & lex)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
mathed_parse_normal(par_, lex);
|
2001-07-26 06:56:43 +00:00
|
|
|
metrics();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-19 11:25:48 +00:00
|
|
|
void InsetFormula::draw(BufferView * bv, LyXFont const & font,
|
2001-06-27 14:10:35 +00:00
|
|
|
int y, float & xx, bool) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-27 14:10:35 +00:00
|
|
|
int x = int(xx) - 1;
|
|
|
|
y -= 2;
|
2001-07-13 14:54:56 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
Painter & pain = bv->painter();
|
2001-02-14 19:35:25 +00:00
|
|
|
|
2001-10-22 15:37:49 +00:00
|
|
|
metrics(bv, font);
|
2001-08-03 09:54:48 +00:00
|
|
|
int w = par_->width();
|
|
|
|
int h = par_->height();
|
|
|
|
int a = par_->ascent();
|
2001-07-13 14:54:56 +00:00
|
|
|
pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
|
|
|
|
|
|
|
|
if (mathcursor && mathcursor->formula() == this) {
|
2001-07-16 15:53:25 +00:00
|
|
|
mathcursor->drawSelection(pain);
|
2001-07-13 14:54:56 +00:00
|
|
|
pain.rectangle(x, y - a, w, h, LColor::mathframe);
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
2001-08-03 09:54:48 +00:00
|
|
|
par_->draw(pain, x, y);
|
|
|
|
xx += par_->width();
|
2001-10-24 09:16:06 +00:00
|
|
|
xo_ = x;
|
|
|
|
yo_ = y;
|
2001-02-14 19:35:25 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
setCursorVisible(false);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
vector<string> const InsetFormula::getLabelList() const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
return mat()->getLabelList();
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
UpdatableInset::RESULT
|
2001-06-28 10:25:20 +00:00
|
|
|
InsetFormula::localDispatch(BufferView * bv, kb_action action,
|
2001-06-25 00:06:33 +00:00
|
|
|
string const & arg)
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
RESULT result = DISPATCHED;
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
switch (action) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_BREAKLINE:
|
|
|
|
bv->lockedInsetStoreUndo(Undo::INSERT);
|
2001-07-06 12:09:32 +00:00
|
|
|
mathcursor->breakLine();
|
2001-07-16 15:53:25 +00:00
|
|
|
mathcursor->normalize();
|
2001-07-17 09:46:07 +00:00
|
|
|
updateLocal(bv, true);
|
2001-06-25 00:06:33 +00:00
|
|
|
break;
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_MATH_NUMBER:
|
|
|
|
{
|
|
|
|
//lyxerr << "toggling all numbers\n";
|
|
|
|
if (display()) {
|
|
|
|
bv->lockedInsetStoreUndo(Undo::INSERT);
|
2001-10-12 12:02:49 +00:00
|
|
|
bool old = mat()->numberedType();
|
2001-09-26 16:52:34 +00:00
|
|
|
for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
|
2001-10-12 12:02:49 +00:00
|
|
|
mat()->numbered(row, !old);
|
2001-06-25 00:06:33 +00:00
|
|
|
bv->owner()->message(old ? _("No number") : _("Number"));
|
2001-07-17 09:46:07 +00:00
|
|
|
updateLocal(bv, true);
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
break;
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_MATH_NONUMBER:
|
|
|
|
{
|
|
|
|
//lyxerr << "toggling line number\n";
|
|
|
|
if (display()) {
|
|
|
|
bv->lockedInsetStoreUndo(Undo::INSERT);
|
2001-10-01 12:10:32 +00:00
|
|
|
MathCursor::row_type row = mathcursor->row();
|
2001-10-12 12:02:49 +00:00
|
|
|
bool old = mat()->numbered(row);
|
2001-06-25 00:06:33 +00:00
|
|
|
bv->owner()->message(old ? _("No number") : _("Number"));
|
2001-10-12 12:02:49 +00:00
|
|
|
mat()->numbered(row, !old);
|
2001-07-17 09:46:07 +00:00
|
|
|
updateLocal(bv, true);
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
break;
|
2001-02-14 19:35:25 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_INSERT_LABEL:
|
|
|
|
{
|
|
|
|
bv->lockedInsetStoreUndo(Undo::INSERT);
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-10-01 12:10:32 +00:00
|
|
|
MathCursor::row_type row = mathcursor->row();
|
2001-10-12 12:02:49 +00:00
|
|
|
string old_label = mat()->label(row);
|
2001-06-25 00:06:33 +00:00
|
|
|
string new_label = arg;
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
if (new_label.empty()) {
|
|
|
|
string const default_label =
|
|
|
|
(lyxrc.label_init_length >= 0) ? "eq:" : "";
|
|
|
|
pair<bool, string> const res = old_label.empty()
|
|
|
|
? askForText(_("Enter new label to insert:"), default_label)
|
|
|
|
: askForText(_("Enter label:"), old_label);
|
|
|
|
|
|
|
|
lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
|
|
|
|
if (!res.first)
|
|
|
|
break;
|
|
|
|
new_label = frontStrip(strip(res.second));
|
|
|
|
}
|
2001-02-14 19:35:25 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
//if (new_label == old_label)
|
|
|
|
// break; // Nothing to do
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
if (!new_label.empty()) {
|
|
|
|
lyxerr << "setting label to '" << new_label << "'\n";
|
2001-10-12 12:02:49 +00:00
|
|
|
mat()->numbered(row, true);
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
|
|
|
|
bv->redraw();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
mat()->label(row, new_label);
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-07-17 09:46:07 +00:00
|
|
|
updateLocal(bv, true);
|
2001-06-25 00:06:33 +00:00
|
|
|
break;
|
2001-02-14 19:35:25 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_MATH_MUTATE:
|
2001-07-06 12:09:32 +00:00
|
|
|
{
|
|
|
|
bv->lockedInsetStoreUndo(Undo::EDIT);
|
|
|
|
int x;
|
|
|
|
int y;
|
2001-07-26 09:01:36 +00:00
|
|
|
mathcursor->getPos(x, y);
|
2001-10-12 12:02:49 +00:00
|
|
|
mat()->mutate(arg);
|
2001-07-26 09:01:36 +00:00
|
|
|
mathcursor->setPos(x, y);
|
2001-07-06 12:09:32 +00:00
|
|
|
mathcursor->normalize();
|
2001-07-17 09:46:07 +00:00
|
|
|
updateLocal(bv, true);
|
2001-06-25 00:06:33 +00:00
|
|
|
break;
|
2001-07-06 12:09:32 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-10-29 15:45:24 +00:00
|
|
|
case LFUN_MATH_EXTERN:
|
|
|
|
{
|
|
|
|
bv->lockedInsetStoreUndo(Undo::EDIT);
|
|
|
|
handleExtern(arg);
|
|
|
|
// re-compute inset dimension
|
|
|
|
metrics(bv);
|
|
|
|
updateLocal(bv, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_MATH_DISPLAY:
|
2001-07-12 11:53:45 +00:00
|
|
|
{
|
2001-11-13 15:29:45 +00:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
2001-07-26 09:01:36 +00:00
|
|
|
mathcursor->getPos(x, y);
|
2001-10-12 12:02:49 +00:00
|
|
|
if (mat()->getType() == LM_OT_SIMPLE)
|
|
|
|
mat()->mutate(LM_OT_EQUATION);
|
2001-06-25 00:06:33 +00:00
|
|
|
else
|
2001-10-12 12:02:49 +00:00
|
|
|
mat()->mutate(LM_OT_SIMPLE);
|
2001-07-26 09:01:36 +00:00
|
|
|
mathcursor->setPos(x, y);
|
2001-07-12 11:53:45 +00:00
|
|
|
mathcursor->normalize();
|
2001-07-17 09:46:07 +00:00
|
|
|
updateLocal(bv, true);
|
2001-06-25 00:06:33 +00:00
|
|
|
break;
|
2001-07-12 11:53:45 +00:00
|
|
|
}
|
2001-07-26 06:46:50 +00:00
|
|
|
|
|
|
|
case LFUN_PASTESELECTION:
|
|
|
|
{
|
|
|
|
string const clip = bv->getClipboard();
|
|
|
|
if (!clip.empty())
|
2001-10-12 12:02:49 +00:00
|
|
|
mathed_parse_normal(par_, clip);
|
2001-07-26 06:46:50 +00:00
|
|
|
break;
|
|
|
|
}
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-09-04 13:32:06 +00:00
|
|
|
case LFUN_MATH_COLUMN_INSERT:
|
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
if (mat()->getType() == LM_OT_ALIGN)
|
|
|
|
mat()->mutate(LM_OT_ALIGNAT);
|
|
|
|
mat()->addCol(mat()->ncols());
|
2001-09-04 13:32:06 +00:00
|
|
|
mathcursor->normalize();
|
|
|
|
updateLocal(bv, true);
|
2001-11-13 15:29:45 +00:00
|
|
|
break;
|
2001-09-04 13:32:06 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
default:
|
2001-06-28 10:25:20 +00:00
|
|
|
result = InsetFormulaBase::localDispatch(bv, action, arg);
|
2001-02-14 19:35:25 +00:00
|
|
|
}
|
2000-05-19 16:46:01 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
return result;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-10-23 13:48:52 +00:00
|
|
|
|
2001-11-12 16:45:09 +00:00
|
|
|
bool needEqnArray(string const & extra)
|
|
|
|
{
|
|
|
|
return extra == "dsolve";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-23 13:48:52 +00:00
|
|
|
void InsetFormula::handleExtern(const string & arg)
|
|
|
|
{
|
|
|
|
// where are we?
|
|
|
|
if (!mathcursor)
|
|
|
|
return;
|
|
|
|
|
2001-11-12 16:45:09 +00:00
|
|
|
string lang;
|
|
|
|
string extra;
|
|
|
|
istringstream iss(arg.c_str());
|
|
|
|
iss >> lang >> extra;
|
|
|
|
if (extra.empty())
|
|
|
|
extra = "noextra";
|
|
|
|
|
2001-10-24 09:16:06 +00:00
|
|
|
bool selected = mathcursor->selection();
|
|
|
|
|
2001-10-23 13:48:52 +00:00
|
|
|
MathArray ar;
|
2001-11-12 16:45:09 +00:00
|
|
|
if (needEqnArray(extra)) {
|
|
|
|
mathcursor->last();
|
|
|
|
mathcursor->readLine(ar);
|
|
|
|
mathcursor->breakLine();
|
|
|
|
} else if (selected) {
|
2001-10-23 13:48:52 +00:00
|
|
|
mathcursor->selGet(ar);
|
2001-11-08 12:06:56 +00:00
|
|
|
//lyxerr << "use selection: " << ar << "\n";
|
2001-10-24 09:16:06 +00:00
|
|
|
} else {
|
2001-11-12 16:45:09 +00:00
|
|
|
mathcursor->last();
|
2001-11-07 11:14:59 +00:00
|
|
|
mathcursor->stripFromLastEqualSign();
|
2001-10-23 13:48:52 +00:00
|
|
|
ar = mathcursor->cursor().cell();
|
2001-10-24 16:10:38 +00:00
|
|
|
mathcursor->insert(MathAtom(new MathCharInset('=', LM_TC_VAR)));
|
2001-11-07 08:51:35 +00:00
|
|
|
//lyxerr << "use whole cell: " << ar << "\n";
|
2001-10-24 09:16:06 +00:00
|
|
|
}
|
|
|
|
|
2001-11-12 16:45:09 +00:00
|
|
|
mathcursor->insert(pipeThroughExtern(lang, extra, ar));
|
2001-10-23 13:48:52 +00:00
|
|
|
}
|
2001-10-12 12:02:49 +00:00
|
|
|
|
2001-10-24 16:10:38 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool InsetFormula::display() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
return mat()->getType() != LM_OT_SIMPLE;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-11-08 12:28:33 +00:00
|
|
|
MathHullInset const * InsetFormula::mat() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-11-09 08:35:57 +00:00
|
|
|
lyx::Assert(par_->asHullInset());
|
|
|
|
return par_->asHullInset();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-08-01 16:48:06 +00:00
|
|
|
|
2001-11-08 12:28:33 +00:00
|
|
|
MathHullInset * InsetFormula::mat()
|
2001-10-12 12:02:49 +00:00
|
|
|
{
|
2001-11-09 08:35:57 +00:00
|
|
|
lyx::Assert(par_->asHullInset());
|
|
|
|
return par_->asHullInset();
|
2001-07-06 12:09:32 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-28 10:25:20 +00:00
|
|
|
Inset::Code InsetFormula::lyxCode() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return Inset::MATH_CODE;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-28 10:25:20 +00:00
|
|
|
void InsetFormula::validate(LaTeXFeatures & features) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-08-03 09:54:48 +00:00
|
|
|
par_->validate(features);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-09-04 13:32:06 +00:00
|
|
|
|
2001-07-23 09:11:14 +00:00
|
|
|
bool InsetFormula::insetAllowed(Inset::Code code) const
|
|
|
|
{
|
2001-11-12 08:20:30 +00:00
|
|
|
return
|
|
|
|
(code == Inset::LABEL_CODE && display())
|
|
|
|
|| code == Inset::ERT_CODE;
|
2001-07-23 09:11:14 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
int InsetFormula::ascent(BufferView *, LyXFont const &) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-10-12 13:15:34 +00:00
|
|
|
return par_->ascent() + 2;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2001-02-16 09:25:43 +00:00
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
int InsetFormula::descent(BufferView *, LyXFont const &) const
|
2001-02-16 09:25:43 +00:00
|
|
|
{
|
2001-10-12 13:15:34 +00:00
|
|
|
return par_->descent() - 2;
|
2001-02-16 09:25:43 +00:00
|
|
|
}
|
2001-03-16 12:08:14 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
2001-10-19 11:25:48 +00:00
|
|
|
int InsetFormula::width(BufferView * bv, LyXFont const & font) const
|
2001-03-16 12:08:14 +00:00
|
|
|
{
|
2001-10-22 15:37:49 +00:00
|
|
|
metrics(bv, font);
|
2001-08-03 09:54:48 +00:00
|
|
|
return par_->width();
|
2001-03-16 12:08:14 +00:00
|
|
|
}
|
2001-08-01 13:28:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
MathInsetTypes InsetFormula::getType() const
|
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
return mat()->getType();
|
2001-08-01 13:28:45 +00:00
|
|
|
}
|