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