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