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