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