re-enabling the "selected text gets converted to math"-feature

small bix fix
cosmetics


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2510 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2001-08-14 07:46:11 +00:00
parent 1279189bd0
commit 8766c98cfe
7 changed files with 142 additions and 111 deletions

View File

@ -1,8 +1,3 @@
2001-08-13 Angus Leeming <a.leeming@ic.ac.uk>
* math_parser.C (lexArg): remove default argument from the .C file
as it causes DEC cxx to complain about a redefinition.
2001-08-13 André Pönitz <poenitz@gmx.net> 2001-08-13 André Pönitz <poenitz@gmx.net>
* math_factory.[Ch]: new files for the creation of math insets * math_factory.[Ch]: new files for the creation of math insets
@ -16,6 +11,14 @@
* math_parser.C: * math_parser.C:
math_cursor.C: reading support for TeX style \over and \choose math_cursor.C: reading support for TeX style \over and \choose
* formulabase.C: re-enabling the "selected text gets converted to
math when pressing C-m" feature.
2001-08-13 Angus Leeming <a.leeming@ic.ac.uk>
* math_parser.C (lexArg): remove default argument from the .C file
as it causes DEC cxx to complain about a redefinition.
2001-08-10 André Pönitz <poenitz@gmx.net> 2001-08-10 André Pönitz <poenitz@gmx.net>
* math_scopeinset.[Ch]: new inset for {} blocks * math_scopeinset.[Ch]: new inset for {} blocks

View File

@ -46,9 +46,6 @@ using std::pair;
using std::endl; using std::endl;
using std::vector; using std::vector;
extern char const * latex_mathenv[];
extern MathCursor * mathcursor;
InsetFormula::InsetFormula() InsetFormula::InsetFormula()
: par_(new MathMatrixInset) : par_(new MathMatrixInset)
@ -68,6 +65,13 @@ InsetFormula::InsetFormula(MathInsetTypes t)
InsetFormula::InsetFormula(string const & s) InsetFormula::InsetFormula(string const & s)
: par_(mathed_parse_normal(s)) : par_(mathed_parse_normal(s))
{ {
if (!par_)
par_ = mathed_parse_normal("$" + s + "$");
if (!par_) {
lyxerr << "cannot interpret '" << s << "' as math\n";
par_ = new MathMatrixInset(LM_OT_SIMPLE);
}
metrics(); metrics();
} }

View File

@ -131,22 +131,26 @@ void InsetFormulaBase::write(Buffer const *, ostream & os) const
write(os); write(os);
} }
int InsetFormulaBase::latex(Buffer const *, ostream & os, int InsetFormulaBase::latex(Buffer const *, ostream & os,
bool fragile, bool spacing) const bool fragile, bool spacing) const
{ {
return latex(os, fragile, spacing); return latex(os, fragile, spacing);
} }
int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
{ {
return ascii(os, spacing); return ascii(os, spacing);
} }
int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
{ {
return linuxdoc(os); return linuxdoc(os);
} }
int InsetFormulaBase::docBook(Buffer const *, ostream & os) const int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
{ {
return docBook(os); return docBook(os);
@ -186,6 +190,7 @@ void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
void InsetFormulaBase::edit(BufferView * bv, bool front) void InsetFormulaBase::edit(BufferView * bv, bool front)
{ {
// looks hackish but seems to work
edit(bv, front ? 0 : 1, 0, 0); edit(bv, front ? 0 : 1, 0, 0);
} }
@ -713,16 +718,16 @@ Inset::Code InsetFormulaBase::lyxCode() const
void mathDispatchCreation(BufferView * bv, string const & arg, bool display) void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
{ {
if (bv->available()) { if (bv->available()) {
// Feature "Read math inset from selection" disabled. // use selection if available..
// // use selection if available.. //string sel;
// string sel; //if (action == LFUN_MATH_IMPORT_SELECTION)
// if (action == LFUN_MATH_IMPORT_SELECTION) // sel = "";
// sel = ""; //else
// else
// sel = bv->getLyXText()->selectionAsString(bv->buffer());
InsetFormula * f; string sel = bv->getLyXText()->selectionAsString(bv->buffer());
// if (sel.empty()) {
InsetFormulaBase * f;
if (sel.empty()) {
f = new InsetFormula; f = new InsetFormula;
if (openNewInset(bv, f)) { if (openNewInset(bv, f)) {
// don't do that also for LFUN_MATH_MODE unless you want end up with // don't do that also for LFUN_MATH_MODE unless you want end up with
@ -732,11 +737,16 @@ void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
f->localDispatch(bv, LFUN_MATH_DISPLAY, string()); f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
f->localDispatch(bv, LFUN_INSERT_MATH, arg); f->localDispatch(bv, LFUN_INSERT_MATH, arg);
} }
// } else { } else {
// f = new InsetFormula(sel); // create a macro if we see "\\newcommand" somewhere, and an ordinary
// bv->getLyXText()->cutSelection(bv); // formula otherwise
// openNewInset(bv, f); if (sel.find("\\newcommand") == string::npos)
// } f = new InsetFormula(sel);
else
f = new InsetFormulaMacro(sel);
bv->getLyXText()->cutSelection(bv);
openNewInset(bv, f);
}
} }
bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode")); bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
} }
@ -745,17 +755,20 @@ void mathDispatchMathDisplay(BufferView * bv, string const & arg)
{ {
mathDispatchCreation(bv, arg, true); mathDispatchCreation(bv, arg, true);
} }
void mathDispatchMathMode(BufferView * bv, string const & arg) void mathDispatchMathMode(BufferView * bv, string const & arg)
{ {
mathDispatchCreation(bv, arg, false); mathDispatchCreation(bv, arg, false);
} }
void mathDispatchMathImportSelection(BufferView * bv, string const & arg) void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
{ {
mathDispatchCreation(bv, arg, true); mathDispatchCreation(bv, arg, true);
} }
void mathDispatchMathMacro(BufferView * bv, string const & arg) void mathDispatchMathMacro(BufferView * bv, string const & arg)
{ {
if (bv->available()) { if (bv->available()) {
@ -770,6 +783,7 @@ void mathDispatchMathMacro(BufferView * bv, string const & arg)
} }
} }
void mathDispatchMathDelim(BufferView * bv, string const & arg) void mathDispatchMathDelim(BufferView * bv, string const & arg)
{ {
if (bv->available()) { if (bv->available()) {
@ -787,6 +801,7 @@ void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
} }
} }
void mathDispatchInsertMath(BufferView * bv, string const & arg) void mathDispatchInsertMath(BufferView * bv, string const & arg)
{ {
if (bv->available()) { if (bv->available()) {

View File

@ -44,6 +44,7 @@ extern MathCursor * mathcursor;
InsetFormulaMacro::InsetFormulaMacro() InsetFormulaMacro::InsetFormulaMacro()
{ {
// inset name is inherited from Inset
setInsetName("unknown"); setInsetName("unknown");
} }
@ -55,6 +56,16 @@ InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
} }
InsetFormulaMacro::InsetFormulaMacro(string const & s)
{
MathMacroTemplate * t = mathed_parse_macro(s);
MathMacroTable::insertTemplate(*t);
setInsetName(t->name());
delete t;
metrics();
}
Inset * InsetFormulaMacro::clone(Buffer const &, bool) const Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
{ {
return new InsetFormulaMacro(*this); return new InsetFormulaMacro(*this);

View File

@ -25,17 +25,18 @@
class MathMacroTemplate; class MathMacroTemplate;
// InsetFormulaMacro's ParInset is the ParInset of the macro definition // InsetFormulaMacro's only knows its name and asks the global
// which in turn is stored in the global MathMacroTable. // MathMacroTable if it needs to know more.
// No copying/updating needed anymore...
/// ///
class InsetFormulaMacro: public InsetFormulaBase { class InsetFormulaMacro: public InsetFormulaBase {
public: public:
/// ///
InsetFormulaMacro(); InsetFormulaMacro();
/// /// construct a macro hull from its name and the number of arguments
explicit InsetFormulaMacro(string name, int na); explicit InsetFormulaMacro(string name, int na);
/// constructs a mocro from its LaTeX definition
explicit InsetFormulaMacro(string const & s);
/// ///
int ascent(BufferView *, LyXFont const &) const; int ascent(BufferView *, LyXFont const &) const;
/// ///

View File

@ -1194,62 +1194,6 @@ MathMatrixInset * MathCursor::outerPar() const
static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par())); static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
} }
////////////////////////////////////////////////////////////////////////
bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
{
return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
}
bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
{
if (ti.par_ != it.par_) {
lyxerr << "can't compare cursor and anchor in different insets\n";
return true;
}
if (ti.idx_ != it.idx_)
return ti.idx_ < it.idx_;
return ti.pos_ < it.pos_;
}
MathArray & MathCursorPos::cell(int idx) const
{
return par_->cell(idx);
}
MathArray & MathCursorPos::cell() const
{
return par_->cell(idx_);
}
MathXArray & MathCursorPos::xcell(int idx) const
{
return par_->xcell(idx);
}
MathXArray & MathCursorPos::xcell() const
{
return par_->xcell(idx_);
}
MathCursorPos MathCursor::normalAnchor() const
{
// use Anchor on the same level as Cursor
MathCursorPos normal = Anchor_[Cursor_.size() - 1];
if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
// anchor is behind cursor -> move anchor behind the inset
++normal.pos_;
}
return normal;
}
void MathCursor::interpret(string const & s) void MathCursor::interpret(string const & s)
{ {
@ -1441,3 +1385,61 @@ void MathCursor::interpret(string const & s)
} }
////////////////////////////////////////////////////////////////////////
bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
{
return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
}
bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
{
if (ti.par_ != it.par_) {
lyxerr << "can't compare cursor and anchor in different insets\n";
return true;
}
if (ti.idx_ != it.idx_)
return ti.idx_ < it.idx_;
return ti.pos_ < it.pos_;
}
MathArray & MathCursorPos::cell(int idx) const
{
return par_->cell(idx);
}
MathArray & MathCursorPos::cell() const
{
return par_->cell(idx_);
}
MathXArray & MathCursorPos::xcell(int idx) const
{
return par_->xcell(idx);
}
MathXArray & MathCursorPos::xcell() const
{
return par_->xcell(idx_);
}
MathCursorPos MathCursor::normalAnchor() const
{
// use Anchor on the same level as Cursor
MathCursorPos normal = Anchor_[Cursor_.size() - 1];
if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
// anchor is behind cursor -> move anchor behind the inset
++normal.pos_;
}
return normal;
}

View File

@ -316,7 +316,11 @@ int Parser::yylex()
while (is_.good()) { while (is_.good()) {
unsigned char c = getuchar(); unsigned char c = getuchar();
//lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl; //lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl;
if (!is_.good())
break;
if (lexcode[c] == LexNewLine) { if (lexcode[c] == LexNewLine) {
++lineno_; ++lineno_;
@ -329,29 +333,23 @@ int Parser::yylex()
} while (c != '\n' && is_.good()); // eat comments } while (c != '\n' && is_.good()); // eat comments
} }
if (lexcode[c] == LexOther) { ival_ = c;
ival_ = c; if (lexcode[c] == LexOther)
return LM_TK_STR; return LM_TK_STR;
}
if (lexcode[c] == LexAlpha || lexcode[c] == LexSpace) { if (lexcode[c] == LexAlpha || lexcode[c] == LexSpace)
ival_ = c;
return LM_TK_ALPHA; return LM_TK_ALPHA;
}
if (lexcode[c] == LexBOP) { if (lexcode[c] == LexBOP)
ival_ = c;
return LM_TK_BOP; return LM_TK_BOP;
}
if (lexcode[c] == LexMath) { if (lexcode[c] == LexMath) {
ival_ = 0; ival_ = 0;
return LM_TK_MATH; return LM_TK_MATH;
} }
if (lexcode[c] == LexSelf) { if (lexcode[c] == LexSelf)
return c; return c;
}
if (lexcode[c] == LexArgument) { if (lexcode[c] == LexArgument) {
c = getuchar(); c = getuchar();
@ -361,9 +359,9 @@ int Parser::yylex()
if (lexcode[c] == LexESC) { if (lexcode[c] == LexESC) {
c = getuchar(); c = getuchar();
//lyxerr << "reading second byte: '" << c << "' code: " << lexcode[c] << endl; //lyxerr << "reading second byte: '" << c
string s; // << "' code: " << lexcode[c] << endl;
s += c; string s(1, c);
latexkeys const * l = in_word_set(s); latexkeys const * l = in_word_set(s);
if (l) { if (l) {
//lyxerr << "found key: " << l << endl; //lyxerr << "found key: " << l << endl;
@ -578,11 +576,11 @@ void Parser::parse_into(MathArray & array, unsigned flags)
{ {
MathTextCodes yyvarcode = LM_TC_VAR; MathTextCodes yyvarcode = LM_TC_VAR;
int t = yylex();
bool panic = false; bool panic = false;
int limits = 0; int limits = 0;
while (t) { while (int t = yylex()) {
//lyxerr << "t: " << t << " flags: " << flags << " i: " << ival_ //lyxerr << "t: " << t << " flags: " << flags << " i: " << ival_
// << " '" << sval_ << "'\n"; // << " '" << sval_ << "'\n";
//array.dump(lyxerr); //array.dump(lyxerr);
@ -594,9 +592,9 @@ void Parser::parse_into(MathArray & array, unsigned flags)
// skip the brace and collect everything to the next matching // skip the brace and collect everything to the next matching
// closing brace // closing brace
flags |= FLAG_BRACE_LAST; flags |= FLAG_BRACE_LAST;
t = yylex(); continue;
} else { } else {
// take only this single token // handle only this single token, leave the loop if done
flags |= FLAG_LEAVE; flags |= FLAG_LEAVE;
} }
} }
@ -608,14 +606,12 @@ void Parser::parse_into(MathArray & array, unsigned flags)
break; break;
} else { } else {
flags &= ~FLAG_BRACE; flags &= ~FLAG_BRACE;
t = yylex();
continue; continue;
} }
} }
if (flags & FLAG_BLOCK) { if (flags & FLAG_BLOCK) {
if (t == LM_TK_CLOSE || t == '&' || if (t == LM_TK_CLOSE || t == '&' || t == LM_TK_NEWLINE || t == LM_TK_END){
t == LM_TK_NEWLINE || t == LM_TK_END) {
putback(t); putback(t);
return; return;
} }
@ -844,16 +840,15 @@ void Parser::parse_into(MathArray & array, unsigned flags)
flags &= ~FLAG_LEAVE; flags &= ~FLAG_LEAVE;
break; break;
} }
}
if (panic) { if (panic) {
lyxerr << " Math Panic, expect problems!\n"; lyxerr << " Math Panic, expect problems!\n";
// Search for the end command. // Search for the end command.
do { int t;
t = yylex(); do {
} while (is_.good() && t != LM_TK_END && t);
} else {
t = yylex(); t = yylex();
} } while (is_.good() && t != LM_TK_END && t);
} }
} }