From 8766c98cfe46bf4e7e9e02605b3ab75df1efd8a1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Tue, 14 Aug 2001 07:46:11 +0000 Subject: [PATCH] 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 --- src/mathed/ChangeLog | 13 +++-- src/mathed/formula.C | 10 +++- src/mathed/formulabase.C | 45 ++++++++++----- src/mathed/formulamacro.C | 11 ++++ src/mathed/formulamacro.h | 9 +-- src/mathed/math_cursor.C | 114 +++++++++++++++++++------------------- src/mathed/math_parser.C | 53 ++++++++---------- 7 files changed, 143 insertions(+), 112 deletions(-) diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index ba67429d5f..4cfad8e847 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,8 +1,3 @@ -2001-08-13 Angus Leeming - - * 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 * math_factory.[Ch]: new files for the creation of math insets @@ -16,6 +11,14 @@ * math_parser.C: 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 + + * 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 * math_scopeinset.[Ch]: new inset for {} blocks diff --git a/src/mathed/formula.C b/src/mathed/formula.C index 3dba3bc584..1ba6cbe728 100644 --- a/src/mathed/formula.C +++ b/src/mathed/formula.C @@ -46,9 +46,6 @@ using std::pair; using std::endl; using std::vector; -extern char const * latex_mathenv[]; -extern MathCursor * mathcursor; - InsetFormula::InsetFormula() : par_(new MathMatrixInset) @@ -68,6 +65,13 @@ InsetFormula::InsetFormula(MathInsetTypes t) InsetFormula::InsetFormula(string const & 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(); } diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index aaba69af14..04f41ccfe2 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -131,22 +131,26 @@ void InsetFormulaBase::write(Buffer const *, ostream & os) const write(os); } + int InsetFormulaBase::latex(Buffer const *, ostream & os, bool fragile, bool spacing) const { return latex(os, fragile, spacing); } + int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const { return ascii(os, spacing); } + int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const { return linuxdoc(os); } + int InsetFormulaBase::docBook(Buffer const *, ostream & os) const { 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) { + // looks hackish but seems to work 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) { if (bv->available()) { -// Feature "Read math inset from selection" disabled. -// // use selection if available.. -// string sel; -// if (action == LFUN_MATH_IMPORT_SELECTION) -// sel = ""; -// else -// sel = bv->getLyXText()->selectionAsString(bv->buffer()); - - InsetFormula * f; -// if (sel.empty()) { + // use selection if available.. + //string sel; + //if (action == LFUN_MATH_IMPORT_SELECTION) + // sel = ""; + //else + + string sel = bv->getLyXText()->selectionAsString(bv->buffer()); + + InsetFormulaBase * f; + if (sel.empty()) { f = new InsetFormula; if (openNewInset(bv, f)) { // 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_INSERT_MATH, arg); } -// } else { -// f = new InsetFormula(sel); -// bv->getLyXText()->cutSelection(bv); -// openNewInset(bv, f); -// } + } else { + // create a macro if we see "\\newcommand" somewhere, and an ordinary + // formula otherwise + 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")); } @@ -745,17 +755,20 @@ void mathDispatchMathDisplay(BufferView * bv, string const & arg) { mathDispatchCreation(bv, arg, true); } + void mathDispatchMathMode(BufferView * bv, string const & arg) { mathDispatchCreation(bv, arg, false); } + void mathDispatchMathImportSelection(BufferView * bv, string const & arg) { mathDispatchCreation(bv, arg, true); } + void mathDispatchMathMacro(BufferView * bv, string const & arg) { if (bv->available()) { @@ -770,6 +783,7 @@ void mathDispatchMathMacro(BufferView * bv, string const & arg) } } + void mathDispatchMathDelim(BufferView * bv, string const & arg) { if (bv->available()) { @@ -787,6 +801,7 @@ void mathDispatchInsertMatrix(BufferView * bv, string const & arg) } } + void mathDispatchInsertMath(BufferView * bv, string const & arg) { if (bv->available()) { diff --git a/src/mathed/formulamacro.C b/src/mathed/formulamacro.C index c32946ffad..bac2b2b092 100644 --- a/src/mathed/formulamacro.C +++ b/src/mathed/formulamacro.C @@ -44,6 +44,7 @@ extern MathCursor * mathcursor; InsetFormulaMacro::InsetFormulaMacro() { + // inset name is inherited from Inset 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 { return new InsetFormulaMacro(*this); diff --git a/src/mathed/formulamacro.h b/src/mathed/formulamacro.h index baf7acff5e..a486179bfe 100644 --- a/src/mathed/formulamacro.h +++ b/src/mathed/formulamacro.h @@ -25,17 +25,18 @@ class MathMacroTemplate; -// InsetFormulaMacro's ParInset is the ParInset of the macro definition -// which in turn is stored in the global MathMacroTable. -// No copying/updating needed anymore... +// InsetFormulaMacro's only knows its name and asks the global +// MathMacroTable if it needs to know more. /// class InsetFormulaMacro: public InsetFormulaBase { public: /// InsetFormulaMacro(); - /// + /// construct a macro hull from its name and the number of arguments explicit InsetFormulaMacro(string name, int na); + /// constructs a mocro from its LaTeX definition + explicit InsetFormulaMacro(string const & s); /// int ascent(BufferView *, LyXFont const &) const; /// diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 90d0eeb4eb..2970c29a6d 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -1194,62 +1194,6 @@ MathMatrixInset * MathCursor::outerPar() const static_cast(const_cast(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) { @@ -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; +} + diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index a499871ccc..93c1f5dbe8 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -316,7 +316,11 @@ int Parser::yylex() while (is_.good()) { unsigned char c = getuchar(); + //lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl; + + if (!is_.good()) + break; if (lexcode[c] == LexNewLine) { ++lineno_; @@ -329,29 +333,23 @@ int Parser::yylex() } while (c != '\n' && is_.good()); // eat comments } - if (lexcode[c] == LexOther) { - ival_ = c; + ival_ = c; + if (lexcode[c] == LexOther) return LM_TK_STR; - } - if (lexcode[c] == LexAlpha || lexcode[c] == LexSpace) { - ival_ = c; + if (lexcode[c] == LexAlpha || lexcode[c] == LexSpace) return LM_TK_ALPHA; - } - if (lexcode[c] == LexBOP) { - ival_ = c; + if (lexcode[c] == LexBOP) return LM_TK_BOP; - } if (lexcode[c] == LexMath) { ival_ = 0; return LM_TK_MATH; } - if (lexcode[c] == LexSelf) { + if (lexcode[c] == LexSelf) return c; - } if (lexcode[c] == LexArgument) { c = getuchar(); @@ -361,9 +359,9 @@ int Parser::yylex() if (lexcode[c] == LexESC) { c = getuchar(); - //lyxerr << "reading second byte: '" << c << "' code: " << lexcode[c] << endl; - string s; - s += c; + //lyxerr << "reading second byte: '" << c + // << "' code: " << lexcode[c] << endl; + string s(1, c); latexkeys const * l = in_word_set(s); if (l) { //lyxerr << "found key: " << l << endl; @@ -578,11 +576,11 @@ void Parser::parse_into(MathArray & array, unsigned flags) { MathTextCodes yyvarcode = LM_TC_VAR; - int t = yylex(); bool panic = false; int limits = 0; - while (t) { + while (int t = yylex()) { + //lyxerr << "t: " << t << " flags: " << flags << " i: " << ival_ // << " '" << sval_ << "'\n"; //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 // closing brace flags |= FLAG_BRACE_LAST; - t = yylex(); + continue; } else { - // take only this single token + // handle only this single token, leave the loop if done flags |= FLAG_LEAVE; } } @@ -608,14 +606,12 @@ void Parser::parse_into(MathArray & array, unsigned flags) break; } else { flags &= ~FLAG_BRACE; - t = yylex(); continue; } } if (flags & FLAG_BLOCK) { - if (t == LM_TK_CLOSE || t == '&' || - t == LM_TK_NEWLINE || t == LM_TK_END) { + if (t == LM_TK_CLOSE || t == '&' || t == LM_TK_NEWLINE || t == LM_TK_END){ putback(t); return; } @@ -844,16 +840,15 @@ void Parser::parse_into(MathArray & array, unsigned flags) flags &= ~FLAG_LEAVE; break; } + } - if (panic) { - lyxerr << " Math Panic, expect problems!\n"; - // Search for the end command. - do { - t = yylex(); - } while (is_.good() && t != LM_TK_END && t); - } else { + if (panic) { + lyxerr << " Math Panic, expect problems!\n"; + // Search for the end command. + int t; + do { t = yylex(); - } + } while (is_.good() && t != LM_TK_END && t); } } -- 2.39.2