]> git.lyx.org Git - features.git/blob - src/mathed/formulabase.C
c66eb525109cf797fa34bbd047108a04411739fa
[features.git] / src / mathed / formulabase.C
1 /*
2 *  File:        formula.C
3 *  Purpose:     Implementation of formula inset
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *  Description: Allows the edition of math paragraphs inside Lyx.
7 *
8 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
9 *
10 *  Version: 0.4, Lyx project.
11 *
12 *   You are free to use and modify this code under the terms of
13 *   the GNU General Public Licence version 2 or later.
14 */
15
16 #include <config.h>
17 #include <fstream>
18
19 #include "Lsstream.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "formula.h"
26 #include "commandtags.h"
27 #include "math_cursor.h"
28 #include "math_parser.h"
29 #include "BufferView.h"
30 #include "lyxtext.h"
31 #include "gettext.h"
32 #include "LaTeXFeatures.h"
33 #include "debug.h"
34 #include "support/LOstream.h"
35 #include "LyXView.h"
36 #include "Painter.h"
37 #include "font.h"
38 #include "math_arrayinset.h"
39 #include "math_spaceinset.h"
40 #include "math_deliminset.h"
41 #include "support/lyxlib.h"
42 #include "mathed/support.h"
43
44 using namespace std;
45
46 extern char const * latex_special_chars;
47
48 int greek_kb_flag = 0;
49 extern char const * latex_mathenv[];
50 LyXFont           * Math_Fonts = 0;
51 MathCursor        * mathcursor = 0;
52
53
54 namespace {
55
56
57 // local global
58 int sel_x;
59 int sel_y;
60 bool sel_flag;
61
62 void mathed_init_fonts();
63
64 string nicelabel(string const & label)
65 {
66         return label.empty() ? string("(#)") : "(" + label + ")";
67 }
68
69 } // namespaces
70
71
72
73 LyXFont WhichFont(short type, int size)
74 {
75         LyXFont f;
76         
77         if (!Math_Fonts)
78                 mathed_init_fonts();
79
80         switch (type) {
81         case LM_TC_SYMB:        
82                 f = Math_Fonts[2];
83                 break;
84
85         case LM_TC_BSYM:        
86                 f = Math_Fonts[2];
87                 break;
88
89         case LM_TC_VAR:
90         case LM_TC_IT:
91                 f = Math_Fonts[0];
92                 break;
93
94         case LM_TC_BF:
95                 f = Math_Fonts[3];
96                 break;
97
98         case LM_TC_SF:
99                 f = Math_Fonts[7];
100                 break;
101
102         case LM_TC_CAL:
103                 f = Math_Fonts[4];
104                 break;
105
106         case LM_TC_TT:
107                 f = Math_Fonts[5];
108                 break;
109
110         case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
111         case LM_TC_TEXTRM:
112         case LM_TC_RM:
113                 f = Math_Fonts[6];
114                 break;
115
116         default:
117                 f = Math_Fonts[1];
118                 break;
119         }
120
121         switch (size) {
122         case LM_ST_DISPLAY:
123                 if (type == LM_TC_BSYM) {
124                         f.incSize();
125                         f.incSize();
126                 }
127                 break;
128
129         case LM_ST_TEXT:
130                 break;
131
132         case LM_ST_SCRIPT:
133                 f.decSize();
134                 break;
135
136         case LM_ST_SCRIPTSCRIPT:
137                 f.decSize();
138                 f.decSize();
139                 break;
140
141         default:
142                 lyxerr << "Math Error: wrong font size: " << size << endl;
143                 break;
144         }
145
146         if (type != LM_TC_TEXTRM)
147                 f.setColor(LColor::math);
148
149         return f;
150 }
151
152
153 namespace {
154
155 void mathed_init_fonts()
156 {
157         Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
158         //at once (JMarc) rc
159
160         for (int i = 0 ; i < 8 ; ++i) {
161                 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
162         }
163
164         Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
165
166         Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
167
168         Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
169         Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
170
171         Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
172
173         Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
174         Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
175
176         Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
177
178         Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
179
180         Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
181 }
182
183
184 // returns the nearest enclosing matrix
185 MathArrayInset * matrixpar(int & idx)
186 {
187         idx = 0;
188         return
189                 static_cast<MathArrayInset *> 
190                         (mathcursor ? mathcursor->enclosing(LM_OT_MATRIX, idx) : 0); 
191 }
192
193
194 } // namespace anon
195
196
197 InsetFormulaBase::InsetFormulaBase(MathInset * par)
198         : par_(par)
199 {}
200
201 InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
202         : UpdatableInset(f), par_(static_cast<MathInset *>(f.par_->Clone()))
203 {}
204
205 InsetFormulaBase::~InsetFormulaBase()
206 {
207 #ifdef WITH_WARNINGS
208 #warning leak this for a while...
209 #endif
210         //delete par_;
211 }
212
213
214 void InsetFormulaBase::Write(Buffer const * buf, ostream & os) const
215 {
216         os << "Formula ";
217         Latex(buf, os, false, false);
218 }
219
220
221 int InsetFormulaBase::Ascii(Buffer const *, ostream & os, int) const
222 {
223         par_->Write(os, false);
224         return 0;
225 }
226
227
228 int InsetFormulaBase::Linuxdoc(Buffer const * buf, ostream & os) const
229 {
230         return Ascii(buf, os, 0);
231 }
232
233
234 int InsetFormulaBase::DocBook(Buffer const * buf, ostream & os) const
235 {
236         return Ascii(buf, os, 0);
237 }
238
239
240 // Check if uses AMS macros
241 void InsetFormulaBase::Validate(LaTeXFeatures &) const
242 {}
243
244
245 string const InsetFormulaBase::EditMessage() const
246 {
247         return _("Math editor mode");
248 }
249
250
251 void InsetFormulaBase::Edit(BufferView * bv, int x, int /*y*/, unsigned int)
252 {
253         mathcursor = new MathCursor(this);
254
255         if (!bv->lockInset(this))
256                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
257
258         par_->Metrics(LM_ST_TEXT);
259         bv->updateInset(this, false);
260         if (x == 0) {
261                 mathcursor->first();
262         } else {
263                 mathcursor->last();
264         }
265         sel_x = 0;
266         sel_y = 0;
267         sel_flag = false;
268 }
269
270
271 void InsetFormulaBase::InsetUnlock(BufferView * bv)
272 {
273         if (mathcursor) {
274                 if (mathcursor->InMacroMode()) {
275                         mathcursor->MacroModeClose();
276                         UpdateLocal(bv);
277                 }
278                 delete mathcursor;
279         }
280         mathcursor = 0;
281         bv->updateInset(this, false);
282 }
283
284
285
286
287 void InsetFormulaBase::GetCursorPos(BufferView *, int & x, int & y) const
288 {
289         mathcursor->GetPos(x, y);
290         x -= par_->xo();
291         y -= par_->yo();
292 }
293
294
295 void InsetFormulaBase::ToggleInsetCursor(BufferView * bv)
296 {
297         if (!mathcursor)
298                 return;
299
300         if (isCursorVisible())
301                 bv->hideLockedInsetCursor();
302         else {
303                 int x;
304                 int y;
305                 mathcursor->GetPos(x, y);
306                 //x -= par_->xo();
307                 y -= par_->yo();
308
309                 LyXFont   font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
310                 int const asc  = lyxfont::maxAscent(font);
311                 int const desc = lyxfont::maxDescent(font);
312
313                 bv->showLockedInsetCursor(x, y, asc, desc);
314         }
315
316         toggleCursorVisible();
317 }
318
319
320 void InsetFormulaBase::ShowInsetCursor(BufferView * bv, bool)
321 {
322         if (!isCursorVisible()) {
323                 if (mathcursor) {
324                         int x;
325                         int y;
326                         mathcursor->GetPos(x, y);
327                         x -= par_->xo();
328                         y -= par_->yo();
329                         LyXFont font   = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
330                         int const asc  = lyxfont::maxAscent(font);
331                         int const desc = lyxfont::maxDescent(font);
332                         bv->fitLockedInsetCursor(x, y, asc, desc);
333                 }
334                 ToggleInsetCursor(bv);
335         }
336 }
337
338
339 void InsetFormulaBase::HideInsetCursor(BufferView * bv)
340 {
341         if (isCursorVisible())
342                 ToggleInsetCursor(bv);
343 }
344
345
346 void InsetFormulaBase::ToggleInsetSelection(BufferView * bv)
347 {
348         if (!mathcursor)
349                 return;
350
351         bv->updateInset(this, false);
352 }
353
354
355
356 vector<string> const InsetFormulaBase::getLabelList() const
357 {
358   return std::vector<string>();
359 }
360
361
362 void InsetFormulaBase::UpdateLocal(BufferView * bv)
363 {
364         par_->Metrics(LM_ST_TEXT);
365         bv->updateInset(this, true);
366 }
367
368
369 void InsetFormulaBase::InsetButtonRelease(BufferView * bv,
370                                       int x, int y, int /*button*/)
371 {
372         if (mathcursor) {
373                 HideInsetCursor(bv);
374                 x += par_->xo();
375                 y += par_->yo();
376                 mathcursor->SetPos(x, y);
377                 ShowInsetCursor(bv);
378                 if (sel_flag) {
379                         sel_flag = false;
380                         sel_x = 0;
381                         sel_y = 0;
382                         bv->updateInset(this, false);
383                 }
384         }
385 }
386
387
388 void InsetFormulaBase::InsetButtonPress(BufferView * bv,
389                 int x, int y, int /*button*/)
390 {
391         sel_flag = false;
392         sel_x = x;
393         sel_y = y;
394         if (mathcursor && mathcursor->Selection()) {
395                 mathcursor->SelClear();
396                 bv->updateInset(this, false);
397         }
398 }
399
400
401 void InsetFormulaBase::InsetMotionNotify(BufferView * bv,
402                 int x, int y, int /*button*/)
403 {
404         if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
405                 sel_flag = true;
406                 HideInsetCursor(bv);
407                 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
408                 mathcursor->SelStart();
409                 ShowInsetCursor(bv);
410                 mathcursor->GetPos(sel_x, sel_y);
411         } else if (sel_flag) {
412                 HideInsetCursor(bv);
413                 x += par_->xo();
414                 y += par_->yo();
415                 mathcursor->SetPos(x, y);
416                 ShowInsetCursor(bv);
417                 mathcursor->GetPos(x, y);
418                 if (sel_x != x || sel_y != y)
419                         bv->updateInset(this, false);
420                 sel_x = x;
421                 sel_y = y;
422         }
423 }
424
425
426 void InsetFormulaBase::InsetKeyPress(XKeyEvent *)
427 {
428         lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
429 }
430
431
432 UpdatableInset::RESULT
433 InsetFormulaBase::LocalDispatch(BufferView * bv, kb_action action,
434                             string const & arg)
435 {
436         //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
437         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
438         //   extern char *dispatch_result;
439
440         if (!mathcursor) 
441                 return UNDISPATCHED;
442
443         MathTextCodes varcode = LM_TC_MIN;
444         bool was_macro = mathcursor->InMacroMode();
445         bool sel = false;
446         bool space_on = false;
447         bool was_selection = mathcursor->Selection();
448         RESULT result = DISPATCHED;
449         static MathSpaceInset * sp = 0;
450
451         HideInsetCursor(bv);
452
453         if (mathcursor->getLastCode() == LM_TC_TEX)
454                 varcode = LM_TC_TEX;
455
456         mathcursor->normalize();
457
458         switch (action) {
459
460                 // --- Cursor Movements ---------------------------------------------
461
462         case LFUN_RIGHTSEL:
463                 sel = true; // fall through...
464
465         case LFUN_RIGHT:
466                 result = DISPATCH_RESULT(mathcursor->Right(sel));
467                 UpdateLocal(bv);
468                 break;
469
470
471         case LFUN_LEFTSEL:
472                 sel = true; // fall through
473
474         case LFUN_LEFT:
475                 result = DISPATCH_RESULT(mathcursor->Left(sel));
476                 UpdateLocal(bv);
477                 break;
478
479
480         case LFUN_UPSEL:
481                 sel = true;
482
483         case LFUN_UP:
484                 result = DISPATCH_RESULT(mathcursor->Up(sel));
485                 UpdateLocal(bv);
486                 break;
487
488
489         case LFUN_DOWNSEL:
490                 sel = true;
491
492         case LFUN_DOWN:
493                 result = DISPATCH_RESULT(mathcursor->Down(sel));
494                 UpdateLocal(bv);
495                 break;
496
497
498         case LFUN_HOME:
499                 mathcursor->Home();
500                 result = DISPATCHED_NOUPDATE;
501                 break;
502
503         case LFUN_END:
504                 mathcursor->End();
505                 result = DISPATCHED_NOUPDATE;
506                 break;
507
508         case LFUN_DELETE_LINE_FORWARD:
509                 bv->lockedInsetStoreUndo(Undo::DELETE);
510                 mathcursor->DelLine();
511                 UpdateLocal(bv);
512                 break;
513
514         case LFUN_TAB:
515                 bv->lockedInsetStoreUndo(Undo::INSERT);
516                 mathcursor->idxRight();
517                 UpdateLocal(bv);
518                 break;
519
520         case LFUN_TABINSERT:
521                 bv->lockedInsetStoreUndo(Undo::INSERT);
522                 mathcursor->idxRight();
523                 UpdateLocal(bv);
524                 break;
525
526         case LFUN_BACKSPACE:
527                 if (!mathcursor->InMacroMode() && mathcursor->pos() == 0) {
528                         bv->lockedInsetStoreUndo(Undo::DELETE);
529                         mathcursor->pullArg();
530                         bv->updateInset(this, true);
531                         break;
532                 }
533                 if (!mathcursor->Left())
534                         break;
535
536                 // fall through...
537
538         case LFUN_DELETE:
539                 bv->lockedInsetStoreUndo(Undo::DELETE);
540                 mathcursor->Delete();
541                 bv->updateInset(this, true);
542                 break;
543
544                 //    case LFUN_GETXY:
545                 //      sprintf(dispatch_buffer, "%d %d",);
546                 //      dispatch_result = dispatch_buffer;
547                 //      break;
548         case LFUN_SETXY:
549         {
550                 lyxerr << "LFUN_SETXY broken!\n";
551                 int x, y, x1, y1;
552                 istringstream is(arg.c_str());
553                 is >> x >> y;
554                 lyxerr << "LFUN_SETXY: x: " << x << " y: " << y << "\n";
555                 par_->GetXY(x1, y1);
556                 mathcursor->SetPos(x1 + x, y1 + y);
557         }
558         break;
559
560                 // cursor selection ---------------------------- 
561
562         case LFUN_PASTE:
563                 if (was_macro)
564                         mathcursor->MacroModeClose();
565                 bv->lockedInsetStoreUndo(Undo::INSERT);
566                 mathcursor->SelPaste();
567                 UpdateLocal(bv);
568                 break;
569
570         case LFUN_CUT:
571                 bv->lockedInsetStoreUndo(Undo::DELETE);
572                 mathcursor->SelCut();
573                 UpdateLocal(bv);
574                 break;
575
576         case LFUN_COPY:
577                 mathcursor->SelCopy();
578                 break;
579
580         case LFUN_HOMESEL:
581         case LFUN_ENDSEL:
582         case LFUN_WORDRIGHTSEL:
583         case LFUN_WORDLEFTSEL:
584                 break;
585
586                 // --- accented characters ------------------------------
587
588         case LFUN_UMLAUT:     mathcursor->setAccent(LM_ddot); break;
589         case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
590         case LFUN_GRAVE:      mathcursor->setAccent(LM_grave); break;
591         case LFUN_ACUTE:      mathcursor->setAccent(LM_acute); break;
592         case LFUN_TILDE:      mathcursor->setAccent(LM_tilde); break;
593         case LFUN_MACRON:     mathcursor->setAccent(LM_bar); break;
594         case LFUN_DOT:        mathcursor->setAccent(LM_dot); break;
595         case LFUN_CARON:      mathcursor->setAccent(LM_check); break;
596         case LFUN_BREVE:      mathcursor->setAccent(LM_breve); break;
597         case LFUN_VECTOR:     mathcursor->setAccent(LM_vec); break;
598
599                 // Greek mode
600         case LFUN_GREEK:
601                 if (!greek_kb_flag) {
602                         greek_kb_flag = 1;
603                         bv->owner()->message(_("Math greek mode on"));
604                 } else
605                         greek_kb_flag = 0;
606                 break;
607
608                 // Greek keyboard
609         case LFUN_GREEK_TOGGLE:
610                 greek_kb_flag = greek_kb_flag ? 0 : 2;
611                 if (greek_kb_flag)
612                         bv->owner()->message(_("Math greek keyboard on"));
613                 else
614                         bv->owner()->message(_("Math greek keyboard off"));
615                 break;
616
617                 //  Math fonts
618         case LFUN_BOLD:  mathcursor->toggleLastCode(LM_TC_BF); break;
619         case LFUN_SANS:  mathcursor->toggleLastCode(LM_TC_SF); break;
620         case LFUN_EMPH:  mathcursor->toggleLastCode(LM_TC_CAL); break;
621         case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
622         case LFUN_CODE:  mathcursor->toggleLastCode(LM_TC_TT); break;
623         case LFUN_DEFAULT:  mathcursor->setLastCode(LM_TC_VAR); break;
624
625         case LFUN_TEX:
626                 // varcode = LM_TC_TEX;
627                 mathcursor->setLastCode(LM_TC_TEX);
628                 bv->owner()->message(_("TeX mode"));
629                 break;
630
631         case LFUN_MATH_LIMITS:
632                 bv->lockedInsetStoreUndo(Undo::INSERT);
633                 if (mathcursor->toggleLimits())
634                         UpdateLocal(bv);
635                 break;
636
637         case LFUN_MATH_SIZE:
638                 if (!arg.empty()) {
639                         bv->lockedInsetStoreUndo(Undo::INSERT);
640                         latexkeys const * l = in_word_set(arg);
641                         mathcursor->SetSize(MathStyles(l ? l->id : -1));
642                         UpdateLocal(bv);
643                 }
644                 break;
645
646         case LFUN_INSERT_MATH:
647                 if (!arg.empty()) {
648                         bv->lockedInsetStoreUndo(Undo::INSERT);
649                         mathcursor->Interpret(arg);
650                         UpdateLocal(bv);
651                 }
652                 break;
653
654
655         case LFUN_INSERT_MATRIX:
656                 if (mathcursor) {
657                         bv->lockedInsetStoreUndo(Undo::INSERT);
658                         int m = 1;
659                         int n = 1;
660                         string v_align, h_align;
661                         istringstream is(arg.c_str());
662                         is >> m >> n >> v_align >> h_align;
663                         MathArrayInset * p = new MathArrayInset(m, n);
664                         p->valign(v_align[0]);
665                         p->halign(h_align);
666                         mathcursor->insert(p);
667                         UpdateLocal(bv);
668                 }
669                 break;
670
671         case LFUN_MATH_DELIM:
672         {
673                 bv->lockedInsetStoreUndo(Undo::INSERT);
674                 int ilt = '(';
675                 int irt = '.';
676                 static const string vdelim("(){}[]./|");
677                 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
678
679                 if (arg.empty())
680                         break;
681
682                 istringstream is(arg.c_str());
683                 string lt, rt;
684                 is >> lt >> rt;
685                 lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
686                 lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
687
688                 if (lt.size() > 1) {
689                         latexkeys const * l = in_word_set(lt);
690                         if (l)
691                                 ilt = l->id;
692                 } else if (vdelim.find(lt[0]) != string::npos)
693                                 ilt = lt[0];
694
695                 if (rt.size() > 1) {
696                         latexkeys const * l = in_word_set(rt);
697                         if (l)
698                                 irt = l->id;
699                 } else if (vdelim.find(rt[0]) != string::npos)
700                                 irt = rt[0];
701
702                 if (mathcursor->selection) {
703                         MathDelimInset * p = new MathDelimInset(ilt, irt);
704                         MathArray ar;
705                         mathcursor->selArray(ar);
706                         lyxerr << "selarray: " << ar << "\n";
707                         p->cell(0) = ar; 
708                         mathcursor->insert(p);
709                 } else {
710                         mathcursor->insert(new MathDelimInset(ilt, irt));
711                 }
712                 UpdateLocal(bv);
713                 break;
714         }
715
716         case LFUN_PROTECTEDSPACE:
717                 bv->lockedInsetStoreUndo(Undo::INSERT);
718                 mathcursor->insert(new MathSpaceInset(1));
719                 space_on = true;
720                 UpdateLocal(bv);
721                 break;
722
723                 // Invalid actions under math mode
724         case LFUN_MATH_MODE:
725                 if (mathcursor->getLastCode() != LM_TC_TEXTRM) {
726                         bv->owner()->message(_("math text mode"));
727                         varcode = LM_TC_TEXTRM;
728                 } else 
729                         varcode = LM_TC_VAR;
730                 mathcursor->setLastCode(varcode);
731                 break;
732
733         case LFUN_UNDO:
734                 bv->owner()->message(_("Invalid action in math mode!"));
735                 break;
736
737
738         case LFUN_MATH_HALIGN:
739         {
740                 bv->lockedInsetStoreUndo(Undo::INSERT);
741                 lyxerr << "handling halign '" << arg << "'\n";
742                 int idx;
743                 MathArrayInset * p = matrixpar(idx);
744                 if (!p)
745                         break; 
746                 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
747                 UpdateLocal(bv);
748                 break;
749         }
750
751         case LFUN_MATH_VALIGN:
752         {
753                 bv->lockedInsetStoreUndo(Undo::INSERT);
754                 lyxerr << "handling valign '" << arg << "'\n";
755                 int idx;
756                 MathArrayInset * p = matrixpar(idx);
757                 if (!p)
758                         break; 
759                 p->valign(arg.size() ? arg[0] : 'c');
760                 UpdateLocal(bv);
761                 break;
762         }
763
764         case LFUN_MATH_ROW_INSERT:
765         {
766                 bv->lockedInsetStoreUndo(Undo::INSERT);
767                 int idx;
768                 MathArrayInset * p = matrixpar(idx);
769                 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
770                 if (!p)
771                         break; 
772                 p->addRow(p->row(idx));
773                 UpdateLocal(bv);
774                 break;
775         }
776
777         case LFUN_MATH_ROW_DELETE:
778         {
779                 bv->lockedInsetStoreUndo(Undo::INSERT);
780                 int idx;
781                 MathArrayInset * p = matrixpar(idx);
782                 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
783                 if (!p)
784                         break; 
785                 p->delRow(p->row(idx));
786                 UpdateLocal(bv);
787                 break;
788         }
789
790         case LFUN_MATH_COLUMN_INSERT:
791         {
792                 bv->lockedInsetStoreUndo(Undo::INSERT);
793                 int idx;
794                 MathArrayInset * p = matrixpar(idx);
795                 if (!p)
796                         break; 
797                 p->addCol(p->col(idx));
798                 UpdateLocal(bv);
799                 break;
800         }
801
802         case LFUN_MATH_COLUMN_DELETE:
803         {
804                 bv->lockedInsetStoreUndo(Undo::INSERT);
805                 int idx;
806                 MathArrayInset * p = matrixpar(idx);
807                 if (!p)
808                         break; 
809                 p->delCol(p->col(idx));
810                 UpdateLocal(bv);
811                 break;
812         }
813
814         case LFUN_EXEC_COMMAND:
815                 result = UNDISPATCHED;
816                 break;
817
818
819                 //------- dummy actions
820 #ifdef WITH_WARNINGS
821 #warning Is this needed here? Shouldnt the main dispatch handle this? (Lgb)
822 #endif
823                 //case LFUN_EXEC_COMMAND:
824                 //bv->owner()->getMiniBuffer()->PrepareForCommand();
825                 //break;
826
827         default:
828                 if ((action == -1  || action == LFUN_SELFINSERT)
829                     && !arg.empty())  {
830                         unsigned char c = arg[0];
831                         bv->lockedInsetStoreUndo(Undo::INSERT);
832
833                         if (c == ' ' && mathcursor->getAccent() == LM_hat) {
834                                 c = '^';
835                                 mathcursor->setAccent(0);
836                         }
837
838                         if (c == 0) {      // Dead key, do nothing
839                                 //lyxerr << "deadkey" << endl;
840                                 break;
841                         }
842
843                         if (isalpha(c)) {
844                                 if (mathcursor->getLastCode() == LM_TC_TEX) {
845                                         mathcursor->MacroModeOpen();
846                                         mathcursor->clearLastCode();
847                                         varcode = LM_TC_MIN;
848                                 } else if (!varcode) {          
849                                         short f = mathcursor->getLastCode() ?
850                                                 mathcursor->getLastCode() :
851                                                 mathcursor->nextCode();
852                                         varcode = MathIsAlphaFont(f) ?
853                                                 static_cast<MathTextCodes>(f) :
854                                                 LM_TC_VAR;
855                                 }
856                                 
857                                 //           lyxerr << "Varcode << vardoce;
858                                 MathTextCodes char_code = varcode;
859                                 if (greek_kb_flag) {
860                                         char greek[26] =
861                                         {'A', 'B', 'X',  0 , 'E',  0 ,  0 , 'H', 'I',  0 ,
862                                          'K',  0 , 'M', 'N', 'O',  0 ,  0 , 'P',  0 , 'T',
863                                          'Y',  0,   0,   0,   0 , 'Z' };
864                                         
865                                         if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
866                                                 char_code = LM_TC_RM;
867                                                 c = greek[c - 'A'];
868                                         } else
869                                                 char_code = LM_TC_SYMB;
870                                 }
871                                 
872                                 mathcursor->insert(c, char_code);
873                                 
874                                 if (greek_kb_flag && char_code == LM_TC_RM )
875                                         mathcursor->setLastCode(LM_TC_VAR);
876                                 
877                                 varcode = LM_TC_MIN;
878                                 
879                                 if (greek_kb_flag < 2)
880                                         greek_kb_flag = 0;
881                                 
882                         } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
883                                 mathcursor->insert(c, LM_TC_TEX);
884                                 if (c == '{') {
885                                         mathcursor->insert('}', LM_TC_TEX);
886                                         mathcursor->Left();
887                                 }
888                                 mathcursor->clearLastCode();
889                                 //             varcode = LM_TC_MIN;
890                         } else if (c == '_' && varcode == LM_TC_TEX) {
891                                 mathcursor->insert(c, LM_TC_SPECIAL);
892                                 mathcursor->clearLastCode();
893                                 //             varcode = LM_TC_MIN;
894                         } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
895                                 mathcursor->MacroModeOpen();
896                                 mathcursor->clearLastCode();
897                                 mathcursor->insert(c, LM_TC_MIN);
898                         } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
899                                 mathcursor->insert(c, LM_TC_CONST);
900                         } else if (strchr("+/-*<>=", c)) {
901                                 mathcursor->insert(c, LM_TC_BOP);
902                         } else if (strchr(latex_special_chars, c) && c!= '_') {
903                                 mathcursor->insert(c, LM_TC_SPECIAL);
904                         } else if (c == '_' || c == '^') {
905                                 char s[2];
906                                 s[0] = c;
907                                 s[1] = 0;
908                                 mathcursor->Interpret(s);
909                         } else if (c == ' ') {  
910                                 if (!varcode) { 
911                                         short f = (mathcursor->getLastCode()) ?
912                                                 mathcursor->getLastCode() :
913                                                 mathcursor->nextCode();
914                                         varcode = MathIsAlphaFont(f) ?
915                                                 static_cast<MathTextCodes>(f) :
916                                                 LM_TC_VAR;
917                                 }
918                                 
919                                 if (varcode == LM_TC_TEXTRM) {
920                                         mathcursor->insert(c, LM_TC_TEXTRM);
921                                 } else if (was_macro) {
922                                         mathcursor->MacroModeClose();
923                                 } else if (sp) {
924                                         int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
925                                         sp->SetSpace(isp);
926                                         space_on = true;
927                                 } else {
928                                         lyxerr << "look here!\n";
929                                         //if (!mathcursor->cursor.pop() && !mathcursor->cursor.OK())
930                                         result = FINISHED;
931                                 }
932                         } else if (c == '\'' || c == '@') {
933                                 mathcursor->insert (c, LM_TC_VAR);
934                         } else if (c == '\\') {
935                                 if (was_macro)
936                                         mathcursor->MacroModeClose();
937                                 bv->owner()->message(_("TeX mode"));
938                                 mathcursor->setLastCode(LM_TC_TEX);
939                         }
940                         UpdateLocal(bv);
941                 } else if (action == LFUN_MATH_PANEL) {
942                         result = UNDISPATCHED;
943                 } else {
944                         lyxerr << "Closed by action " << action << endl;
945                         result =  FINISHED;
946                 }
947         }
948
949         if (mathcursor)
950                 mathcursor->normalize();
951
952         if (mathcursor && was_macro != mathcursor->InMacroMode()
953                                 && action >= 0
954                                 && action != LFUN_BACKSPACE) 
955                 UpdateLocal(bv);
956         
957         //if (mathcursor)
958         //              UpdateLocal(bv);
959
960         if (sp && !space_on)
961                 sp = 0;
962
963         if (mathcursor && (mathcursor->Selection() || was_selection))
964                 ToggleInsetSelection(bv);
965
966         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
967             result == UNDISPATCHED)
968                 ShowInsetCursor(bv);
969         else
970                 bv->unlockInset(this);
971
972         return result;  // original version
973 }
974
975
976
977 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
978  * on greek mode */
979 bool math_insert_greek(BufferView * bv, char c)
980 {
981         if (!bv->available())
982                 return false;
983
984         if (!isalpha(c))
985                 return false;
986
987         string tmp;
988         tmp = c;
989         if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
990                 int greek_kb_flag_save = greek_kb_flag;
991                 InsetFormula * new_inset = new InsetFormula();
992                 bv->beforeChange(bv->text);
993                 if (!bv->insertInset(new_inset)) {
994                         delete new_inset;
995                         return false;
996                 }
997                 //Update(1);//BUG
998                 new_inset->Edit(bv, 0, 0, 0);
999                 new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1000                 if (greek_kb_flag_save < 2) {
1001                         bv->unlockInset(new_inset); // bv->theLockingInset());
1002 #warning someone broke this in bolzano
1003                         //bv->text->cursorRight(bv, true);
1004                 }
1005         } else
1006                 if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
1007                                 bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
1008                         static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1009                 else
1010                         lyxerr << "Math error: attempt to write on a wrong "
1011                                 "class of inset." << endl;
1012         return true;
1013 }
1014
1015
1016
1017 Inset::Code InsetFormulaBase::LyxCode() const
1018 {
1019         return Inset::MATH_CODE;
1020 }
1021
1022
1023 LyXFont const InsetFormulaBase::ConvertFont(LyXFont const & f) const
1024 {
1025         // We have already discussed what was here
1026         LyXFont font(f);
1027         font.setLatex(LyXFont::OFF);
1028         return font;
1029 }
1030
1031 MathInset * InsetFormulaBase::par() const
1032 {
1033         return par_;
1034 }
1035