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