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