2 * \file math_hullinset.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "math_charinset.h"
14 #include "math_data.h"
15 #include "math_extern.h"
16 #include "math_hullinset.h"
17 #include "math_mathmlstream.h"
18 #include "math_streamstr.h"
19 #include "math_support.h"
21 #include "BufferView.h"
23 #include "dispatchresult.h"
25 #include "funcrequest.h"
27 #include "LaTeXFeatures.h"
30 #include "outputparams.h"
31 #include "textpainter.h"
33 #include "frontends/Alert.h"
35 #include "support/std_sstream.h"
37 using lyx::support::trim;
45 using std::istringstream;
47 using std::ostringstream;
55 int getCols(string const & type)
57 if (type == "eqnarray")
61 if (type == "flalign")
63 if (type == "alignat")
65 if (type == "xalignat")
67 if (type == "xxalignat")
73 // returns position of first relation operator in the array
74 // used for "intelligent splitting"
75 size_t firstRelOp(MathArray const & ar)
77 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
79 return it - ar.begin();
84 char const * star(bool numbered)
86 return numbered ? "" : "*";
90 int typecode(string const & s)
92 if (s == "none") return 0;
93 if (s == "simple") return 1;
94 if (s == "equation") return 2;
95 if (s == "eqnarray") return 3;
96 if (s == "align") return 4;
97 if (s == "alignat") return 5;
98 if (s == "xalignat") return 6;
99 if (s == "xxalignat") return 7;
100 if (s == "multline") return 8;
101 if (s == "gather") return 9;
102 if (s == "flalign") return 10;
103 lyxerr << "unknown hull type '" << s << "'" << endl;
107 bool smaller(string const & s, string const & t)
109 return typecode(s) < typecode(t);
113 } // end anon namespace
117 MathHullInset::MathHullInset()
118 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
120 // This is needed as long the math parser is not re-entrant
122 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
123 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
124 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
125 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
130 MathHullInset::MathHullInset(string const & type)
131 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
137 auto_ptr<InsetBase> MathHullInset::clone() const
139 return auto_ptr<InsetBase>(new MathHullInset(*this));
143 MathInset::mode_type MathHullInset::currentMode() const
146 return UNDECIDED_MODE;
147 // definitely math mode ...
152 bool MathHullInset::idxFirst(LCursor & cur) const
160 bool MathHullInset::idxLast(LCursor & cur) const
162 cur.idx() = nargs() - 1;
163 cur.pos() = cur.lastpos();
168 char MathHullInset::defaultColAlign(col_type col)
170 if (type_ == "eqnarray")
172 if (typecode(type_) >= typecode("align"))
173 return "rl"[col & 1];
178 int MathHullInset::defaultColSpace(col_type col)
180 if (type_ == "align" || type_ == "alignat")
182 if (type_ == "xalignat")
183 return (col & 1) ? 20 : 0;
184 if (type_ == "xxalignat" || type_ == "flalign")
185 return (col & 1) ? 40 : 0;
190 char const * MathHullInset::standardFont() const
193 return "lyxnochange";
198 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
200 FontSetChanger dummy1(mi.base, standardFont());
201 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
203 // let the cells adjust themselves
204 MathGridInset::metrics(mi, dim);
211 if (numberedType()) {
212 FontSetChanger dummy(mi.base, "mathbf");
214 for (row_type row = 0; row < nrows(); ++row)
215 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
221 // make it at least as high as the current font
224 math_font_max_dim(mi.base.font, asc, des);
225 dim.asc = max(dim.asc, asc);
226 dim.des = max(dim.des, des);
229 metricsMarkers2(dim);
234 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
236 FontSetChanger dummy1(pi.base, standardFont());
237 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
238 MathGridInset::draw(pi, x + 1, y);
240 if (numberedType()) {
241 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
242 for (row_type row = 0; row < nrows(); ++row) {
243 int const yy = y + rowinfo_[row].offset_;
244 FontSetChanger dummy(pi.base, "mathrm");
245 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
249 drawMarkers2(pi, x, y);
253 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
256 MathGridInset::metricsT(mi, dim);
259 WriteStream wi(os, false, true);
261 dim.wid = os.str().size();
268 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
271 MathGridInset::drawT(pain, x, y);
274 WriteStream wi(os, false, true);
276 pain.draw(x, y, os.str().c_str());
281 string MathHullInset::label(row_type row) const
283 row_type n = nrows();
284 BOOST_ASSERT(row < n);
289 void MathHullInset::label(row_type row, string const & label)
291 //lyxerr << "setting label '" << label << "' for row " << row << endl;
296 void MathHullInset::numbered(row_type row, bool num)
302 bool MathHullInset::numbered(row_type row) const
308 bool MathHullInset::ams() const
312 type_ == "flalign" ||
313 type_ == "multline" ||
315 type_ == "alignat" ||
316 type_ == "xalignat" ||
317 type_ == "xxalignat";
321 bool MathHullInset::display() const
323 return type_ != "simple" && type_ != "none";
327 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
329 for (row_type row = 0; row < nrows(); ++row)
330 if (!label_[row].empty() && nonum_[row] != 1)
331 labels.push_back(label_[row]);
335 bool MathHullInset::numberedType() const
339 if (type_ == "simple")
341 if (type_ == "xxalignat")
343 for (row_type row = 0; row < nrows(); ++row)
350 void MathHullInset::validate(LaTeXFeatures & features) const
353 features.require("amsmath");
356 // Validation is necessary only if not using AMS math.
357 // To be safe, we will always run mathedvalidate.
358 //if (features.amsstyle)
361 features.require("boldsymbol");
362 //features.binom = true;
364 MathGridInset::validate(features);
368 void MathHullInset::header_write(WriteStream & os) const
370 bool n = numberedType();
375 else if (type_ == "simple") {
381 else if (type_ == "equation") {
383 os << "\\begin{equation" << star(n) << "}\n";
388 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
389 || type_ == "gather" || type_ == "multline")
390 os << "\\begin{" << type_ << star(n) << "}\n";
392 else if (type_ == "alignat" || type_ == "xalignat")
393 os << "\\begin{" << type_ << star(n) << '}'
394 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
396 else if (type_ == "xxalignat")
397 os << "\\begin{" << type_ << '}'
398 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
401 os << "\\begin{unknown" << star(n) << '}';
405 void MathHullInset::footer_write(WriteStream & os) const
407 bool n = numberedType();
412 else if (type_ == "simple")
415 else if (type_ == "equation")
417 os << "\\end{equation" << star(n) << "}\n";
421 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
422 || type_ == "alignat" || type_ == "xalignat"
423 || type_ == "gather" || type_ == "multline")
424 os << "\\end{" << type_ << star(n) << "}\n";
426 else if (type_ == "xxalignat")
427 os << "\\end{" << type_ << "}\n";
430 os << "\\end{unknown" << star(n) << '}';
434 bool MathHullInset::colChangeOK() const
437 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
438 type_ == "xalignat" || type_ == "xxalignat";
442 void MathHullInset::addRow(row_type row)
444 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
445 label_.insert(label_.begin() + row + 1, string());
446 MathGridInset::addRow(row);
450 void MathHullInset::swapRow(row_type row)
454 if (row + 1 == nrows())
456 swap(nonum_[row], nonum_[row + 1]);
457 swap(label_[row], label_[row + 1]);
458 MathGridInset::swapRow(row);
462 void MathHullInset::delRow(row_type row)
466 MathGridInset::delRow(row);
467 nonum_.erase(nonum_.begin() + row);
468 label_.erase(label_.begin() + row);
472 void MathHullInset::addCol(col_type col)
475 MathGridInset::addCol(col);
477 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
481 void MathHullInset::delCol(col_type col)
484 MathGridInset::delCol(col);
486 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
490 string MathHullInset::nicelabel(row_type row) const
494 if (label_[row].empty())
495 return string("(#)");
496 return '(' + label_[row] + ')';
500 void MathHullInset::glueall()
503 for (idx_type i = 0; i < nargs(); ++i)
505 *this = MathHullInset("simple");
511 string const & MathHullInset::getType() const
517 void MathHullInset::setType(string const & type)
525 void MathHullInset::mutate(string const & newtype)
527 lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
529 // we try to move along the chain
530 // none <-> simple <-> equation <-> eqnarray
532 if (newtype == "dump") {
536 else if (newtype == type_) {
540 else if (type_ == "none") {
546 else if (type_ == "simple") {
547 if (newtype == "none") {
556 else if (type_ == "equation") {
557 if (smaller(newtype, type_)) {
560 } else if (newtype == "eqnarray") {
561 MathGridInset::addCol(1);
562 MathGridInset::addCol(1);
564 // split it "nicely" on the firest relop
565 pos_type pos = firstRelOp(cell(0));
566 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
567 cell(0).erase(pos, cell(0).size());
569 if (cell(1).size()) {
570 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
571 cell(1).erase(1, cell(1).size());
575 } else if (newtype == "multline" || newtype == "gather") {
579 MathGridInset::addCol(1);
581 pos_type pos = firstRelOp(cell(0));
583 cell(0).erase(pos, cell(0).size());
584 cell(1).erase(0, pos);
590 else if (type_ == "eqnarray") {
591 if (smaller(newtype, type_)) {
592 // set correct (no)numbering
593 bool allnonum = true;
594 for (row_type row = 0; row < nrows(); ++row)
598 // set first non-empty label
600 for (row_type row = 0; row < nrows(); ++row) {
601 if (!label_[row].empty()) {
608 nonum_[0] = allnonum;
611 } else { // align & Co.
612 for (row_type row = 0; row < nrows(); ++row) {
613 idx_type c = 3 * row + 1;
614 cell(c).append(cell(c + 1));
616 MathGridInset::delCol(2);
622 else if (type_ == "align") {
623 if (smaller(newtype, type_)) {
624 MathGridInset::addCol(1);
632 else if (type_ == "multline") {
633 if (newtype == "gather" || newtype == "align" ||
634 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
636 else if (newtype == "eqnarray") {
637 MathGridInset::addCol(1);
638 MathGridInset::addCol(1);
641 lyxerr << "mutation from '" << type_
642 << "' to '" << newtype << "' not implemented" << endl;
646 else if (type_ == "gather") {
647 if (newtype == "multline") {
650 lyxerr << "mutation from '" << type_
651 << "' to '" << newtype << "' not implemented" << endl;
656 lyxerr << "mutation from '" << type_
657 << "' to '" << newtype << "' not implemented" << endl;
662 string MathHullInset::eolString(row_type row, bool fragile) const
665 if (numberedType()) {
666 if (!label_[row].empty() && !nonum_[row])
667 res += "\\label{" + label_[row] + '}';
668 if (nonum_[row] && (type_ != "multline"))
669 res += "\\nonumber ";
671 return res + MathGridInset::eolString(row, fragile);
675 void MathHullInset::write(WriteStream & os) const
678 MathGridInset::write(os);
683 void MathHullInset::normalize(NormalStream & os) const
685 os << "[formula " << type_ << ' ';
686 MathGridInset::normalize(os);
691 void MathHullInset::mathmlize(MathMLStream & os) const
693 MathGridInset::mathmlize(os);
697 void MathHullInset::infoize(ostream & os) const
699 os << "Type: " << type_;
703 void MathHullInset::check() const
705 BOOST_ASSERT(nonum_.size() == nrows());
706 BOOST_ASSERT(label_.size() == nrows());
710 void MathHullInset::doExtern(LCursor & cur, FuncRequest const & func)
714 istringstream iss(func.argument.c_str());
715 iss >> lang >> extra;
720 #warning temporarily disabled
721 //if (cur.selection()) {
724 // lyxerr << "use selection: " << ar << endl;
725 // insert(pipeThroughExtern(lang, extra, ar));
731 eq.push_back(MathAtom(new MathCharInset('=')));
733 // go to first item in line
734 cur.idx() -= cur.idx() % ncols();
737 if (getType() == "simple") {
738 size_type pos = cur.cell().find_last(eq);
740 if (cur.inMathed() && cur.selection()) {
741 asArray(cur.grabAndEraseSelection(), ar);
742 } else if (pos == cur.cell().size()) {
744 lyxerr << "use whole cell: " << ar << endl;
746 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
747 lyxerr << "use partial cell form pos: " << pos << endl;
749 cur.cell().append(eq);
750 cur.cell().append(pipeThroughExtern(lang, extra, ar));
751 cur.pos() = cur.lastpos();
755 if (getType() == "equation") {
756 lyxerr << "use equation inset" << endl;
758 MathArray & ar = cur.cell();
759 lyxerr << "use cell: " << ar << endl;
763 cur.cell() = pipeThroughExtern(lang, extra, ar);
764 // move to end of line
765 cur.pos() = cur.lastpos();
770 lyxerr << "use eqnarray" << endl;
771 cur.idx() += 2 - cur.idx() % ncols();
773 MathArray ar = cur.cell();
774 lyxerr << "use cell: " << ar << endl;
776 #warning temporarily disabled
783 cur.cell() = pipeThroughExtern(lang, extra, ar);
784 cur.pos() = cur.lastpos();
790 MathHullInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
792 lyxerr << "*** MathHullInset: request: " << cmd << endl;
793 switch (cmd.action) {
796 if (type_ == "simple" || type_ == "equation") {
800 return DispatchResult(true, FINISHED);
802 return MathGridInset::priv_dispatch(cur, cmd);
804 case LFUN_MATH_NUMBER:
805 //lyxerr << "toggling all numbers" << endl;
807 //recordUndo(cur, Undo::INSERT);
808 bool old = numberedType();
809 if (type_ == "multline")
810 numbered(nrows() - 1, !old);
812 for (row_type row = 0; row < nrows(); ++row)
814 //cur.bv()->owner()->message(old ? _("No number") : _("Number"));
816 return DispatchResult(true, true);
818 case LFUN_MATH_NONUMBER:
820 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
821 //recordUndo(cur, Undo::INSERT);
822 bool old = numbered(r);
823 //cur.bv()->owner()->message(old ? _("No number") : _("Number"));
826 return DispatchResult(true, true);
828 case LFUN_INSERT_LABEL: {
829 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
830 string old_label = label(r);
831 string new_label = cmd.argument;
833 if (new_label.empty()) {
834 string const default_label =
835 (lyxrc.label_init_length >= 0) ? "eq:" : "";
836 pair<bool, string> const res = old_label.empty()
837 ? Alert::askForText(_("Enter new label to insert:"), default_label)
838 : Alert::askForText(_("Enter label:"), old_label);
840 return DispatchResult(false);
841 new_label = trim(res.second);
844 //if (new_label == old_label)
845 // break; // Nothing to do
847 if (!new_label.empty())
850 return DispatchResult(true, true);
853 case LFUN_MATH_EXTERN:
855 return DispatchResult(true, FINISHED);
857 case LFUN_MATH_MUTATE: {
858 lyxerr << "Hull: MUTATE: " << cmd.argument << endl;
859 row_type r = cur.row();
860 col_type c = cur.col();
861 mutate(cmd.argument);
862 cur.idx() = r * ncols() + c;
863 if (cur.idx() >= nargs())
864 cur.idx() = nargs() - 1;
865 if (cur.pos() > cur.lastpos())
866 cur.pos() = cur.lastpos();
867 return DispatchResult(true, FINISHED);
870 case LFUN_MATH_DISPLAY: {
871 mutate(type_ == "simple" ? "equation" : "simple");
873 cur.pos() = cur.lastpos();
874 return DispatchResult(true, FINISHED);
878 return MathGridInset::priv_dispatch(cur, cmd);
883 string MathHullInset::fileInsetLabel() const
889 /////////////////////////////////////////////////////////////////////
891 #include "formulamacro.h"
892 #include "math_arrayinset.h"
893 #include "math_deliminset.h"
894 #include "math_factory.h"
895 #include "math_parser.h"
896 #include "math_spaceinset.h"
897 #include "ref_inset.h"
899 #include "bufferview_funcs.h"
903 #include "frontends/LyXView.h"
904 #include "frontends/Dialogs.h"
906 #include "support/std_sstream.h"
907 #include "support/lstrings.h"
908 #include "support/lyxlib.h"
922 int MathHullInset::ylow() const
924 return yo_ - dim_.asc;
928 int MathHullInset::yhigh() const
930 return yo_ + dim_.des;
934 int MathHullInset::xlow() const
940 int MathHullInset::xhigh() const
942 return xo_ + dim_.wid;
946 // simply scrap this function if you want
947 void MathHullInset::mutateToText()
950 // translate to latex
952 latex(NULL, os, false, false);
953 string str = os.str();
956 LyXText * lt = view_->getLyXText();
957 string::const_iterator cit = str.begin();
958 string::const_iterator end = str.end();
959 for (; cit != end; ++cit)
960 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
963 //view_->owner()->dispatch(LFUN_ESCAPE);
968 void MathHullInset::handleFont
969 (LCursor & cur, string const & arg, string const & font)
971 // this whole function is a hack and won't work for incremental font
973 recordUndo(cur, Undo::ATOMIC);
975 if (cur.inset()->asMathInset()->name() == font)
976 cur.handleFont(font);
978 cur.handleNest(createMathInset(font));
984 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
986 recordUndo(cur, Undo::ATOMIC);
989 bv_funcs::string2font(arg, font, b);
990 if (font.color() != LColor::inherit) {
991 MathAtom at = createMathInset("color");
992 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
993 cur.handleNest(at, 1);
998 string const MathHullInset::editMessage() const
1000 return _("Math editor mode");
1004 void MathHullInset::insetUnlock(BufferView & bv)
1006 if (bv.cursor().inMathed()) {
1007 if (bv.cursor().inMacroMode())
1008 bv.cursor().macroModeClose();
1009 bv.cursor().releaseMathCursor();
1012 generatePreview(*bv.buffer());
1017 void MathHullInset::getCursorDim(int & asc, int & desc) const
1021 //math_font_max_dim(font_, asc, des);
1026 MathHullInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
1028 if (!cur.inMathed())
1029 return DispatchResult(false);
1031 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
1033 if (cmd.button() == mouse_button::button3) {
1034 // try to dispatch to enclosed insets first
1035 if (!cur.dispatch(cmd).dispatched()) {
1036 // launch math panel for right mouse button
1037 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
1038 cur.bv().owner()->getDialogs().show("mathpanel");
1040 return DispatchResult(true, true);
1043 if (cmd.button() == mouse_button::button2) {
1045 asArray(cur.bv().getClipboard(), ar);
1047 cur.setScreenPos(cmd.x, cmd.y);
1050 return DispatchResult(true, true);
1053 if (cmd.button() == mouse_button::button1) {
1054 // try to dispatch to enclosed insets first
1056 cur.bv().stuffClipboard(cur.grabSelection());
1057 return DispatchResult(true, true);
1060 return DispatchResult(false);
1065 MathHullInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
1067 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
1069 if (!cur.inMathed() || cur.formula() != this) {
1070 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
1071 cur.releaseMathCursor();
1074 cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
1077 if (cmd.button() == mouse_button::button3) {
1079 return DispatchResult(true, true);
1082 if (cmd.button() == mouse_button::button1) {
1086 cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
1088 return DispatchResult(true, true);
1092 return DispatchResult(true, true);
1097 MathHullInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
1099 if (!cur.inMathed())
1100 return DispatchResult(true, true);
1102 if (cur.dispatch(FuncRequest(cmd)).dispatched())
1103 return DispatchResult(true, true);
1105 // only select with button 1
1106 if (cmd.button() != mouse_button::button1)
1107 return DispatchResult(true, true);
1109 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
1110 return DispatchResult(true, true);
1115 if (!cur.selection())
1118 cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
1120 return DispatchResult(true, true);
1124 void MathHullInset::edit(LCursor & cur, bool left)
1126 lyxerr << "Called FormulaBase::edit" << endl;
1128 cur.idx() = left ? 0 : cur.lastidx();
1129 cur.pos() = left ? 0 : cur.lastpos();
1135 void MathHullInset::edit(LCursor & cur, int x, int y)
1137 lyxerr << "Called MathHullInset::edit with '" << x << ' ' << y << "'" << endl;
1139 //cur.idx() = left ? 0 : cur.lastidx();
1142 cur.setScreenPos(x + xo_, y + yo_);
1143 // if that is removed, we won't get the magenta box when entering an
1144 // inset for the first time
1150 void MathHullInset::revealCodes(LCursor & cur) const
1152 if (!cur.inMathed())
1156 cur.bv().owner()->message(os.str());
1158 // write something to the minibuffer
1159 // translate to latex
1163 string str = os.str();
1165 string::size_type pos = 0;
1167 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1170 else if (*it == '\0') {
1172 pos = it - str.begin();
1178 res = res.substr(pos - 30);
1179 if (res.size() > 60)
1180 res = res.substr(0, 60);
1181 bv.owner()->message(res);
1186 InsetBase::Code MathHullInset::lyxCode() const
1192 /////////////////////////////////////////////////////////////////////
1196 bool MathHullInset::searchForward(BufferView *, string const &, bool, bool)
1202 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1206 #ifdef WITH_WARNINGS
1207 #warning pretty ugly
1209 static MathHullInset * lastformula = 0;
1210 static CursorBase current = CursorBase(ibegin(nucleus()));
1211 static MathArray ar;
1212 static string laststr;
1214 if (lastformula != this || laststr != str) {
1215 //lyxerr << "reset lastformula to " << this << endl;
1218 current = ibegin(nucleus());
1220 mathed_parse_cell(ar, str);
1224 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1226 for (CursorBase it = current; it != iend(nucleus()); increment(it)) {
1227 CursorSlice & top = it.back();
1228 MathArray const & a = top.asMathInset()->cell(top.idx_);
1229 if (a.matchpart(ar, top.pos_)) {
1230 bv->cursor().setSelection(it, ar.size());
1232 top.pos_ += ar.size();
1238 //lyxerr << "not found!" << endl;
1245 bool MathHullInset::searchBackward(BufferView * bv, string const & what,
1248 lyxerr[Debug::MATHED]
1249 << "searching backward not implemented in mathed" << endl;
1250 return searchForward(bv, what, a, b);
1254 void MathHullInset::write(Buffer const &, std::ostream & os) const
1256 WriteStream wi(os, false, false);
1257 os << fileInsetLabel() << ' ';
1262 void MathHullInset::read(Buffer const &, LyXLex & lex)
1265 mathed_parse_normal(at, lex);
1266 operator=(*at->asHullInset());
1270 int MathHullInset::latex(Buffer const &, ostream & os,
1271 OutputParams const & runparams) const
1273 WriteStream wi(os, runparams.moving_arg, true);
1279 int MathHullInset::plaintext(Buffer const &, ostream & os,
1280 OutputParams const &) const
1282 if (0 && display()) {
1286 TextPainter tpain(dim.width(), dim.height());
1287 drawT(tpain, 0, dim.ascent());
1289 // reset metrics cache to "real" values
1291 return tpain.textheight();
1293 WriteStream wi(os, false, true);
1294 wi << ' ' << cell(0) << ' ';
1300 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1301 OutputParams const & runparams) const
1303 return docbook(buf, os, runparams);
1307 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1308 OutputParams const & runparams) const
1310 MathMLStream ms(os);
1311 ms << MTag("equation");
1314 int res = plaintext(buf, ms.os(), runparams);
1318 MathGridInset::mathmlize(ms);
1320 ms << ETag("equation");
1321 return ms.line() + res;
1326 /////////////////////////////////////////////
1330 bool openNewInset(LCursor & cur, InsetBase * inset)
1332 if (!cur.bv().insertInset(inset)) {
1336 inset->edit(cur, true);
1341 void mathDispatchCreation(LCursor & cur, FuncRequest const & cmd,
1344 // use selection if available..
1346 //if (action == LFUN_MATH_IMPORT_SELECTION)
1351 cur.bv().getLyXText()->selectionAsString(*cur.bv().buffer(), false);
1354 InsetBase * f = new MathHullInset;
1355 if (openNewInset(cur, f)) {
1356 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
1357 // don't do that also for LFUN_MATH_MODE unless you want end up with
1358 // always changing to mathrm when opening an inlined inset
1359 // -- I really hate "LyXfunc overloading"...
1361 f->dispatch(cur, FuncRequest(LFUN_MATH_DISPLAY));
1362 f->dispatch(cur, FuncRequest(LFUN_INSERT_MATH, cmd.argument));
1365 // create a macro if we see "\\newcommand" somewhere, and an ordinary
1366 // formula otherwise
1368 if (sel.find("\\newcommand") == string::npos &&
1369 sel.find("\\def") == string::npos)
1370 f = new MathHullInset(sel);
1372 f = new InsetFormulaMacro(sel);
1373 cur.bv().getLyXText()->cutSelection(true, false);
1374 openNewInset(cur, f);
1376 cmd.message(N_("Math editor mode"));
1382 void mathDispatch(LCursor & cur, FuncRequest const & cmd)
1384 if (!cur.bv().available())
1387 switch (cmd.action) {
1389 case LFUN_MATH_DISPLAY:
1390 mathDispatchCreation(cur, cmd, true);
1393 case LFUN_MATH_MODE:
1394 mathDispatchCreation(cur, cmd, false);
1397 case LFUN_MATH_IMPORT_SELECTION:
1398 mathDispatchCreation(cur, cmd, false);
1402 case LFUN_MATH_MACRO:
1403 if (cmd.argument.empty())
1404 cmd.errorMessage(N_("Missing argument"));
1406 string s = cmd.argument;
1407 string const s1 = token(s, ' ', 1);
1408 int const nargs = s1.empty() ? 0 : atoi(s1);
1409 string const s2 = token(s, ' ', 2);
1410 string const type = s2.empty() ? "newcommand" : s2;
1411 openNewInset(cur, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
1415 case LFUN_INSERT_MATH:
1416 case LFUN_INSERT_MATRIX:
1417 case LFUN_MATH_DELIM: {
1418 MathHullInset * f = new MathHullInset;
1419 if (openNewInset(cur, f)) {
1420 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
1421 cur.inset()->dispatch(cur, cmd);