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