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 "textpainter.h"
32 #include "frontends/Alert.h"
34 #include "support/std_sstream.h"
37 using lyx::support::trim;
44 using std::istringstream;
46 using std::ostringstream;
54 int getCols(string const & type)
56 if (type == "eqnarray")
60 if (type == "flalign")
62 if (type == "alignat")
64 if (type == "xalignat")
66 if (type == "xxalignat")
72 // returns position of first relation operator in the array
73 // used for "intelligent splitting"
74 size_t firstRelOp(MathArray const & ar)
76 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
78 return it - ar.begin();
83 char const * star(bool numbered)
85 return numbered ? "" : "*";
89 int typecode(string const & s)
91 if (s == "none") return 0;
92 if (s == "simple") return 1;
93 if (s == "equation") return 2;
94 if (s == "eqnarray") return 3;
95 if (s == "align") return 4;
96 if (s == "alignat") return 5;
97 if (s == "xalignat") return 6;
98 if (s == "xxalignat") return 7;
99 if (s == "multline") return 8;
100 if (s == "gather") return 9;
101 if (s == "flalign") return 10;
102 lyxerr << "unknown hull type '" << s << "'" << endl;
106 bool smaller(string const & s, string const & t)
108 return typecode(s) < typecode(t);
112 } // end anon namespace
115 MathHullInset::MathHullInset()
116 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
118 // This is needed as long the math parser is not re-entrant
120 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
121 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
122 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
123 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
128 MathHullInset::MathHullInset(string const & type)
129 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
135 auto_ptr<InsetBase> MathHullInset::clone() const
137 return auto_ptr<InsetBase>(new MathHullInset(*this));
141 MathInset::mode_type MathHullInset::currentMode() const
144 return UNDECIDED_MODE;
145 // definitely math mode ...
150 bool MathHullInset::idxFirst(LCursor & cur) const
158 bool MathHullInset::idxLast(LCursor & cur) const
160 cur.idx() = nargs() - 1;
161 cur.pos() = cur.lastpos();
166 char MathHullInset::defaultColAlign(col_type col)
168 if (type_ == "eqnarray")
170 if (typecode(type_) >= typecode("align"))
171 return "rl"[col & 1];
176 int MathHullInset::defaultColSpace(col_type col)
178 if (type_ == "align" || type_ == "alignat")
180 if (type_ == "xalignat")
181 return (col & 1) ? 20 : 0;
182 if (type_ == "xxalignat" || type_ == "flalign")
183 return (col & 1) ? 40 : 0;
188 char const * MathHullInset::standardFont() const
191 return "lyxnochange";
196 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
198 FontSetChanger dummy1(mi.base, standardFont());
199 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
201 // let the cells adjust themselves
202 MathGridInset::metrics(mi);
209 if (numberedType()) {
210 FontSetChanger dummy(mi.base, "mathbf");
212 for (row_type row = 0; row < nrows(); ++row)
213 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
219 // make it at least as high as the current font
222 math_font_max_dim(mi.base.font, asc, des);
223 dim_.asc = max(dim_.asc, asc);
224 dim_.des = max(dim_.des, des);
232 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
234 FontSetChanger dummy1(pi.base, standardFont());
235 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
236 MathGridInset::draw(pi, x + 1, y);
238 if (numberedType()) {
239 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
240 for (row_type row = 0; row < nrows(); ++row) {
241 int const yy = y + rowinfo_[row].offset_;
242 FontSetChanger dummy(pi.base, "mathrm");
243 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
247 drawMarkers2(pi, x, y);
251 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
254 MathGridInset::metricsT(mi, dim);
257 WriteStream wi(os, false, true);
259 dim.wid = os.str().size();
266 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
269 MathGridInset::drawT(pain, x, y);
272 WriteStream wi(os, false, true);
274 pain.draw(x, y, os.str().c_str());
279 string MathHullInset::label(row_type row) const
281 row_type n = nrows();
282 BOOST_ASSERT(row < n);
287 void MathHullInset::label(row_type row, string const & label)
289 //lyxerr << "setting label '" << label << "' for row " << row << endl;
294 void MathHullInset::numbered(row_type row, bool num)
300 bool MathHullInset::numbered(row_type row) const
306 bool MathHullInset::ams() const
310 type_ == "flalign" ||
311 type_ == "multline" ||
313 type_ == "alignat" ||
314 type_ == "xalignat" ||
315 type_ == "xxalignat";
319 bool MathHullInset::display() const
321 return type_ != "simple" && type_ != "none";
325 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
327 for (row_type row = 0; row < nrows(); ++row)
328 if (!label_[row].empty() && nonum_[row] != 1)
329 labels.push_back(label_[row]);
333 bool MathHullInset::numberedType() const
337 if (type_ == "simple")
339 if (type_ == "xxalignat")
341 for (row_type row = 0; row < nrows(); ++row)
348 void MathHullInset::validate(LaTeXFeatures & features) const
351 features.require("amsmath");
354 // Validation is necessary only if not using AMS math.
355 // To be safe, we will always run mathedvalidate.
356 //if (features.amsstyle)
359 features.require("boldsymbol");
360 //features.binom = true;
362 MathGridInset::validate(features);
366 void MathHullInset::header_write(WriteStream & os) const
368 bool n = numberedType();
373 else if (type_ == "simple") {
379 else if (type_ == "equation") {
381 os << "\\begin{equation" << star(n) << "}\n";
386 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
387 || type_ == "gather" || type_ == "multline")
388 os << "\\begin{" << type_ << star(n) << "}\n";
390 else if (type_ == "alignat" || type_ == "xalignat")
391 os << "\\begin{" << type_ << star(n) << '}'
392 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
394 else if (type_ == "xxalignat")
395 os << "\\begin{" << type_ << '}'
396 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
399 os << "\\begin{unknown" << star(n) << '}';
403 void MathHullInset::footer_write(WriteStream & os) const
405 bool n = numberedType();
410 else if (type_ == "simple")
413 else if (type_ == "equation")
415 os << "\\end{equation" << star(n) << "}\n";
419 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
420 || type_ == "alignat" || type_ == "xalignat"
421 || type_ == "gather" || type_ == "multline")
422 os << "\\end{" << type_ << star(n) << "}\n";
424 else if (type_ == "xxalignat")
425 os << "\\end{" << type_ << "}\n";
428 os << "\\end{unknown" << star(n) << '}';
432 bool MathHullInset::colChangeOK() const
435 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
436 type_ == "xalignat" || type_ == "xxalignat";
440 void MathHullInset::addRow(row_type row)
442 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
443 label_.insert(label_.begin() + row + 1, string());
444 MathGridInset::addRow(row);
448 void MathHullInset::swapRow(row_type row)
452 if (row + 1 == nrows())
454 swap(nonum_[row], nonum_[row + 1]);
455 swap(label_[row], label_[row + 1]);
456 MathGridInset::swapRow(row);
460 void MathHullInset::delRow(row_type row)
464 MathGridInset::delRow(row);
465 nonum_.erase(nonum_.begin() + row);
466 label_.erase(label_.begin() + row);
470 void MathHullInset::addCol(col_type col)
473 MathGridInset::addCol(col);
475 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
479 void MathHullInset::delCol(col_type col)
482 MathGridInset::delCol(col);
484 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
488 string MathHullInset::nicelabel(row_type row) const
492 if (label_[row].empty())
493 return string("(#)");
494 return '(' + label_[row] + ')';
498 void MathHullInset::glueall()
501 for (idx_type i = 0; i < nargs(); ++i)
503 *this = MathHullInset("simple");
509 string const & MathHullInset::getType() const
515 void MathHullInset::setType(string const & type)
523 void MathHullInset::mutate(string const & newtype)
525 lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
527 // we try to move along the chain
528 // none <-> simple <-> equation <-> eqnarray
530 if (newtype == "dump") {
534 else if (newtype == type_) {
538 else if (type_ == "none") {
544 else if (type_ == "simple") {
545 if (newtype == "none") {
554 else if (type_ == "equation") {
555 if (smaller(newtype, type_)) {
558 } else if (newtype == "eqnarray") {
559 MathGridInset::addCol(1);
560 MathGridInset::addCol(1);
562 // split it "nicely" on the firest relop
563 pos_type pos = firstRelOp(cell(0));
564 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
565 cell(0).erase(pos, cell(0).size());
567 if (cell(1).size()) {
568 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
569 cell(1).erase(1, cell(1).size());
573 } else if (newtype == "multline" || newtype == "gather") {
577 MathGridInset::addCol(1);
579 pos_type pos = firstRelOp(cell(0));
581 cell(0).erase(pos, cell(0).size());
582 cell(1).erase(0, pos);
588 else if (type_ == "eqnarray") {
589 if (smaller(newtype, type_)) {
590 // set correct (no)numbering
591 bool allnonum = true;
592 for (row_type row = 0; row < nrows(); ++row)
596 // set first non-empty label
598 for (row_type row = 0; row < nrows(); ++row) {
599 if (!label_[row].empty()) {
606 nonum_[0] = allnonum;
609 } else { // align & Co.
610 for (row_type row = 0; row < nrows(); ++row) {
611 idx_type c = 3 * row + 1;
612 cell(c).append(cell(c + 1));
614 MathGridInset::delCol(2);
620 else if (type_ == "align") {
621 if (smaller(newtype, type_)) {
622 MathGridInset::addCol(1);
630 else if (type_ == "multline") {
631 if (newtype == "gather" || newtype == "align" ||
632 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
634 else if (newtype == "eqnarray") {
635 MathGridInset::addCol(1);
636 MathGridInset::addCol(1);
639 lyxerr << "mutation from '" << type_
640 << "' to '" << newtype << "' not implemented" << endl;
644 else if (type_ == "gather") {
645 if (newtype == "multline") {
648 lyxerr << "mutation from '" << type_
649 << "' to '" << newtype << "' not implemented" << endl;
654 lyxerr << "mutation from '" << type_
655 << "' to '" << newtype << "' not implemented" << endl;
660 string MathHullInset::eolString(row_type row, bool fragile) const
663 if (numberedType()) {
664 if (!label_[row].empty() && !nonum_[row])
665 res += "\\label{" + label_[row] + '}';
666 if (nonum_[row] && (type_ != "multline"))
667 res += "\\nonumber ";
669 return res + MathGridInset::eolString(row, fragile);
673 void MathHullInset::write(WriteStream & os) const
676 MathGridInset::write(os);
681 void MathHullInset::normalize(NormalStream & os) const
683 os << "[formula " << type_ << ' ';
684 MathGridInset::normalize(os);
689 void MathHullInset::mathmlize(MathMLStream & os) const
691 MathGridInset::mathmlize(os);
695 void MathHullInset::infoize(ostream & os) const
697 os << "Type: " << type_;
701 void MathHullInset::check() const
703 BOOST_ASSERT(nonum_.size() == nrows());
704 BOOST_ASSERT(label_.size() == nrows());
708 void MathHullInset::doExtern(LCursor & cur, FuncRequest const & func)
712 istringstream iss(func.argument.c_str());
713 iss >> lang >> extra;
718 #warning temporarily disabled
719 //if (cur.selection()) {
722 // lyxerr << "use selection: " << ar << endl;
723 // insert(pipeThroughExtern(lang, extra, ar));
729 eq.push_back(MathAtom(new MathCharInset('=')));
731 // go to first item in line
732 cur.idx() -= cur.idx() % ncols();
735 if (getType() == "simple") {
736 size_type pos = cur.cell().find_last(eq);
738 if (cur.inMathed() && cur.selection()) {
739 asArray(cur.grabAndEraseSelection(), ar);
740 } else if (pos == cur.cell().size()) {
742 lyxerr << "use whole cell: " << ar << endl;
744 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
745 lyxerr << "use partial cell form pos: " << pos << endl;
747 cur.cell().append(eq);
748 cur.cell().append(pipeThroughExtern(lang, extra, ar));
749 cur.pos() = cur.lastpos();
753 if (getType() == "equation") {
754 lyxerr << "use equation inset" << endl;
756 MathArray & ar = cur.cell();
757 lyxerr << "use cell: " << ar << endl;
761 cur.cell() = pipeThroughExtern(lang, extra, ar);
762 // move to end of line
763 cur.pos() = cur.lastpos();
768 lyxerr << "use eqnarray" << endl;
769 cur.idx() += 2 - cur.idx() % ncols();
771 MathArray ar = cur.cell();
772 lyxerr << "use cell: " << ar << endl;
774 #warning temporarily disabled
781 cur.cell() = pipeThroughExtern(lang, extra, ar);
782 cur.pos() = cur.lastpos();
788 MathHullInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
790 switch (cmd.action) {
793 if (type_ == "simple" || type_ == "equation") {
797 return DispatchResult(true, FINISHED);
799 return MathGridInset::priv_dispatch(cur, cmd);
801 case LFUN_MATH_NUMBER:
802 //lyxerr << "toggling all numbers" << endl;
804 //recordUndo(cur, Undo::INSERT);
805 bool old = numberedType();
806 if (type_ == "multline")
807 numbered(nrows() - 1, !old);
809 for (row_type row = 0; row < nrows(); ++row)
811 //cur.bv()->owner()->message(old ? _("No number") : _("Number"));
813 return DispatchResult(true, true);
815 case LFUN_MATH_NONUMBER:
817 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
818 //recordUndo(cur, Undo::INSERT);
819 bool old = numbered(r);
820 //cur.bv()->owner()->message(old ? _("No number") : _("Number"));
823 return DispatchResult(true, true);
825 case LFUN_INSERT_LABEL: {
826 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
827 string old_label = label(r);
828 string new_label = cmd.argument;
830 if (new_label.empty()) {
831 string const default_label =
832 (lyxrc.label_init_length >= 0) ? "eq:" : "";
833 pair<bool, string> const res = old_label.empty()
834 ? Alert::askForText(_("Enter new label to insert:"), default_label)
835 : Alert::askForText(_("Enter label:"), old_label);
837 return DispatchResult(false);
838 new_label = trim(res.second);
841 //if (new_label == old_label)
842 // break; // Nothing to do
844 if (!new_label.empty())
847 return DispatchResult(true, true);
850 case LFUN_MATH_EXTERN:
852 return DispatchResult(true, FINISHED);
854 case LFUN_MATH_MUTATE: {
855 lyxerr << "Hull: MUTATE: " << cmd.argument << endl;
856 row_type r = cur.row();
857 col_type c = cur.col();
858 mutate(cmd.argument);
859 cur.idx() = r * ncols() + c;
860 if (cur.idx() >= nargs())
861 cur.idx() = nargs() - 1;
862 if (cur.pos() > cur.lastpos())
863 cur.pos() = cur.lastpos();
864 return DispatchResult(true, FINISHED);
867 case LFUN_MATH_DISPLAY: {
868 mutate(type_ == "simple" ? "equation" : "simple");
870 cur.pos() = cur.lastpos();
871 return DispatchResult(true, FINISHED);
875 return MathGridInset::priv_dispatch(cur, cmd);
880 string MathHullInset::fileInsetLabel() const
886 /////////////////////////////////////////////////////////////////////
888 #include "formulamacro.h"
889 #include "math_arrayinset.h"
890 #include "math_data.h"
891 #include "math_deliminset.h"
892 #include "math_factory.h"
893 #include "math_hullinset.h"
894 #include "math_parser.h"
895 #include "math_spaceinset.h"
896 #include "math_support.h"
897 #include "ref_inset.h"
899 #include "BufferView.h"
900 #include "bufferview_funcs.h"
902 #include "dispatchresult.h"
904 #include "funcrequest.h"
910 #include "frontends/LyXView.h"
911 #include "frontends/Dialogs.h"
913 #include "support/std_sstream.h"
914 #include "support/lstrings.h"
915 #include "support/lyxlib.h"
917 using lyx::support::atoi;
918 using lyx::support::split;
919 using lyx::support::token;
924 using std::istringstream;
925 using std::ostringstream;
934 bool openNewInset(LCursor & cur, InsetBase * inset)
936 if (!cur.bv().insertInset(inset)) {
940 inset->edit(cur, true);
949 int MathHullInset::ylow() const
951 return yo_ - dim_.asc;
955 int MathHullInset::yhigh() const
957 return yo_ + dim_.des;
961 int MathHullInset::xlow() const
967 int MathHullInset::xhigh() const
969 return xo_ + dim_.wid;
973 // simply scrap this function if you want
974 void MathHullInset::mutateToText()
977 // translate to latex
979 latex(NULL, os, false, false);
980 string str = os.str();
983 LyXText * lt = view_->getLyXText();
984 string::const_iterator cit = str.begin();
985 string::const_iterator end = str.end();
986 for (; cit != end; ++cit)
987 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
990 //view_->owner()->dispatch(LFUN_ESCAPE);
995 void MathHullInset::handleFont
996 (LCursor & cur, string const & arg, string const & font)
998 // this whole function is a hack and won't work for incremental font
1000 recordUndo(cur, Undo::ATOMIC);
1002 if (cur.inset()->asMathInset()->name() == font)
1003 cur.handleFont(font);
1005 cur.handleNest(createMathInset(font));
1011 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1013 recordUndo(cur, Undo::ATOMIC);
1016 bv_funcs::string2font(arg, font, b);
1017 if (font.color() != LColor::inherit) {
1018 MathAtom at = createMathInset("color");
1019 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
1020 cur.handleNest(at, 1);
1025 string const MathHullInset::editMessage() const
1027 return _("Math editor mode");
1031 void MathHullInset::insetUnlock(BufferView & bv)
1033 if (bv.cursor().inMathed()) {
1034 if (bv.cursor().inMacroMode())
1035 bv.cursor().macroModeClose();
1036 bv.cursor().releaseMathCursor();
1039 generatePreview(*bv.buffer());
1044 void MathHullInset::getCursorPos(BufferView & bv, int & x, int & y) const
1046 if (bv.cursor().inMathed()) {
1047 bv.cursor().getScreenPos(x, y);
1048 x = bv.cursor().targetX();
1051 lyxerr << "MathHullInset::getCursorPos: " << x << ' ' << y << endl;
1055 lyxerr << "getCursorPos - should not happen";
1060 void MathHullInset::getCursorDim(int & asc, int & desc) const
1064 //math_font_max_dim(font_, asc, des);
1069 MathHullInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
1071 if (!cur.inMathed())
1072 return DispatchResult(false);
1074 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
1076 if (cmd.button() == mouse_button::button3) {
1077 // try to dispatch to enclosed insets first
1078 if (!cur.dispatch(cmd).dispatched()) {
1079 // launch math panel for right mouse button
1080 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
1081 cur.bv().owner()->getDialogs().show("mathpanel");
1083 return DispatchResult(true, true);
1086 if (cmd.button() == mouse_button::button2) {
1088 asArray(cur.bv().getClipboard(), ar);
1090 cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
1093 return DispatchResult(true, true);
1096 if (cmd.button() == mouse_button::button1) {
1097 // try to dispatch to enclosed insets first
1099 cur.bv().stuffClipboard(cur.grabSelection());
1100 // try to set the cursor
1101 //delete mathcursor;
1102 //mathcursor = new MathCursor(bv, this, x == 0);
1104 //cur.setScreenPos(x + xo_, y + yo_);
1105 return DispatchResult(true, true);
1108 return DispatchResult(false);
1113 MathHullInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
1115 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
1117 if (!cur.inMathed() || cur.formula() != this) {
1118 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
1119 cur.releaseMathCursor();
1122 cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
1125 if (cmd.button() == mouse_button::button3) {
1127 return DispatchResult(true, true);
1130 if (cmd.button() == mouse_button::button1) {
1134 cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
1136 return DispatchResult(true, true);
1140 return DispatchResult(true, true);
1145 MathHullInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
1147 if (!cur.inMathed())
1148 return DispatchResult(true, true);
1150 if (cur.dispatch(FuncRequest(cmd)).dispatched())
1151 return DispatchResult(true, true);
1153 // only select with button 1
1154 if (cmd.button() != mouse_button::button1)
1155 return DispatchResult(true, true);
1157 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
1158 return DispatchResult(true, true);
1163 if (!cur.selection())
1166 cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
1168 return DispatchResult(true, true);
1172 void MathHullInset::edit(LCursor & cur, bool left)
1174 lyxerr << "Called FormulaBase::edit" << endl;
1176 cur.idx() = left ? 0 : cur.lastidx();
1177 cur.pos() = left ? 0 : cur.lastpos();
1182 void MathHullInset::edit(LCursor & cur, int x, int y)
1184 lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
1187 //cur.idx() = left ? 0 : cur.lastidx();
1190 cur.setScreenPos(x + xo_, y + yo_);
1191 // if that is removed, we won't get the magenta box when entering an
1192 // inset for the first time
1199 void MathHullInset::revealCodes(LCursor & cur) const
1201 if (!cur.inMathed())
1205 cur.bv().owner()->message(os.str());
1207 // write something to the minibuffer
1208 // translate to latex
1212 string str = os.str();
1214 string::size_type pos = 0;
1216 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1219 else if (*it == '\0') {
1221 pos = it - str.begin();
1227 res = res.substr(pos - 30);
1228 if (res.size() > 60)
1229 res = res.substr(0, 60);
1230 bv.owner()->message(res);
1235 InsetBase::Code MathHullInset::lyxCode() const
1241 /////////////////////////////////////////////////////////////////////
1245 bool MathHullInset::searchForward(BufferView *, string const &, bool, bool)
1251 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1255 #ifdef WITH_WARNINGS
1256 #warning pretty ugly
1258 static MathHullInset * lastformula = 0;
1259 static CursorBase current = CursorBase(ibegin(par().nucleus()));
1260 static MathArray ar;
1261 static string laststr;
1263 if (lastformula != this || laststr != str) {
1264 //lyxerr << "reset lastformula to " << this << endl;
1267 current = ibegin(par().nucleus());
1269 mathed_parse_cell(ar, str);
1273 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1275 for (CursorBase it = current; it != iend(par().nucleus()); increment(it)) {
1276 CursorSlice & top = it.back();
1277 MathArray const & a = top.asMathInset()->cell(top.idx_);
1278 if (a.matchpart(ar, top.pos_)) {
1279 bv->cursor().setSelection(it, ar.size());
1281 top.pos_ += ar.size();
1287 //lyxerr << "not found!" << endl;
1294 bool MathHullInset::searchBackward(BufferView * bv, string const & what,
1297 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
1298 return searchForward(bv, what, a, b);
1302 void mathDispatchCreation(LCursor & cur, FuncRequest const & cmd,
1305 // use selection if available..
1307 //if (action == LFUN_MATH_IMPORT_SELECTION)
1312 cur.bv().getLyXText()->selectionAsString(*cur.bv().buffer(), false);
1315 InsetBase * f = new MathHullInset;
1316 if (openNewInset(cur, f)) {
1317 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
1318 // don't do that also for LFUN_MATH_MODE unless you want end up with
1319 // always changing to mathrm when opening an inlined inset
1320 // -- I really hate "LyXfunc overloading"...
1322 f->dispatch(cur, FuncRequest(LFUN_MATH_DISPLAY));
1323 f->dispatch(cur, FuncRequest(LFUN_INSERT_MATH, cmd.argument));
1326 // create a macro if we see "\\newcommand" somewhere, and an ordinary
1327 // formula otherwise
1329 if (sel.find("\\newcommand") == string::npos &&
1330 sel.find("\\def") == string::npos)
1331 f = new MathHullInset(sel);
1333 f = new InsetFormulaMacro(sel);
1334 cur.bv().getLyXText()->cutSelection(true, false);
1335 openNewInset(cur, f);
1337 cmd.message(N_("Math editor mode"));
1341 void mathDispatch(LCursor & cur, FuncRequest const & cmd)
1343 if (!cur.bv().available())
1346 switch (cmd.action) {
1348 case LFUN_MATH_DISPLAY:
1349 mathDispatchCreation(cur, cmd, true);
1352 case LFUN_MATH_MODE:
1353 mathDispatchCreation(cur, cmd, false);
1356 case LFUN_MATH_IMPORT_SELECTION:
1357 mathDispatchCreation(cur, cmd, false);
1361 case LFUN_MATH_MACRO:
1362 if (cmd.argument.empty())
1363 cmd.errorMessage(N_("Missing argument"));
1365 string s = cmd.argument;
1366 string const s1 = token(s, ' ', 1);
1367 int const nargs = s1.empty() ? 0 : atoi(s1);
1368 string const s2 = token(s, ' ', 2);
1369 string const type = s2.empty() ? "newcommand" : s2;
1370 openNewInset(cur, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
1374 case LFUN_INSERT_MATH:
1375 case LFUN_INSERT_MATRIX:
1376 case LFUN_MATH_DELIM: {
1377 MathHullInset * f = new MathHullInset;
1378 if (openNewInset(cur, f)) {
1379 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
1380 cur.inset()->dispatch(cur, cmd);