]> git.lyx.org Git - features.git/blob - src/mathed/formulabase.C
9191e48c3d1dd4dc5cb14b8a8ff4bcf92c4a52d4
[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 "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, int code)
81 {
82         bv->lockedInsetStoreUndo(Undo::EDIT);
83         mathcursor->handleAccent(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         par_->Metrics(LM_ST_TEXT);
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         par_->Metrics(LM_ST_TEXT);
412         bv->updateInset(this, true);
413 }
414
415
416 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
417                                           int x, int y, int /*button*/)
418 {
419         if (mathcursor) {
420                 hideInsetCursor(bv);
421                 x += par_->xo();
422                 y += par_->yo();
423                 mathcursor->SetPos(x, y);
424                 showInsetCursor(bv);
425                 if (sel_flag) {
426                         sel_flag = false;
427                         sel_x = 0;
428                         sel_y = 0;
429                 }
430                 bv->updateInset(this, false);
431         }
432 }
433
434
435 void InsetFormulaBase::insetButtonPress(BufferView * bv,
436                                         int x, int y, int /*button*/)
437 {
438         sel_flag = false;
439         sel_x = x;
440         sel_y = y;
441         if (mathcursor && mathcursor->Selection()) {
442                 mathcursor->SelClear();
443                 bv->updateInset(this, false);
444         }
445 }
446
447
448 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
449                                          int x, int y, int /*button*/)
450 {
451         if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
452                 sel_flag = true;
453                 hideInsetCursor(bv);
454                 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
455                 mathcursor->SelStart();
456                 showInsetCursor(bv);
457                 mathcursor->GetPos(sel_x, sel_y);
458         } else if (sel_flag) {
459                 hideInsetCursor(bv);
460                 x += par_->xo();
461                 y += par_->yo();
462                 mathcursor->SetPos(x, y);
463                 showInsetCursor(bv);
464                 mathcursor->GetPos(x, y);
465                 if (sel_x != x || sel_y != y)
466                         bv->updateInset(this, false);
467                 sel_x = x;
468                 sel_y = y;
469         }
470 }
471
472
473 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
474 {
475         lyxerr[Debug::MATHED]
476                 << "Used InsetFormulaBase::InsetKeyPress." << endl;
477 }
478
479
480
481 UpdatableInset::RESULT
482 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
483                             string const & arg)
484 {
485         //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
486         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
487
488         if (!mathcursor) 
489                 return UNDISPATCHED;
490
491         MathTextCodes varcode = LM_TC_MIN;
492         bool was_macro = mathcursor->InMacroMode();
493         bool sel = false;
494         bool was_selection = mathcursor->Selection();
495         RESULT result = DISPATCHED;
496
497         hideInsetCursor(bv);
498
499         if (mathcursor->getLastCode() == LM_TC_TEX)
500                 varcode = LM_TC_TEX;
501
502         mathcursor->normalize();
503
504         switch (action) {
505
506                 // --- Cursor Movements ---------------------------------------------
507
508         case LFUN_RIGHTSEL:
509                 sel = true; // fall through...
510
511         case LFUN_RIGHT:
512                 result = DISPATCH_RESULT(mathcursor->Right(sel));
513                 updateLocal(bv);
514                 break;
515
516
517         case LFUN_LEFTSEL:
518                 sel = true; // fall through
519
520         case LFUN_LEFT:
521                 result = DISPATCH_RESULT(mathcursor->Left(sel));
522                 updateLocal(bv);
523                 break;
524
525
526         case LFUN_UPSEL:
527                 sel = true;
528
529         case LFUN_UP:
530                 result = DISPATCH_RESULT(mathcursor->Up(sel));
531                 updateLocal(bv);
532                 break;
533
534
535         case LFUN_DOWNSEL:
536                 sel = true;
537
538         case LFUN_DOWN:
539                 result = DISPATCH_RESULT(mathcursor->Down(sel));
540                 updateLocal(bv);
541                 break;
542
543         case LFUN_HOME:
544                 mathcursor->Home();
545                 updateLocal(bv);
546                 break;
547
548         case LFUN_END:
549                 mathcursor->End();
550                 updateLocal(bv);
551                 break;
552
553         case LFUN_DELETE_LINE_FORWARD:
554                 bv->lockedInsetStoreUndo(Undo::DELETE);
555                 mathcursor->DelLine();
556                 updateLocal(bv);
557                 break;
558
559         case LFUN_TAB:
560                 mathcursor->idxNext();
561                 updateLocal(bv);
562                 break;
563
564         case LFUN_SHIFT_TAB:
565                 mathcursor->idxPrev();
566                 updateLocal(bv);
567                 break;
568
569         case LFUN_TABINSERT:
570                 bv->lockedInsetStoreUndo(Undo::EDIT);
571                 mathcursor->splitCell();
572                 updateLocal(bv);
573                 break;
574
575         case LFUN_BACKSPACE:
576                 if (!mathcursor->InMacroMode() && mathcursor->pos() == 0) {
577                         bv->lockedInsetStoreUndo(Undo::DELETE);
578                         mathcursor->pullArg();
579                         bv->updateInset(this, true);
580                         break;
581                 }
582                 if (!mathcursor->Left())
583                         break;
584                 // fall through...
585
586         case LFUN_DELETE:
587                 bv->lockedInsetStoreUndo(Undo::DELETE);
588                 mathcursor->Delete();
589                 bv->updateInset(this, true);
590                 break;
591
592                 //    case LFUN_GETXY:
593                 //      sprintf(dispatch_buffer, "%d %d",);
594                 //      dispatch_result = dispatch_buffer;
595                 //      break;
596         case LFUN_SETXY:
597         {
598                 lyxerr << "LFUN_SETXY broken!\n";
599                 int x;
600                 int y;
601                 int x1;
602                 int y1;
603                 istringstream is(arg.c_str());
604                 is >> x >> y;
605                 par_->GetXY(x1, y1);
606                 mathcursor->SetPos(x1 + x, y1 + y);
607                 updateLocal(bv);
608         }
609         break;
610
611                 // cursor selection ---------------------------- 
612
613         case LFUN_PASTE:
614                 if (was_macro)
615                         mathcursor->MacroModeClose();
616                 bv->lockedInsetStoreUndo(Undo::INSERT);
617                 mathcursor->SelPaste();
618                 updateLocal(bv);
619                 break;
620
621         case LFUN_CUT:
622                 bv->lockedInsetStoreUndo(Undo::DELETE);
623                 mathcursor->SelCut();
624                 updateLocal(bv);
625                 break;
626
627         case LFUN_COPY:
628                 mathcursor->SelCopy();
629                 break;
630
631         case LFUN_HOMESEL:
632         case LFUN_ENDSEL:
633         case LFUN_WORDRIGHTSEL:
634         case LFUN_WORDLEFTSEL:
635                 break;
636
637                 // --- accented characters ------------------------------
638
639         case LFUN_UMLAUT:     handleAccent(bv, LM_ddot); break;
640         case LFUN_CIRCUMFLEX: handleAccent(bv, LM_hat); break;
641         case LFUN_GRAVE:      handleAccent(bv, LM_grave); break;
642         case LFUN_ACUTE:      handleAccent(bv, LM_acute); break;
643         case LFUN_TILDE:      handleAccent(bv, LM_tilde); break;
644         case LFUN_MACRON:     handleAccent(bv, LM_bar); break;
645         case LFUN_DOT:        handleAccent(bv, LM_dot); break;
646         case LFUN_CARON:      handleAccent(bv, LM_check); break;
647         case LFUN_BREVE:      handleAccent(bv, LM_breve); break;
648         case LFUN_VECTOR:     handleAccent(bv, LM_vec); break;
649
650                 // Greek mode
651         case LFUN_GREEK:
652                 if (!greek_kb_flag) {
653                         greek_kb_flag = 1;
654                         bv->owner()->message(_("Math greek mode on"));
655                 } else
656                         greek_kb_flag = 0;
657                 break;
658
659                 // Greek keyboard
660         case LFUN_GREEK_TOGGLE:
661                 greek_kb_flag = greek_kb_flag ? 0 : 2;
662                 if (greek_kb_flag)
663                         bv->owner()->message(_("Math greek keyboard on"));
664                 else
665                         bv->owner()->message(_("Math greek keyboard off"));
666                 break;
667
668                 //  Math fonts
669         case LFUN_BOLD:    handleFont(bv, LM_TC_BF); break;
670         case LFUN_SANS:    handleFont(bv, LM_TC_SF); break;
671         case LFUN_EMPH:    handleFont(bv, LM_TC_CAL); break;
672         case LFUN_ROMAN:   handleFont(bv, LM_TC_RM); break;
673         case LFUN_CODE:    handleFont(bv, LM_TC_TT); break;
674         case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
675
676         case LFUN_MATH_MODE:
677                 handleFont(bv, LM_TC_TEXTRM);
678                 //bv->owner()->message(_("math text mode toggled"));
679                 break;
680
681 #ifndef NO_LATEX
682         case LFUN_TEX:
683                 if (!mathcursor->Selection()) {
684                         mathcursor->handleFont(LM_TC_TEX);
685                         //bv->owner()->message(_("TeX mode toggled"));
686                 }
687                 break;
688 #endif
689
690         case LFUN_MATH_LIMITS:
691                 bv->lockedInsetStoreUndo(Undo::INSERT);
692                 if (mathcursor->toggleLimits())
693                         updateLocal(bv);
694                 break;
695
696         case LFUN_MATH_SIZE:
697                 if (!arg.empty()) {
698                         bv->lockedInsetStoreUndo(Undo::INSERT);
699                         latexkeys const * l = in_word_set(arg);
700                         mathcursor->SetSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
701                         updateLocal(bv);
702                 }
703                 break;
704
705         case LFUN_INSERT_MATRIX:
706                 if (!arg.empty()) {
707                         bv->lockedInsetStoreUndo(Undo::INSERT);
708                         mathcursor->Interpret("matrix " + arg);
709                         updateLocal(bv);
710                 }
711                 break;
712
713         case LFUN_INSERT_MATH:
714                 if (!arg.empty()) {
715                         bv->lockedInsetStoreUndo(Undo::INSERT);
716                         mathcursor->Interpret(arg);
717                         updateLocal(bv);
718                 }
719                 break;
720
721         case LFUN_MATH_DELIM:
722         {
723                 bv->lockedInsetStoreUndo(Undo::INSERT);
724                 int ilt = '(';
725                 int irt = '.';
726                 static const string vdelim("(){}[]./|");
727                 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
728
729                 if (arg.empty())
730                         break;
731
732                 istringstream is(arg.c_str());
733                 string lt;
734                 string rt;
735                 is >> lt >> rt;
736                 lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
737                 lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
738
739                 if (lt.size() > 1) {
740                         latexkeys const * l = in_word_set(lt);
741                         if (l)
742                                 ilt = l->id;
743                 } else if (vdelim.find(lt[0]) != string::npos)
744                                 ilt = lt[0];
745
746                 if (rt.size() > 1) {
747                         latexkeys const * l = in_word_set(rt);
748                         if (l)
749                                 irt = l->id;
750                 } else if (vdelim.find(rt[0]) != string::npos)
751                                 irt = rt[0];
752
753                 handleDelim(bv, ilt, irt);
754                 updateLocal(bv);
755                 break;
756         }
757
758         case LFUN_PROTECTEDSPACE:
759                 bv->lockedInsetStoreUndo(Undo::INSERT);
760                 mathcursor->insert(new MathSpaceInset(1));
761                 updateLocal(bv);
762                 break;
763
764         case LFUN_UNDO:
765                 bv->owner()->message(_("Invalid action in math mode!"));
766                 break;
767
768
769         case LFUN_MATH_HALIGN:
770         {
771                 bv->lockedInsetStoreUndo(Undo::INSERT);
772                 lyxerr << "handling halign '" << arg << "'\n";
773                 int idx;
774                 MathArrayInset * p = matrixpar(idx);
775                 if (!p)
776                         break; 
777                 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
778                 updateLocal(bv);
779                 break;
780         }
781
782         case LFUN_MATH_VALIGN:
783         {
784                 bv->lockedInsetStoreUndo(Undo::INSERT);
785                 lyxerr << "handling valign '" << arg << "'\n";
786                 int idx;
787                 MathArrayInset * p = matrixpar(idx);
788                 if (!p)
789                         break; 
790                 p->valign(arg.size() ? arg[0] : 'c');
791                 updateLocal(bv);
792                 break;
793         }
794
795         case LFUN_MATH_ROW_INSERT:
796         {
797                 bv->lockedInsetStoreUndo(Undo::INSERT);
798                 int idx;
799                 MathArrayInset * p = matrixpar(idx);
800                 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
801                 if (!p)
802                         break; 
803                 p->addRow(p->row(idx));
804                 updateLocal(bv);
805                 break;
806         }
807
808         case LFUN_MATH_ROW_DELETE:
809         {
810                 bv->lockedInsetStoreUndo(Undo::INSERT);
811                 int idx;
812                 MathArrayInset * p = matrixpar(idx);
813                 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
814                 if (!p)
815                         break; 
816                 p->delRow(p->row(idx));
817                 updateLocal(bv);
818                 break;
819         }
820
821         case LFUN_MATH_COLUMN_INSERT:
822         {
823                 bv->lockedInsetStoreUndo(Undo::INSERT);
824                 int idx;
825                 MathArrayInset * p = matrixpar(idx);
826                 if (!p)
827                         break; 
828                 p->addCol(p->col(idx));
829                 updateLocal(bv);
830                 break;
831         }
832
833         case LFUN_MATH_COLUMN_DELETE:
834         {
835                 bv->lockedInsetStoreUndo(Undo::INSERT);
836                 int idx;
837                 MathArrayInset * p = matrixpar(idx);
838                 if (!p)
839                         break; 
840                 p->delCol(p->col(idx));
841                 updateLocal(bv);
842                 break;
843         }
844
845         case LFUN_EXEC_COMMAND:
846                 result = UNDISPATCHED;
847                 break;
848
849         default:
850                 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
851                         unsigned char c = arg[0];
852                         //lyxerr << "char: '" << c << "'  int: " << int(c) << endl;
853                         //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);      
854                         //lyxerr << "trans: '" << c << "'  int: " << int(c) << endl;
855                         bv->lockedInsetStoreUndo(Undo::INSERT);
856
857                         if (c == 0) {      // Dead key, do nothing
858                                 //lyxerr << "deadkey" << endl;
859                                 break;
860                         }
861
862                         if (isalpha(c)) {
863                                 if (mathcursor->getLastCode() == LM_TC_TEX) {
864                                         mathcursor->MacroModeOpen();
865                                         mathcursor->clearLastCode();
866                                         varcode = LM_TC_MIN;
867                                 } else if (!varcode) {          
868                                         short f = mathcursor->getLastCode() ?
869                                                 mathcursor->getLastCode() :
870                                                 mathcursor->nextCode();
871                                         varcode = MathIsAlphaFont(f) ?
872                                                 static_cast<MathTextCodes>(f) :
873                                                 LM_TC_VAR;
874                                 }
875                                 
876                                 //           lyxerr << "Varcode << vardoce;
877                                 MathTextCodes char_code = varcode;
878                                 if (greek_kb_flag) {
879                                         char greek[26] =
880                                         {'A', 'B', 'X',  0 , 'E',  0 ,  0 , 'H', 'I',  0 ,
881                                          'K',  0 , 'M', 'N', 'O',  0 ,  0 , 'P',  0 , 'T',
882                                          'Y',  0,   0,   0,   0 , 'Z' };
883                                         
884                                         if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
885                                                 char_code = LM_TC_RM;
886                                                 c = greek[c - 'A'];
887                                         } else
888                                                 char_code = LM_TC_SYMB;
889                                 }
890                                 
891                                 mathcursor->insert(c, char_code);
892                                 
893                                 if (greek_kb_flag && char_code == LM_TC_RM )
894                                         mathcursor->setLastCode(LM_TC_VAR);
895                                 
896                                 varcode = LM_TC_MIN;
897                                 
898                                 if (greek_kb_flag < 2)
899                                         greek_kb_flag = 0;
900                                 
901                         } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
902                                 mathcursor->insert(c, LM_TC_TEX);
903                                 if (c == '{') {
904                                         mathcursor->insert('}', LM_TC_TEX);
905                                         mathcursor->Left();
906                                 }
907                                 mathcursor->clearLastCode();
908                                 //             varcode = LM_TC_MIN;
909                         } else if (c == '_' && varcode == LM_TC_TEX) {
910                                 mathcursor->insert(c, LM_TC_SPECIAL);
911                                 mathcursor->clearLastCode();
912                                 //             varcode = LM_TC_MIN;
913                         } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
914                                 mathcursor->MacroModeOpen();
915                                 mathcursor->clearLastCode();
916                                 mathcursor->insert(c, LM_TC_MIN);
917                         } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
918                                 MathTextCodes code = mathcursor->getLastCode();
919                                 if (code != LM_TC_TEXTRM)
920                                         code = LM_TC_CONST;
921                                 mathcursor->insert(c, code);
922                         } else if (strchr("+/-*<>=", c)) {
923                                 MathTextCodes code = mathcursor->getLastCode();
924                                 if (code != LM_TC_TEXTRM)
925                                         code = LM_TC_BOP;
926                                 mathcursor->insert(c, code);
927                         } else if (strchr(latex_special_chars, c) && c != '_') {
928                                 MathTextCodes code = mathcursor->getLastCode();
929                                 if (code != LM_TC_TEXTRM)
930                                         code = LM_TC_SPECIAL;
931                                 mathcursor->insert(c, code);
932                         } else if (c == '_' || c == '^') {
933                                 char s[2];
934                                 s[0] = c;
935                                 s[1] = 0;
936                                 mathcursor->Interpret(s);
937                         } else if (c == ' ') {  
938                                 if (!varcode) { 
939                                         short f = (mathcursor->getLastCode()) ?
940                                                 mathcursor->getLastCode() :
941                                                 mathcursor->nextCode();
942                                         varcode = MathIsAlphaFont(f) ?
943                                                 static_cast<MathTextCodes>(f) :
944                                                 LM_TC_VAR;
945                                 }
946                                 
947                                 if (varcode == LM_TC_TEXTRM) {
948                                         mathcursor->insert(c, LM_TC_TEXTRM);
949                                 } else if (was_macro) {
950                                         mathcursor->MacroModeClose();
951                                 } else {
952                                         if (!mathcursor->pop())
953                                                 result = FINISHED;
954                                         mathcursor->plainRight();
955                                 }
956                         } else if (c == '\'' || c == '@') {
957                                 mathcursor->insert (c, LM_TC_VAR);
958                         } else if (c == '\\') {
959                                 if (was_macro)
960                                         mathcursor->MacroModeClose();
961                                 bv->owner()->message(_("TeX mode"));
962                                 mathcursor->setLastCode(LM_TC_TEX);
963                         }
964                         updateLocal(bv);
965                 } else if (action == LFUN_MATH_PANEL) {
966                         result = UNDISPATCHED;
967                 } else {
968                         lyxerr << "Closed by action " << action << endl;
969                         result =  FINISHED;
970                 }
971         }
972
973         mathcursor->normalize();
974
975         if (mathcursor && was_macro != mathcursor->InMacroMode()
976                                 && action >= 0
977                                 && action != LFUN_BACKSPACE) 
978                 updateLocal(bv);
979         
980         //if (mathcursor)
981         //              updateLocal(bv);
982
983         if (mathcursor && (mathcursor->Selection() || was_selection))
984                 toggleInsetSelection(bv);
985
986         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
987             result == UNDISPATCHED)
988                 showInsetCursor(bv);
989         else
990                 bv->unlockInset(this);
991
992         return result;  // original version
993 }
994
995
996
997 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
998  * on greek mode */
999 bool math_insert_greek(BufferView * bv, char c)
1000 {
1001         if (!bv->available())
1002                 return false;
1003
1004         if (!isalpha(c))
1005                 return false;
1006
1007         string tmp;
1008         tmp = c;
1009         if (!bv->theLockingInset() || bv->theLockingInset()->isTextInset()) {
1010                 int greek_kb_flag_save = greek_kb_flag;
1011                 InsetFormula * new_inset = new InsetFormula();
1012                 bv->beforeChange(bv->text);
1013                 if (!bv->insertInset(new_inset)) {
1014                         delete new_inset;
1015                         return false;
1016                 }
1017                 //Update(1);//BUG
1018                 new_inset->edit(bv, 0, 0, 0);
1019                 new_inset->localDispatch(bv, LFUN_SELFINSERT, tmp);
1020                 if (greek_kb_flag_save < 2) {
1021                         bv->unlockInset(new_inset); // bv->theLockingInset());
1022                         bv->text->cursorRight(bv, true);
1023                 }
1024         } else
1025                 if (bv->theLockingInset()->lyxCode() == Inset::MATH_CODE ||
1026                                 bv->theLockingInset()->lyxCode() == Inset::MATHMACRO_CODE)
1027                         static_cast<InsetFormula*>(bv->theLockingInset())->localDispatch(bv, LFUN_SELFINSERT, tmp);
1028                 else
1029                         lyxerr << "Math error: attempt to write on a wrong "
1030                                 "class of inset." << endl;
1031         return true;
1032 }
1033
1034
1035
1036 Inset::Code InsetFormulaBase::lyxCode() const
1037 {
1038         return Inset::MATH_CODE;
1039 }
1040
1041
1042 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
1043 {
1044         // We have already discussed what was here
1045         LyXFont font(f);
1046 #ifndef NO_LATEX
1047         font.setLatex(LyXFont::OFF);
1048 #endif
1049         return font;
1050 }
1051
1052 MathInset * InsetFormulaBase::par() const
1053 {
1054         return par_;
1055 }
1056
1057
1058 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
1059 {
1060         if (bv->available()) {
1061 // Feature "Read math inset from selection" disabled.
1062 //              // use selection if available..
1063 //              string sel;
1064 //              if (action == LFUN_MATH_IMPORT_SELECTION)
1065 //                      sel = "";
1066 //              else
1067 //                      sel = bv->getLyXText()->selectionAsString(bv->buffer());
1068
1069                         InsetFormula * f;
1070 //              if (sel.empty()) {
1071                                 f = new InsetFormula;
1072                                 if (openNewInset(bv, f)) {
1073                                         // don't do that also for LFUN_MATH_MODE unless you want end up with
1074                                         // always changing to mathrm when opening an inlined inset
1075                                         // -- I really hate "LyXfunc overloading"...
1076                                         if (display)
1077                                                 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
1078                                         f->localDispatch(bv, LFUN_INSERT_MATH, arg);
1079                                 }
1080 //              } else {
1081 //                      f = new InsetFormula(sel);
1082 //                      bv->getLyXText()->cutSelection(bv);
1083 //                      openNewInset(bv, f);
1084 //              }
1085         }
1086         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
1087 }
1088
1089 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
1090 {
1091         mathDispatchCreation(bv, arg, true);
1092 }
1093         
1094 void mathDispatchMathMode(BufferView * bv, string const & arg)
1095 {
1096         mathDispatchCreation(bv, arg, false);
1097 }
1098
1099 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
1100 {
1101         mathDispatchCreation(bv, arg, true);
1102 }
1103
1104 void mathDispatchMathMacro(BufferView * bv, string const & arg)
1105 {
1106         if (bv->available()) {
1107                 string s(arg);
1108                 if (s.empty())
1109                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1110                 else {
1111                         string const s1 = token(s, ' ', 1);
1112                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
1113                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1114                 }
1115         }
1116 }
1117
1118 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1119 {          
1120         if (bv->available()) { 
1121                 if (openNewInset(bv, new InsetFormula))
1122                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1123         }
1124 }          
1125
1126
1127 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1128 {          
1129         if (bv->available()) { 
1130                 if (openNewInset(bv, new InsetFormula))
1131                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1132         }
1133 }          
1134
1135 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1136 {
1137         if (bv->available()) {
1138                 if (arg.size() && arg[0] == '\\') {
1139                         InsetFormula * f = new InsetFormula(arg);
1140                         openNewInset(bv, f);
1141                 } else {
1142                         mathDispatchMathMode(bv, arg);
1143                 }
1144         }
1145 }
1146