]> git.lyx.org Git - features.git/blob - src/mathed/math_hullinset.C
swallow <Return> events in mathed. Should mimic 1.3.x behaviour.
[features.git] / src / mathed / math_hullinset.C
1 /**
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.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
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"
20
21 #include "BufferView.h"
22 #include "cursor.h"
23 #include "dispatchresult.h"
24 #include "debug.h"
25 #include "funcrequest.h"
26 #include "gettext.h"
27 #include "LaTeXFeatures.h"
28 #include "LColor.h"
29 #include "lyxrc.h"
30 #include "outputparams.h"
31 #include "textpainter.h"
32 #include "undo.h"
33
34 #include "frontends/Alert.h"
35
36 #include "support/std_sstream.h"
37
38
39 using std::endl;
40 using std::max;
41 using std::string;
42 using std::ostream;
43 using std::auto_ptr;
44 using std::istringstream;
45 using std::ostream;
46 using std::ostringstream;
47 using std::pair;
48 using std::swap;
49 using std::vector;
50
51
52 namespace {
53
54         int getCols(string const & type)
55         {
56                 if (type == "eqnarray")
57                         return 3;
58                 if (type == "align")
59                         return 2;
60                 if (type == "flalign")
61                         return 2;
62                 if (type == "alignat")
63                         return 2;
64                 if (type == "xalignat")
65                         return 2;
66                 if (type == "xxalignat")
67                         return 2;
68                 return 1;
69         }
70
71
72         // returns position of first relation operator in the array
73         // used for "intelligent splitting"
74         size_t firstRelOp(MathArray const & ar)
75         {
76                 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
77                         if ((*it)->isRelOp())
78                                 return it - ar.begin();
79                 return ar.size();
80         }
81
82
83         char const * star(bool numbered)
84         {
85                 return numbered ? "" : "*";
86         }
87
88
89         int typecode(string const & s)
90         {
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;
103                 return 0;
104         }
105
106         bool smaller(string const & s, string const & t)
107         {
108                 return typecode(s) < typecode(t);
109         }
110
111
112 } // end anon namespace
113
114
115
116 MathHullInset::MathHullInset()
117         : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
118 {
119         // This is needed as long the math parser is not re-entrant
120         initMath();
121         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
122         //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
123         //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
124         //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
125         setDefaults();
126 }
127
128
129 MathHullInset::MathHullInset(string const & type)
130         : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
131 {
132         setDefaults();
133 }
134
135
136 auto_ptr<InsetBase> MathHullInset::clone() const
137 {
138         return auto_ptr<InsetBase>(new MathHullInset(*this));
139 }
140
141
142 MathInset::mode_type MathHullInset::currentMode() const
143 {
144         if (type_ == "none")
145                 return UNDECIDED_MODE;
146         // definitely math mode ...
147         return MATH_MODE;
148 }
149
150
151 bool MathHullInset::idxFirst(LCursor & cur) const
152 {
153         cur.idx() = 0;
154         cur.pos() = 0;
155         return true;
156 }
157
158
159 bool MathHullInset::idxLast(LCursor & cur) const
160 {
161         cur.idx() = nargs() - 1;
162         cur.pos() = cur.lastpos();
163         return true;
164 }
165
166
167 char MathHullInset::defaultColAlign(col_type col)
168 {
169         if (type_ == "eqnarray")
170                 return "rcl"[col];
171         if (typecode(type_) >= typecode("align"))
172                 return "rl"[col & 1];
173         return 'c';
174 }
175
176
177 int MathHullInset::defaultColSpace(col_type col)
178 {
179         if (type_ == "align" || type_ == "alignat")
180                 return 0;
181         if (type_ == "xalignat")
182                 return (col & 1) ? 20 : 0;
183         if (type_ == "xxalignat" || type_ == "flalign")
184                 return (col & 1) ? 40 : 0;
185         return 0;
186 }
187
188
189 char const * MathHullInset::standardFont() const
190 {
191         if (type_ == "none")
192                 return "lyxnochange";
193         return "mathnormal";
194 }
195
196
197 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
198 {
199         FontSetChanger dummy1(mi.base, standardFont());
200         StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
201
202         // let the cells adjust themselves
203         MathGridInset::metrics(mi, dim);
204
205         if (display()) {
206                 dim.asc += 12;
207                 dim.des += 12;
208         }
209
210         if (numberedType()) {
211                 FontSetChanger dummy(mi.base, "mathbf");
212                 int l = 0;
213                 for (row_type row = 0; row < nrows(); ++row)
214                         l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
215
216                 if (l)
217                         dim.wid += 30 + l;
218         }
219
220         // make it at least as high as the current font
221         int asc = 0;
222         int des = 0;
223         math_font_max_dim(mi.base.font, asc, des);
224         dim.asc = max(dim.asc, asc);
225         dim.des = max(dim.des, des);
226
227         dim_ = dim;
228 }
229
230
231 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
232 {
233         FontSetChanger dummy1(pi.base, standardFont());
234         StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
235         MathGridInset::draw(pi, x + 1, y);
236
237         if (numberedType()) {
238                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
239                 for (row_type row = 0; row < nrows(); ++row) {
240                         int const yy = y + rowinfo_[row].offset_;
241                         FontSetChanger dummy(pi.base, "mathrm");
242                         drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
243                 }
244         }
245         setPosCache(pi, x, y);
246 }
247
248
249 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
250 {
251         if (display()) {
252                 MathGridInset::metricsT(mi, dim);
253         } else {
254                 ostringstream os;
255                 WriteStream wi(os, false, true);
256                 write(wi);
257                 dim.wid = os.str().size();
258                 dim.asc = 1;
259                 dim.des = 0;
260         }
261 }
262
263
264 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
265 {
266         if (display()) {
267                 MathGridInset::drawT(pain, x, y);
268         } else {
269                 ostringstream os;
270                 WriteStream wi(os, false, true);
271                 write(wi);
272                 pain.draw(x, y, os.str().c_str());
273         }
274 }
275
276
277 string MathHullInset::label(row_type row) const
278 {
279         row_type n = nrows();
280         BOOST_ASSERT(row < n);
281         return label_[row];
282 }
283
284
285 void MathHullInset::label(row_type row, string const & label)
286 {
287         //lyxerr << "setting label '" << label << "' for row " << row << endl;
288         label_[row] = label;
289 }
290
291
292 void MathHullInset::numbered(row_type row, bool num)
293 {
294         nonum_[row] = !num;
295 }
296
297
298 bool MathHullInset::numbered(row_type row) const
299 {
300         return !nonum_[row];
301 }
302
303
304 bool MathHullInset::ams() const
305 {
306         return
307                 type_ == "align" ||
308                 type_ == "flalign" ||
309                 type_ == "multline" ||
310                 type_ == "gather" ||
311                 type_ == "alignat" ||
312                 type_ == "xalignat" ||
313                 type_ == "xxalignat";
314 }
315
316
317 bool MathHullInset::display() const
318 {
319         return type_ != "simple" && type_ != "none";
320 }
321
322
323 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
324 {
325         for (row_type row = 0; row < nrows(); ++row)
326                 if (!label_[row].empty() && nonum_[row] != 1)
327                         labels.push_back(label_[row]);
328 }
329
330
331 bool MathHullInset::numberedType() const
332 {
333         if (type_ == "none")
334                 return false;
335         if (type_ == "simple")
336                 return false;
337         if (type_ == "xxalignat")
338                 return false;
339         for (row_type row = 0; row < nrows(); ++row)
340                 if (!nonum_[row])
341                         return true;
342         return false;
343 }
344
345
346 void MathHullInset::validate(LaTeXFeatures & features) const
347 {
348         if (ams())
349                 features.require("amsmath");
350
351
352         // Validation is necessary only if not using AMS math.
353         // To be safe, we will always run mathedvalidate.
354         //if (features.amsstyle)
355         //  return;
356
357         features.require("boldsymbol");
358         //features.binom      = true;
359
360         MathGridInset::validate(features);
361 }
362
363
364 void MathHullInset::header_write(WriteStream & os) const
365 {
366         bool n = numberedType();
367
368         if (type_ == "none")
369                 ;
370
371         else if (type_ == "simple") {
372                 os << '$';
373                 if (cell(0).empty())
374                         os << ' ';
375         }
376
377         else if (type_ == "equation") {
378                 if (n)
379                         os << "\\begin{equation" << star(n) << "}\n";
380                 else
381                         os << "\\[\n";
382         }
383
384         else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
385                  || type_ == "gather" || type_ == "multline")
386                         os << "\\begin{" << type_ << star(n) << "}\n";
387
388         else if (type_ == "alignat" || type_ == "xalignat")
389                 os << "\\begin{" << type_ << star(n) << '}'
390                   << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
391
392         else if (type_ == "xxalignat")
393                 os << "\\begin{" << type_ << '}'
394                   << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
395
396         else
397                 os << "\\begin{unknown" << star(n) << '}';
398 }
399
400
401 void MathHullInset::footer_write(WriteStream & os) const
402 {
403         bool n = numberedType();
404
405         if (type_ == "none")
406                 os << "\n";
407
408         else if (type_ == "simple")
409                 os << '$';
410
411         else if (type_ == "equation")
412                 if (n)
413                         os << "\\end{equation" << star(n) << "}\n";
414                 else
415                         os << "\\]\n";
416
417         else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
418                  || type_ == "alignat" || type_ == "xalignat"
419                  || type_ == "gather" || type_ == "multline")
420                 os << "\\end{" << type_ << star(n) << "}\n";
421
422         else if (type_ == "xxalignat")
423                 os << "\\end{" << type_ << "}\n";
424
425         else
426                 os << "\\end{unknown" << star(n) << '}';
427 }
428
429
430 bool MathHullInset::colChangeOK() const
431 {
432         return
433                 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
434                 type_ == "xalignat" || type_ == "xxalignat";
435 }
436
437
438 void MathHullInset::addRow(row_type row)
439 {
440         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
441         label_.insert(label_.begin() + row + 1, string());
442         MathGridInset::addRow(row);
443 }
444
445
446 void MathHullInset::swapRow(row_type row)
447 {
448         if (nrows() == 1)
449                 return;
450         if (row + 1 == nrows())
451                 --row;
452         swap(nonum_[row], nonum_[row + 1]);
453         swap(label_[row], label_[row + 1]);
454         MathGridInset::swapRow(row);
455 }
456
457
458 void MathHullInset::delRow(row_type row)
459 {
460         if (nrows() <= 1)
461                 return;
462         MathGridInset::delRow(row);
463         nonum_.erase(nonum_.begin() + row);
464         label_.erase(label_.begin() + row);
465 }
466
467
468 void MathHullInset::addCol(col_type col)
469 {
470         if (colChangeOK())
471                 MathGridInset::addCol(col);
472         else
473                 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
474 }
475
476
477 void MathHullInset::delCol(col_type col)
478 {
479         if (colChangeOK())
480                 MathGridInset::delCol(col);
481         else
482                 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
483 }
484
485
486 string MathHullInset::nicelabel(row_type row) const
487 {
488         if (nonum_[row])
489                 return string();
490         if (label_[row].empty())
491                 return string("(#)");
492         return '(' + label_[row] + ')';
493 }
494
495
496 void MathHullInset::glueall()
497 {
498         MathArray ar;
499         for (idx_type i = 0; i < nargs(); ++i)
500                 ar.append(cell(i));
501         *this = MathHullInset("simple");
502         cell(0) = ar;
503         setDefaults();
504 }
505
506
507 string const & MathHullInset::getType() const
508 {
509         return type_;
510 }
511
512
513 void MathHullInset::setType(string const & type)
514 {
515         type_ = type;
516         setDefaults();
517 }
518
519
520
521 void MathHullInset::mutate(string const & newtype)
522 {
523         lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
524
525         // we try to move along the chain
526         // none <-> simple <-> equation <-> eqnarray
527
528         if (newtype == "dump") {
529                 dump();
530         }
531
532         else if (newtype == type_) {
533                 // done
534         }
535
536         else if (type_ == "none") {
537                 setType("simple");
538                 numbered(0, false);
539                 mutate(newtype);
540         }
541
542         else if (type_ == "simple") {
543                 if (newtype == "none") {
544                         setType("none");
545                 } else {
546                         setType("equation");
547                         numbered(0, false);
548                         mutate(newtype);
549                 }
550         }
551
552         else if (type_ == "equation") {
553                 if (smaller(newtype, type_)) {
554                         setType("simple");
555                         mutate(newtype);
556                 } else if (newtype == "eqnarray") {
557                         MathGridInset::addCol(1);
558                         MathGridInset::addCol(1);
559
560                         // split it "nicely" on the firest relop
561                         pos_type pos = firstRelOp(cell(0));
562                         cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
563                         cell(0).erase(pos, cell(0).size());
564
565                         if (cell(1).size()) {
566                                 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
567                                 cell(1).erase(1, cell(1).size());
568                         }
569                         setType("eqnarray");
570                         mutate(newtype);
571                 } else if (newtype == "multline" || newtype == "gather") {
572                         setType(newtype);
573                         numbered(0, false);
574                 } else {
575                         MathGridInset::addCol(1);
576                         // split it "nicely"
577                         pos_type pos = firstRelOp(cell(0));
578                         cell(1) = cell(0);
579                         cell(0).erase(pos, cell(0).size());
580                         cell(1).erase(0, pos);
581                         setType("align");
582                         mutate(newtype);
583                 }
584         }
585
586         else if (type_ == "eqnarray") {
587                 if (smaller(newtype, type_)) {
588                         // set correct (no)numbering
589                         bool allnonum = true;
590                         for (row_type row = 0; row < nrows(); ++row)
591                                 if (!nonum_[row])
592                                         allnonum = false;
593
594                         // set first non-empty label
595                         string label;
596                         for (row_type row = 0; row < nrows(); ++row) {
597                                 if (!label_[row].empty()) {
598                                         label = label_[row];
599                                         break;
600                                 }
601                         }
602
603                         glueall();
604                         nonum_[0] = allnonum;
605                         label_[0] = label;
606                         mutate(newtype);
607                 } else { // align & Co.
608                         for (row_type row = 0; row < nrows(); ++row) {
609                                 idx_type c = 3 * row + 1;
610                                 cell(c).append(cell(c + 1));
611                         }
612                         MathGridInset::delCol(2);
613                         setType("align");
614                         mutate(newtype);
615                 }
616         }
617
618         else if (type_ == "align") {
619                 if (smaller(newtype, type_)) {
620                         MathGridInset::addCol(1);
621                         setType("eqnarray");
622                         mutate(newtype);
623                 } else {
624                         setType(newtype);
625                 }
626         }
627
628         else if (type_ == "multline") {
629                 if (newtype == "gather" || newtype == "align" ||
630                     newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
631                         setType(newtype);
632                 else if (newtype == "eqnarray") {
633                         MathGridInset::addCol(1);
634                         MathGridInset::addCol(1);
635                         setType("eqnarray");
636                 } else {
637                         lyxerr << "mutation from '" << type_
638                                 << "' to '" << newtype << "' not implemented" << endl;
639                 }
640         }
641
642         else if (type_ == "gather") {
643                 if (newtype == "multline") {
644                         setType("multline");
645                 } else {
646                         lyxerr << "mutation from '" << type_
647                                 << "' to '" << newtype << "' not implemented" << endl;
648                 }
649         }
650
651         else {
652                 lyxerr << "mutation from '" << type_
653                                          << "' to '" << newtype << "' not implemented" << endl;
654         }
655 }
656
657
658 string MathHullInset::eolString(row_type row, bool fragile) const
659 {
660         string res;
661         if (numberedType()) {
662                 if (!label_[row].empty() && !nonum_[row])
663                         res += "\\label{" + label_[row] + '}';
664                 if (nonum_[row] && (type_ != "multline"))
665                         res += "\\nonumber ";
666         }
667         return res + MathGridInset::eolString(row, fragile);
668 }
669
670
671 void MathHullInset::write(WriteStream & os) const
672 {
673         header_write(os);
674         MathGridInset::write(os);
675         footer_write(os);
676 }
677
678
679 void MathHullInset::normalize(NormalStream & os) const
680 {
681         os << "[formula " << type_ << ' ';
682         MathGridInset::normalize(os);
683         os << "] ";
684 }
685
686
687 void MathHullInset::mathmlize(MathMLStream & os) const
688 {
689         MathGridInset::mathmlize(os);
690 }
691
692
693 void MathHullInset::infoize(ostream & os) const
694 {
695         os << "Type: " << type_;
696 }
697
698
699 void MathHullInset::check() const
700 {
701         BOOST_ASSERT(nonum_.size() == nrows());
702         BOOST_ASSERT(label_.size() == nrows());
703 }
704
705
706 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
707 {
708         string lang;
709         string extra;
710         istringstream iss(func.argument.c_str());
711         iss >> lang >> extra;
712         if (extra.empty())
713                 extra = "noextra";
714
715 #ifdef WITH_WARNINGS
716 #warning temporarily disabled
717         //if (cur.selection()) {
718         //      MathArray ar;
719         //      selGet(cur.ar);
720         //      lyxerr << "use selection: " << ar << endl;
721         //      insert(pipeThroughExtern(lang, extra, ar));
722         //      return;
723         //}
724 #endif
725
726         MathArray eq;
727         eq.push_back(MathAtom(new MathCharInset('=')));
728
729         // go to first item in line
730         cur.idx() -= cur.idx() % ncols();
731         cur.pos() = 0;
732
733         if (getType() == "simple") {
734                 size_type pos = cur.cell().find_last(eq);
735                 MathArray ar;
736                 if (cur.inMathed() && cur.selection()) {
737                         asArray(cur.grabAndEraseSelection(), ar);
738                 } else if (pos == cur.cell().size()) {
739                         ar = cur.cell();
740                         lyxerr << "use whole cell: " << ar << endl;
741                 } else {
742                         ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
743                         lyxerr << "use partial cell form pos: " << pos << endl;
744                 }
745                 cur.cell().append(eq);
746                 cur.cell().append(pipeThroughExtern(lang, extra, ar));
747                 cur.pos() = cur.lastpos();
748                 return;
749         }
750
751         if (getType() == "equation") {
752                 lyxerr << "use equation inset" << endl;
753                 mutate("eqnarray");
754                 MathArray & ar = cur.cell();
755                 lyxerr << "use cell: " << ar << endl;
756                 ++cur.idx();
757                 cur.cell() = eq;
758                 ++cur.idx();
759                 cur.cell() = pipeThroughExtern(lang, extra, ar);
760                 // move to end of line
761                 cur.pos() = cur.lastpos();
762                 return;
763         }
764
765         {
766                 lyxerr << "use eqnarray" << endl;
767                 cur.idx() += 2 - cur.idx() % ncols();
768                 cur.pos() = 0;
769                 MathArray ar = cur.cell();
770                 lyxerr << "use cell: " << ar << endl;
771 #ifdef WITH_WARNINGS
772 #warning temporarily disabled
773 #endif
774                 addRow(cur.row());
775                 ++cur.idx();
776                 ++cur.idx();
777                 cur.cell() = eq;
778                 ++cur.idx();
779                 cur.cell() = pipeThroughExtern(lang, extra, ar);
780                 cur.pos() = cur.lastpos();
781         }
782 }
783
784
785 void MathHullInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
786 {
787         switch (cmd.action) {
788
789         case LFUN_BREAKPARAGRAPH:
790                 // just swallow this
791                 break;
792
793         case LFUN_BREAKLINE:
794                 if (type_ == "simple" || type_ == "equation") {
795                         mutate("eqnarray");
796                         cur.idx() = 1;
797                         cur.pos() = 0;
798                         //cur.dispatched(FINISHED);
799                         break;
800                 }
801                 MathGridInset::priv_dispatch(cur, cmd);
802                 break;
803
804         case LFUN_MATH_NUMBER:
805                 //lyxerr << "toggling all numbers" << endl;
806                 if (display()) {
807                         recordUndo(cur);
808                         bool old = numberedType();
809                         if (type_ == "multline")
810                                 numbered(nrows() - 1, !old);
811                         else
812                                 for (row_type row = 0; row < nrows(); ++row)
813                                         numbered(row, !old);
814                         cur.message(old ? _("No number") : _("Number"));
815                 }
816                 break;
817
818         case LFUN_MATH_NONUMBER:
819                 if (display()) {
820                         row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
821                         recordUndo(cur);
822                         bool old = numbered(r);
823                         cur.message(old ? _("No number") : _("Number"));
824                         numbered(r, !old);
825                 }
826                 break;
827
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;
832
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);
839                         new_label = lyx::support::trim(res.second);
840                 }
841
842                 if (!new_label.empty())
843                         numbered(r, true);
844                 label(r, new_label);
845                 break;
846         }
847
848         case LFUN_MATH_EXTERN:
849                 doExtern(cur, cmd);
850                 //cur.dispatched(FINISHED);
851                 break;
852
853         case LFUN_MATH_MUTATE: {
854                 lyxerr << "Hull: MUTATE: " << cmd.argument << endl;
855                 row_type row = cur.row();
856                 col_type col = cur.col();
857                 mutate(cmd.argument);
858                 cur.idx() = row * ncols() + col;
859                 if (cur.idx() > cur.lastidx()) {
860                         cur.idx() = cur.lastidx();
861                         cur.pos() = cur.lastpos();
862                 }
863                 if (cur.pos() > cur.lastpos())
864                         cur.pos() = cur.lastpos();
865                 //cur.dispatched(FINISHED);
866                 break;
867         }
868
869         case LFUN_MATH_DISPLAY: {
870                 mutate(type_ == "simple" ? "equation" : "simple");
871                 cur.idx() = 0;
872                 cur.pos() = cur.lastpos();
873                 //cur.dispatched(FINISHED);
874                 break;
875         }
876
877         default:
878                 MathGridInset::priv_dispatch(cur, cmd);
879                 break;
880         }
881 }
882
883
884 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
885                 FuncStatus & flag) const
886 {
887         switch (cmd.action) {
888         case LFUN_BREAKLINE:
889         case LFUN_MATH_NUMBER:
890         case LFUN_MATH_NONUMBER:
891         case LFUN_INSERT_LABEL:
892         case LFUN_MATH_EXTERN:
893         case LFUN_MATH_MUTATE:
894         case LFUN_MATH_DISPLAY:
895                 // we handle these
896                 return true;
897         default:
898                 return MathGridInset::getStatus(cur, cmd, flag);
899         }
900 }
901
902
903 /////////////////////////////////////////////////////////////////////
904
905 #include "formulamacro.h"
906 #include "math_arrayinset.h"
907 #include "math_deliminset.h"
908 #include "math_factory.h"
909 #include "math_parser.h"
910 #include "math_spaceinset.h"
911 #include "ref_inset.h"
912
913 #include "bufferview_funcs.h"
914 #include "lyxtext.h"
915 #include "undo.h"
916
917 #include "frontends/LyXView.h"
918 #include "frontends/Dialogs.h"
919
920 #include "support/std_sstream.h"
921 #include "support/lstrings.h"
922 #include "support/lyxlib.h"
923
924
925 // simply scrap this function if you want
926 void MathHullInset::mutateToText()
927 {
928 #if 0
929         // translate to latex
930         ostringstream os;
931         latex(NULL, os, false, false);
932         string str = os.str();
933
934         // insert this text
935         LyXText * lt = view_->getLyXText();
936         string::const_iterator cit = str.begin();
937         string::const_iterator end = str.end();
938         for (; cit != end; ++cit)
939                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
940
941         // remove ourselves
942         //view_->owner()->dispatch(LFUN_ESCAPE);
943 #endif
944 }
945
946
947 void MathHullInset::handleFont(LCursor & cur, string const & arg,
948         string const & font)
949 {
950         // this whole function is a hack and won't work for incremental font
951         // changes...
952         recordUndo(cur);
953         if (cur.inset().asMathInset()->name() == font)
954                 cur.handleFont(font);
955         else {
956                 cur.handleNest(createMathInset(font));
957                 cur.insert(arg);
958         }
959 }
960
961
962 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
963 {
964         recordUndo(cur);
965         LyXFont font;
966         bool b;
967         bv_funcs::string2font(arg, font, b);
968         if (font.color() != LColor::inherit) {
969                 MathAtom at = createMathInset("color");
970                 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
971                 cur.handleNest(at, 1);
972         }
973 }
974
975
976 void MathHullInset::edit(LCursor & cur, bool left)
977 {
978         lyxerr << "MathHullInset: edit left/right" << endl;
979         cur.push(*this);
980 }
981
982
983 string const MathHullInset::editMessage() const
984 {
985         return _("Math editor mode");
986 }
987
988
989 void MathHullInset::getCursorDim(int & asc, int & desc) const
990 {
991         asc = 10;
992         desc = 2;
993         //math_font_max_dim(font_, asc, des);
994 }
995
996
997 void MathHullInset::revealCodes(LCursor & cur) const
998 {
999         if (!cur.inMathed())
1000                 return;
1001         ostringstream os;
1002         cur.info(os);
1003         cur.message(os.str());
1004 /*
1005         // write something to the minibuffer
1006         // translate to latex
1007         cur.markInsert(bv);
1008         ostringstream os;
1009         write(NULL, os);
1010         string str = os.str();
1011         cur.markErase(bv);
1012         string::size_type pos = 0;
1013         string res;
1014         for (string::iterator it = str.begin(); it != str.end(); ++it) {
1015                 if (*it == '\n')
1016                         res += ' ';
1017                 else if (*it == '\0') {
1018                         res += "  -X-  ";
1019                         pos = it - str.begin();
1020                 }
1021                 else
1022                         res += *it;
1023         }
1024         if (pos > 30)
1025                 res = res.substr(pos - 30);
1026         if (res.size() > 60)
1027                 res = res.substr(0, 60);
1028         cur.message(res);
1029 */
1030 }
1031
1032
1033 InsetBase::Code MathHullInset::lyxCode() const
1034 {
1035         return MATH_CODE;
1036 }
1037
1038
1039 /////////////////////////////////////////////////////////////////////
1040
1041
1042 #if 1
1043 bool MathHullInset::searchForward(BufferView *, string const &, bool, bool)
1044 {
1045         return false;
1046 }
1047
1048 #else
1049 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1050                                      bool, bool)
1051 {
1052         return false;
1053 #ifdef WITH_WARNINGS
1054 #warning pretty ugly
1055 #endif
1056         static MathHullInset * lastformula = 0;
1057         static CursorBase current = DocIterator(ibegin(nucleus()));
1058         static MathArray ar;
1059         static string laststr;
1060
1061         if (lastformula != this || laststr != str) {
1062                 //lyxerr << "reset lastformula to " << this << endl;
1063                 lastformula = this;
1064                 laststr = str;
1065                 current = ibegin(nucleus());
1066                 ar.clear();
1067                 mathed_parse_cell(ar, str);
1068         } else {
1069                 increment(current);
1070         }
1071         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1072
1073         for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1074                 CursorSlice & top = it.back();
1075                 MathArray const & a = top.asMathInset()->cell(top.idx_);
1076                 if (a.matchpart(ar, top.pos_)) {
1077                         bv->cursor().setSelection(it, ar.size());
1078                         current = it;
1079                         top.pos_ += ar.size();
1080                         bv->update();
1081                         return true;
1082                 }
1083         }
1084
1085         //lyxerr << "not found!" << endl;
1086         lastformula = 0;
1087         return false;
1088 }
1089 #endif
1090
1091
1092 bool MathHullInset::searchBackward(BufferView * bv, string const & what,
1093                                       bool a, bool b)
1094 {
1095         lyxerr[Debug::MATHED]
1096                 << "searching backward not implemented in mathed" << endl;
1097         return searchForward(bv, what, a, b);
1098 }
1099
1100
1101 void MathHullInset::write(Buffer const &, std::ostream & os) const
1102 {
1103         WriteStream wi(os, false, false);
1104         os << "Formula ";
1105         write(wi);
1106 }
1107
1108
1109 void MathHullInset::read(Buffer const &, LyXLex & lex)
1110 {
1111         MathAtom at;
1112         mathed_parse_normal(at, lex);
1113         operator=(*at->asHullInset());
1114 }
1115
1116
1117 int MathHullInset::plaintext(Buffer const &, ostream & os,
1118                         OutputParams const &) const
1119 {
1120         if (0 && display()) {
1121                 Dimension dim;
1122                 TextMetricsInfo mi;
1123                 metricsT(mi, dim);
1124                 TextPainter tpain(dim.width(), dim.height());
1125                 drawT(tpain, 0, dim.ascent());
1126                 tpain.show(os, 3);
1127                 // reset metrics cache to "real" values
1128                 //metrics();
1129                 return tpain.textheight();
1130         } else {
1131                 WriteStream wi(os, false, true);
1132                 wi << ' ' << cell(0) << ' ';
1133                 return wi.line();
1134         }
1135 }
1136
1137
1138 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1139                            OutputParams const & runparams) const
1140 {
1141         return docbook(buf, os, runparams);
1142 }
1143
1144
1145 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1146                           OutputParams const & runparams) const
1147 {
1148         MathMLStream ms(os);
1149         ms << MTag("equation");
1150         ms <<   MTag("alt");
1151         ms <<    "<[CDATA[";
1152         int res = plaintext(buf, ms.os(), runparams);
1153         ms <<    "]]>";
1154         ms <<   ETag("alt");
1155         ms <<   MTag("math");
1156         MathGridInset::mathmlize(ms);
1157         ms <<   ETag("math");
1158         ms << ETag("equation");
1159         return ms.line() + res;
1160 }