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