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