]> git.lyx.org Git - features.git/commitdiff
re-enabling the "selected text gets converted to math"-feature
authorAndré Pönitz <poenitz@gmx.net>
Tue, 14 Aug 2001 07:46:11 +0000 (07:46 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Tue, 14 Aug 2001 07:46:11 +0000 (07:46 +0000)
small bix fix
cosmetics

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2510 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/ChangeLog
src/mathed/formula.C
src/mathed/formulabase.C
src/mathed/formulamacro.C
src/mathed/formulamacro.h
src/mathed/math_cursor.C
src/mathed/math_parser.C

index ba67429d5fd4ee3d547e494d98407370ca40ab5c..4cfad8e8472b0c8d856e0ae9ffb7d6ea1191426e 100644 (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>
 
        * math_factory.[Ch]: new files for the creation of math insets
        * 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  <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>
 
        *       math_scopeinset.[Ch]: new inset for {} blocks
index 3dba3bc58466bb2a8647a6e27dceeba9171c3421..1ba6cbe72883b388884fee2a5989575e94d681e9 100644 (file)
@@ -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();
 }
 
index aaba69af14e1f9b8830fe505ee6b42bee2895c57..04f41ccfe25de6c1503daf95a9a11d53f8a2bbf6 100644 (file)
@@ -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()) {
index c32946ffad5981ddabbd34533e3f167f347c62e8..bac2b2b09219ca5fef32c4cba0ee7d6ae75448ee 100644 (file)
@@ -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);
index baf7acff5e80f602d0abfa3a3a91992f35295feb..a486179bfe0c1df093d8604b779645a483514063 100644 (file)
 
 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;
        ///
index 90d0eeb4ebec8b2ed4672404b52afbd081f53ecf..2970c29a6d66c724d52ffcfc94e5721b8b606c3b 100644 (file)
@@ -1194,62 +1194,6 @@ MathMatrixInset * MathCursor::outerPar() const
                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)
 {
@@ -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;
+}
+
index a499871ccc39507066117af6264e011d5fcbfd89..93c1f5dbe84901acf7074ce99b3bcffa8508c9ff 100644 (file)
@@ -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);
        }
 }