]> git.lyx.org Git - features.git/blob - src/mathed/formulabase.C
*duck*
[features.git] / src / mathed / formulabase.C
1 /*
2 *  File:        formulabase.C
3 *  Purpose:     Implementation of common parts of the LyX  math insets
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *
7 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
8 *
9 *  Version: 0.4, Lyx project.
10 *
11 *   You are free to use and modify this code under the terms of
12 *   the GNU General Public Licence version 2 or later.
13 */
14
15 #include <config.h>
16 #include <fstream>
17
18 #include "Lsstream.h"
19 #include "support/LAssert.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 "BufferView.h"
29 #include "lyxtext.h"
30 #include "lyxfunc.h"
31 #include "gettext.h"
32 #include "LaTeXFeatures.h"
33 #include "debug.h"
34 #include "math_support.h"
35 #include "support/lstrings.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/font_metrics.h"
38 #include "frontends/mouse_state.h"
39 #include "Lsstream.h"
40 #include "math_arrayinset.h"
41 #include "math_charinset.h"
42 #include "math_cursor.h"
43 #include "math_factory.h"
44 #include "math_fontinset.h"
45 #include "math_hullinset.h"
46 #include "math_iterator.h"
47 #include "math_macrotable.h"
48 #include "math_parser.h"
49 #include "math_pos.h"
50 #include "math_spaceinset.h"
51 #include "undo_funcs.h"
52 #include "textpainter.h"
53 #include "frontends/Dialogs.h"
54 #include "intl.h"
55
56 using std::endl;
57 using std::ostream;
58 using std::vector;
59 using std::abs;
60
61 MathCursor * mathcursor = 0;
62
63
64 namespace {
65
66 // local global
67 int first_x;
68 int first_y;
69
70
71
72 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
73 {
74         if (!bv->insertInset(new_inset)) {
75                 delete new_inset;
76                 return false;
77         }
78         new_inset->edit(bv, 0, 0, mouse_button::none);
79         return true;
80 }
81
82
83 } // namespace anon
84
85
86
87 InsetFormulaBase::InsetFormulaBase()
88         : view_(0), font_(), xo_(0), yo_(0)
89 {
90         // This is needed as long the math parser is not re-entrant
91         MathMacroTable::builtinMacros();
92         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
93         //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
94         //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
95         //lyxerr << "sizeof(LyXFont): " << sizeof(LyXFont) << "\n";
96 }
97
98
99 // simply scrap this function if you want
100 void InsetFormulaBase::mutateToText()
101 {
102 #if 0
103         // translate to latex
104         ostringstream os;
105         latex(NULL, os, false, false);
106         string str = os.str();
107
108         // insert this text
109         LyXText * lt = view_->getLyXText();
110         string::const_iterator cit = str.begin();
111         string::const_iterator end = str.end();
112         for (; cit != end; ++cit)
113                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
114
115         // remove ourselves
116         //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
117 #endif
118 }
119
120
121 void InsetFormulaBase::handleFont
122         (BufferView * bv, string const & arg, string const & font)
123 {
124         bv->lockedInsetStoreUndo(Undo::EDIT);
125         bool sel = mathcursor->selection();
126         if (sel)
127                 updateLocal(bv, true);
128         mathcursor->handleNest(new MathFontInset(font));
129   for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
130     mathcursor->insert(*it);
131         if (!sel)
132                 updateLocal(bv, false);
133 }
134
135
136 // Check if uses AMS macros
137 void InsetFormulaBase::validate(LaTeXFeatures &) const
138 {}
139
140
141 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
142 {
143         font_ = f;
144         metrics(bv);
145 }
146
147
148 void InsetFormulaBase::metrics(BufferView * bv) const
149 {
150         if (bv)
151                 view_ = bv;
152         MathMetricsInfo mi;
153         mi.view       = view_;
154         mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
155         mi.base.font  = font_;
156         mi.base.font.setColor(LColor::math);
157         par()->metrics(mi);
158 }
159
160
161 string const InsetFormulaBase::editMessage() const
162 {
163         return _("Math editor mode");
164 }
165
166
167 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
168 {
169         if (!bv->lockInset(this))
170                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
171         delete mathcursor;
172         mathcursor = new MathCursor(this, true);
173         metrics(bv);
174         mathcursor->setPos(x, y);
175         //lyxerr << "setting pos to " << x << "," << y << "\n";
176
177         // if that is removed, we won't get the magenta box when entering an
178         // inset for the first time
179         bv->updateInset(this, false);
180 }
181
182
183 void InsetFormulaBase::edit(BufferView * bv, bool front)
184 {
185         if (!bv->lockInset(this))
186                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
187         delete mathcursor;
188         mathcursor = new MathCursor(this, front);
189         metrics(bv);
190         bv->updateInset(this, false);
191 }
192
193
194 void InsetFormulaBase::insetUnlock(BufferView * bv)
195 {
196         if (mathcursor) {
197                 if (mathcursor->inMacroMode()) {
198                         mathcursor->macroModeClose();
199                         updateLocal(bv, true);
200                 }
201                 delete mathcursor;
202                 mathcursor = 0;
203         }
204         bv->updateInset(this, false);
205 }
206
207
208 void InsetFormulaBase::getCursorPos(BufferView * bv, int & x, int & y) const
209 {
210         metrics(bv);
211         mathcursor->getPos(x, y);
212         //x -= xo_;
213         y -= yo_;
214         //lyxerr << "getCursorPos: " << x << " " << y << "\n";
215 }
216
217
218 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
219 {
220         if (!mathcursor)
221                 return;
222
223         if (isCursorVisible())
224                 bv->hideLockedInsetCursor();
225         else {
226                 metrics(bv);
227                 int x;
228                 int y;
229                 mathcursor->getPos(x, y);
230                 y -= yo_;
231                 int asc = 0;
232                 int des = 0;
233                 math_font_max_dim(font_, asc, des);
234                 bv->showLockedInsetCursor(x, y, asc, des);
235                 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
236         }
237
238         toggleCursorVisible();
239 }
240
241
242 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
243 {
244         if (isCursorVisible())
245                 return;
246         if (mathcursor) {
247                 metrics(bv);
248                 int x;
249                 int y;
250                 mathcursor->getPos(x, y);
251                 y -= yo_;
252                 int asc = 0;
253                 int des = 0;
254                 math_font_max_dim(font_, asc, des);
255                 bv->fitLockedInsetCursor(x, y, asc, des);
256                 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
257         }
258         toggleInsetCursor(bv);
259 }
260
261
262 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
263 {
264         if (isCursorVisible())
265                 toggleInsetCursor(bv);
266 }
267
268
269 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
270 {
271         if (!mathcursor)
272                 return;
273         
274         int const asc = font_metrics::maxAscent(font_);
275         int const desc = font_metrics::maxDescent(font_);
276         int x, y;
277
278         getCursorPos(bv, x, y);
279         bv->fitLockedInsetCursor(x, y, asc, desc);
280 }
281
282
283 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
284 {
285         if (mathcursor)
286                 bv->updateInset(this, false);
287 }
288
289
290 vector<string> const InsetFormulaBase::getLabelList() const
291 {
292   return vector<string>();
293 }
294
295
296 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
297 {
298         metrics(bv);
299         if (mathcursor)
300                 bv->fitCursor();
301         bv->updateInset(this, dirty);
302 }
303
304
305 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
306                                           int /*x*/, int /*y*/, mouse_button::state button)
307 {
308         //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
309
310         if (!mathcursor)
311                 return false;
312         hideInsetCursor(bv);
313         showInsetCursor(bv);
314         bv->updateInset(this, false);
315
316         if (button == mouse_button::button3) {
317                 // launch math panel for right mouse button
318                 bv->owner()->getDialogs()->showMathPanel();
319                 return true;
320         }
321         return false;
322 }
323
324
325 void InsetFormulaBase::insetButtonPress(BufferView * bv,
326                                         int x, int y, mouse_button::state button)
327 {
328         //lyxerr << "insetButtonPress: "
329         //      << x << " " << y << " but: " << button << "\n";
330 #if 0
331         switch (button) {
332                 default:
333                 case 1:
334                         // left click
335                         delete mathcursor;
336                         mathcursor = new MathCursor(this, x == 0);
337                         metrics(bv);
338                         first_x = x;
339                         first_y = y;
340                         mathcursor->selClear();
341                         mathcursor->setPos(x + xo_, y + yo_);
342                         break;
343 /*
344                 case 2:
345                         lyxerr << "insetButtonPress: 2\n";
346                         // insert stuff
347                         if (mathcursor) {
348                                 bv->lockedInsetStoreUndo(Undo::EDIT);
349                                 MathArray ar;
350                                 mathcursor->selGet(ar);
351                                 mathcursor->setPos(x + xo_, y + yo_);
352                                 string sel =
353                                         bv->getLyXText()->selectionAsString(bv->buffer(), false);
354                                 mathed_parse_cell(ar, sel);
355                                 mathcursor->insert(ar);
356                         }
357                         break;
358 */
359                 case 3:
360                         // launch math panel for right mouse button
361                         bv->owner()->getDialogs()->showMathPanel();
362                         break;
363         }
364 #else
365         if (button == mouse_button::button1 || !mathcursor) {
366                 delete mathcursor;
367                 mathcursor = new MathCursor(this, x == 0);
368                 metrics(bv);
369                 first_x = x;
370                 first_y = y;
371                 mathcursor->selClear();
372                 mathcursor->setPos(x + xo_, y + yo_);
373         }
374 #if 0
375 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
376         if (button == 3) {
377                 // launch math panel for right mouse button
378                 bv->owner()->getDialogs()->showMathPanel();
379         }
380 #endif
381 #endif
382         bv->updateInset(this, false);
383 }
384
385
386 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
387         int x, int y, mouse_button::state)
388 {
389         if (!mathcursor)
390                 return;
391
392         if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
393                 //lyxerr << "insetMotionNotify: ignored\n";
394                 return;
395         }
396         first_x = x;
397         first_y = y;
398
399         if (!mathcursor->selection())
400                 mathcursor->selStart();
401
402         //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
403         //      << ' ' << button << "\n";
404         hideInsetCursor(bv);
405         mathcursor->setPos(x + xo_, y + yo_);
406         showInsetCursor(bv);
407         bv->updateInset(this, false);
408 }
409
410
411 UpdatableInset::RESULT
412 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
413                             string const & arg)
414 {
415         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
416         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
417
418         if (!mathcursor)
419                 return UNDISPATCHED;
420
421         RESULT result      = DISPATCHED;
422         bool sel           = false;
423         bool was_macro     = mathcursor->inMacroMode();
424         bool was_selection = mathcursor->selection();
425
426         hideInsetCursor(bv);
427
428         mathcursor->normalize();
429         mathcursor->touch();
430
431         switch (action) {
432
433                 // --- Cursor Movements ---------------------------------------------
434
435         case LFUN_RIGHTSEL:
436                 sel = true; // fall through...
437
438         case LFUN_RIGHT:
439                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
440                 //lyxerr << "calling scroll 20\n";
441                 //scroll(bv, 20);
442                 updateLocal(bv, false);
443                 // write something to the minibuffer
444                 //bv->owner()->message(mathcursor->info());
445                 break;
446
447
448         case LFUN_LEFTSEL:
449                 sel = true; // fall through
450
451         case LFUN_LEFT:
452                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
453                 updateLocal(bv, false);
454                 break;
455
456
457         case LFUN_UPSEL:
458                 sel = true;
459
460         case LFUN_UP:
461                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
462                 updateLocal(bv, false);
463                 break;
464
465
466         case LFUN_DOWNSEL:
467                 sel = true;
468
469         case LFUN_DOWN:
470                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
471                 updateLocal(bv, false);
472                 break;
473
474         case LFUN_HOMESEL:
475                 sel = true;
476
477         case LFUN_HOME:
478                 mathcursor->home(sel);
479                 updateLocal(bv, false);
480                 break;
481
482         case LFUN_ENDSEL:
483                 sel = true;
484
485         case LFUN_END:
486                 mathcursor->end(sel);
487                 updateLocal(bv, false);
488                 break;
489
490         case LFUN_DELETE_LINE_FORWARD:
491                 bv->lockedInsetStoreUndo(Undo::DELETE);
492                 mathcursor->delLine();
493                 updateLocal(bv, true);
494                 break;
495
496         case LFUN_TAB:
497                 mathcursor->idxNext();
498                 updateLocal(bv, false);
499                 break;
500
501         case LFUN_SHIFT_TAB:
502                 mathcursor->idxPrev();
503                 updateLocal(bv, false);
504                 break;
505
506         case LFUN_TABINSERT:
507                 bv->lockedInsetStoreUndo(Undo::EDIT);
508                 mathcursor->splitCell();
509                 updateLocal(bv, true);
510                 break;
511
512         case LFUN_DELETE_WORD_BACKWARD:
513         case LFUN_BACKSPACE:
514                 bv->lockedInsetStoreUndo(Undo::DELETE);
515                 mathcursor->backspace();
516                 bv->updateInset(this, true);
517                 break;
518
519         case LFUN_DELETE_WORD_FORWARD:
520         case LFUN_DELETE:
521                 bv->lockedInsetStoreUndo(Undo::DELETE);
522                 mathcursor->erase();
523                 bv->updateInset(this, true);
524                 break;
525
526         //    case LFUN_GETXY:
527         //      sprintf(dispatch_buffer, "%d %d",);
528         //      dispatch_result = dispatch_buffer;
529         //      break;
530         case LFUN_SETXY: {
531                 lyxerr << "LFUN_SETXY broken!\n";
532                 int x = 0;
533                 int y = 0;
534                 istringstream is(arg.c_str());
535                 is >> x >> y;
536                 mathcursor->setPos(x, y);
537                 updateLocal(bv, false);
538                 break;
539         }
540
541         case LFUN_PASTE:
542                 if (was_macro)
543                         mathcursor->macroModeClose();
544                 bv->lockedInsetStoreUndo(Undo::EDIT);
545                 mathcursor->selPaste();
546                 updateLocal(bv, true);
547                 break;
548
549         case LFUN_CUT:
550                 bv->lockedInsetStoreUndo(Undo::DELETE);
551                 mathcursor->selCut();
552                 updateLocal(bv, true);
553                 break;
554
555         case LFUN_COPY:
556                 mathcursor->selCopy();
557                 break;
558
559         case LFUN_WORDRIGHTSEL:
560         case LFUN_WORDLEFTSEL:
561                 break;
562
563         // Special casing for superscript in case of LyX handling
564         // dead-keys:
565         case LFUN_CIRCUMFLEX:
566                 if (arg.empty()) {
567                         // do superscript if LyX handles
568                         // deadkeys
569                         bv->lockedInsetStoreUndo(Undo::EDIT);
570                         mathcursor->script(true);
571                         updateLocal(bv, true);
572                 }
573                 break;
574         case LFUN_UMLAUT:
575         case LFUN_ACUTE:
576         case LFUN_GRAVE:
577         case LFUN_BREVE:
578         case LFUN_DOT:
579         case LFUN_MACRON:
580         case LFUN_CARON:
581         case LFUN_TILDE:
582         case LFUN_CEDILLA:
583         case LFUN_CIRCLE:
584         case LFUN_UNDERDOT:
585         case LFUN_TIE:
586         case LFUN_OGONEK:
587         case LFUN_HUNG_UMLAUT:
588                 break;
589
590         //  Math fonts
591         case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
592         case LFUN_BOLD:         handleFont(bv, arg, "textbf"); break;
593         case LFUN_SANS:         handleFont(bv, arg, "textsf"); break;
594         case LFUN_EMPH:         handleFont(bv, arg, "mathcal"); break;
595         case LFUN_ROMAN:        handleFont(bv, arg, "mathrm"); break;
596         case LFUN_CODE:         handleFont(bv, arg, "texttt"); break;
597         case LFUN_FRAK:         handleFont(bv, arg, "mathfrak"); break;
598         case LFUN_ITAL:         handleFont(bv, arg, "mathit"); break;
599         case LFUN_NOUN:         handleFont(bv, arg, "mathbb"); break;
600         case LFUN_DEFAULT:      handleFont(bv, arg, "textnormal"); break;
601         case LFUN_FREE:         handleFont(bv, arg, "textrm"); break;
602
603         case LFUN_GREEK:
604                 handleFont(bv, arg, "lyxgreek1");
605                 if (arg.size())
606                         mathcursor->interpret(arg);
607                 break;
608
609         case LFUN_MATH_MODE:
610                 if (mathcursor->inMathMode()) {
611                         handleFont(bv, arg, "textrm");
612                 } else {
613                         mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
614                         updateLocal(bv, true);
615                 }
616                 //bv->owner()->message(_("math text mode toggled"));
617                 break;
618
619         case LFUN_MATH_LIMITS:
620                 bv->lockedInsetStoreUndo(Undo::EDIT);
621                 if (mathcursor->toggleLimits())
622                         updateLocal(bv, true);
623                 break;
624
625         case LFUN_MATH_SIZE:
626 #if 0
627                 if (!arg.empty()) {
628                         bv->lockedInsetStoreUndo(Undo::EDIT);
629                         mathcursor->setSize(arg);
630                         updateLocal(bv, true);
631                 }
632 #endif
633                 break;
634
635         case LFUN_INSERT_MATRIX:
636                 if (!arg.empty()) {
637                         bv->lockedInsetStoreUndo(Undo::EDIT);
638                         mathcursor->interpret("matrix " + arg);
639                         updateLocal(bv, true);
640                 }
641                 break;
642
643         case LFUN_SUPERSCRIPT:
644         case LFUN_SUBSCRIPT:
645         {
646                 bv->lockedInsetStoreUndo(Undo::EDIT);
647                 mathcursor->script(action == LFUN_SUPERSCRIPT);
648                 updateLocal(bv, true);
649                 break;
650         }
651
652         case LFUN_MATH_DELIM:
653         {
654                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
655                 string ls;
656                 string rs = split(arg, ls, ' ');
657                 // Reasonable default values
658                 if (ls.empty())
659                         ls = '(';
660                 if (rs.empty())
661                         rs = ')';
662
663                 bv->lockedInsetStoreUndo(Undo::EDIT);
664                 mathcursor->handleDelim(ls, rs);
665                 updateLocal(bv, true);
666                 break;
667         }
668
669         case LFUN_PROTECTEDSPACE:
670         case LFUN_MATH_SPACE:
671                 bv->lockedInsetStoreUndo(Undo::EDIT);
672                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
673                 updateLocal(bv, true);
674                 break;
675
676         case LFUN_UNDO:
677                 bv->owner()->message(_("Invalid action in math mode!"));
678                 break;
679
680
681         case LFUN_MATH_HALIGN:
682         case LFUN_MATH_VALIGN:
683         case LFUN_MATH_ROW_INSERT:
684         case LFUN_MATH_ROW_DELETE:
685         case LFUN_MATH_COLUMN_INSERT:
686         case LFUN_MATH_COLUMN_DELETE:
687         {
688                 MathInset::idx_type idx = 0;
689                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
690                 if (p) {
691                         mathcursor->popToEnclosingGrid();
692                         bv->lockedInsetStoreUndo(Undo::EDIT);
693                         char align = arg.size() ? arg[0] : 'c';
694                         switch (action) {
695                                 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
696                                 case LFUN_MATH_VALIGN: p->valign(align); break;
697                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
698                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
699                                 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
700                                 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
701                                 default: ;
702                         }
703                         updateLocal(bv, true);
704                 }
705                 break;
706         }
707
708         case LFUN_EXEC_COMMAND:
709                 result = UNDISPATCHED;
710                 break;
711
712         case LFUN_BREAKPARAGRAPH:
713         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
714                 //lyxerr << "LFUN ignored\n";
715                 break;
716
717         case LFUN_INSET_ERT:
718                 // interpret this as if a backslash was typed
719                 bv->lockedInsetStoreUndo(Undo::EDIT);
720                 mathcursor->interpret('\\');
721                 updateLocal(bv, true);
722                 break;
723
724         case -1:
725         case LFUN_INSERT_MATH:
726         case LFUN_SELFINSERT:
727                 if (!arg.empty()) {
728                         bv->lockedInsetStoreUndo(Undo::EDIT);
729                         if (arg.size() == 1)
730                                 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
731                         else
732                                 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
733                         updateLocal(bv, true);
734                 }
735                 break;
736
737         case LFUN_MATH_PANEL:
738                 result = UNDISPATCHED;
739                 break;
740
741         case LFUN_ESCAPE:
742                 if (mathcursor->selection())
743                         mathcursor->selClear();
744                 else
745                         result = UNDISPATCHED;
746                 break;
747
748         case LFUN_INSET_TOGGLE:
749                 mathcursor->insetToggle();
750                 updateLocal(bv, true);
751                 break;
752
753         default:
754                 result = UNDISPATCHED;
755         }
756
757         mathcursor->normalize();
758         mathcursor->touch();
759
760         lyx::Assert(mathcursor);
761
762         if (mathcursor->selection() || was_selection)
763                 toggleInsetSelection(bv);
764
765         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
766             result == UNDISPATCHED)
767                 showInsetCursor(bv);
768         else
769                 bv->unlockInset(this);
770
771         revealCodes(bv);
772
773         return result;  // original version
774 }
775
776
777 void InsetFormulaBase::revealCodes(BufferView * bv) const
778 {
779         if (!mathcursor)
780                 return;
781         bv->owner()->message(mathcursor->info());
782
783 #if 0
784         // write something to the minibuffer
785         // translate to latex
786         mathcursor->markInsert();
787         ostringstream os;
788         write(NULL, os);
789         string str = os.str();
790         mathcursor->markErase();
791         string::size_type pos = 0;
792         string res;
793         for (string::iterator it = str.begin(); it != str.end(); ++it) {
794                 if (*it == '\n')
795                         res += ' ';
796                 else if (*it == '\0') {
797                         res += "  -X-  ";
798                         pos = it - str.begin();
799                 }
800                 else
801                         res += *it;
802         }
803         if (pos > 30)
804                 res = res.substr(pos - 30);
805         if (res.size() > 60)
806                 res = res.substr(0, 60);
807         bv->owner()->message(res);
808 #endif
809 }
810
811
812 Inset::Code InsetFormulaBase::lyxCode() const
813 {
814         return Inset::MATH_CODE;
815 }
816
817
818 int InsetFormulaBase::ylow() const
819 {
820         return yo_ - ascent(view_, font_);
821 }
822
823
824 int InsetFormulaBase::yhigh() const
825 {
826         return yo_ + descent(view_, font_);
827 }
828
829
830 int InsetFormulaBase::xlow() const
831 {
832         return xo_;
833 }
834
835
836 int InsetFormulaBase::xhigh() const
837 {
838         return xo_ + width(view_, font_);
839 }
840
841
842 /////////////////////////////////////////////////////////////////////
843
844
845 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
846                                      bool, bool)
847 {
848 #ifdef WITH_WARNINGS
849 #warning pretty ugly
850 #endif
851         static InsetFormulaBase * lastformula = 0;
852         static MathIterator current = MathIterator(ibegin(par().nucleus()));
853         static MathArray ar;
854         static string laststr;
855
856         if (lastformula != this || laststr != str) {
857                 //lyxerr << "reset lastformula to " << this << "\n";
858                 lastformula = this;
859                 laststr = str;
860                 current = ibegin(par().nucleus());
861                 ar.clear();
862                 mathed_parse_cell(ar, str);
863         } else {
864                 ++current;
865         }
866         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
867
868         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
869                 if (it.cell().matchpart(ar, it.position().pos_)) {
870                         bv->unlockInset(bv->theLockingInset());
871                         if (!bv->lockInset(this)) {
872                                 lyxerr << "Cannot lock inset" << endl;
873                                 return false;
874                         }
875                         delete mathcursor;
876                         mathcursor = new MathCursor(this, true);
877                         metrics(bv);
878                         mathcursor->setSelection(it.cursor(), ar.size());
879                         current = it;
880                         it.jump(ar.size());
881                         updateLocal(bv, false);
882                         return true;
883                 }
884         }
885
886         //lyxerr << "not found!\n";
887         lastformula = 0;
888         return false;
889 }
890
891
892 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
893                                       bool a, bool b)
894 {
895         lyxerr << "searching backward not implemented in mathed" << endl;
896         return searchForward(bv, what, a, b);
897 }
898
899
900 /////////////////////////////////////////////////////////////////////
901
902
903 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
904 {
905         if (bv->available()) {
906                 // use selection if available..
907                 //string sel;
908                 //if (action == LFUN_MATH_IMPORT_SELECTION)
909                 //      sel = "";
910                 //else
911
912                 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
913
914                 InsetFormulaBase * f;
915                 if (sel.empty()) {
916                         f = new InsetFormula;
917                         if (openNewInset(bv, f)) {
918                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
919                                 // always changing to mathrm when opening an inlined inset
920                                 // -- I really hate "LyXfunc overloading"...
921                                 if (display)
922                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
923                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
924                         }
925                 } else {
926                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
927                         // formula otherwise
928                         if (sel.find("\\newcommand") == string::npos &&
929                                   sel.find("\\def") == string::npos)
930                         {
931                                 f = new InsetFormula(sel);
932                         } else {
933                                 string name;
934                                 if (!mathed_parse_macro(name, sel))
935                                         return;
936                                 f = new InsetFormulaMacro(sel);
937                         }
938                         bv->getLyXText()->cutSelection(bv);
939                         openNewInset(bv, f);
940                 }
941         }
942         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
943 }
944
945
946 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
947 {
948         mathDispatchCreation(bv, arg, true);
949 }
950
951
952 void mathDispatchMathMode(BufferView * bv, string const & arg)
953 {
954         mathDispatchCreation(bv, arg, false);
955 }
956
957
958 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
959 {
960         mathDispatchCreation(bv, arg, true);
961 }
962
963
964 void mathDispatchMathMacro(BufferView * bv, string const & arg)
965 {
966         if (bv->available()) {
967                 if (arg.empty())
968                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
969                 else {
970                         string s = arg;
971                         string const s1 = token(s, ' ', 1);
972                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
973                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
974                 }
975         }
976 }
977
978
979 void mathDispatchMathDelim(BufferView * bv, string const & arg)
980 {
981         if (bv->available()) {
982                 if (openNewInset(bv, new InsetFormula))
983                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
984         }
985 }
986
987
988 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
989 {
990         if (bv->available()) {
991                 if (openNewInset(bv, new InsetFormula))
992                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
993         }
994 }
995
996
997 void mathDispatchInsertMath(BufferView * bv, string const & arg)
998 {
999         if (bv->available()) {
1000                 if (arg.size() && arg[0] == '\\') {
1001                         InsetFormula * f = new InsetFormula(arg);
1002                         if (!bv->insertInset(f))
1003                                 delete f;
1004                         else if (!mathcursor) // hotfix
1005                                 bv->getLyXText()->cursorRight(bv);
1006                 } else {
1007                         mathDispatchMathMode(bv, arg);
1008                 }
1009         }
1010 }
1011
1012
1013 void mathDispatchGreek(BufferView * bv, string const & arg)
1014 {
1015         if (bv->available()) {
1016                 InsetFormula * f = new InsetFormula;
1017                 if (openNewInset(bv, f)) {
1018                         bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1019                         bv->unlockInset(f);
1020                 }
1021         }
1022 }
1023
1024
1025 void mathDispatch(BufferView *, kb_action, string const &)
1026 {}
1027
1028
1029 void mathDispatch(BufferView *, string const &)
1030 {}