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