]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
don't draw spaces...
[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 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         mathcursor->touch();
396
397         switch (action) {
398
399                 // --- Cursor Movements ---------------------------------------------
400
401         case LFUN_RIGHTSEL:
402                 sel = true; // fall through...
403
404         case LFUN_RIGHT:
405                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
406                 //lyxerr << "calling scroll 20\n";
407                 //scroll(bv, 20);
408                 updateLocal(bv, false);
409                 // write something to the minibuffer
410                 //bv->owner()->message(mathcursor->info());
411                 break;
412
413
414         case LFUN_LEFTSEL:
415                 sel = true; // fall through
416
417         case LFUN_LEFT:
418                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
419                 updateLocal(bv, false);
420                 break;
421
422
423         case LFUN_UPSEL:
424                 sel = true;
425
426         case LFUN_UP:
427                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
428                 updateLocal(bv, false);
429                 break;
430
431
432         case LFUN_DOWNSEL:
433                 sel = true;
434
435         case LFUN_DOWN:
436                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
437                 updateLocal(bv, false);
438                 break;
439
440         case LFUN_HOMESEL:
441                 sel = true;
442
443         case LFUN_HOME:
444                 mathcursor->home(sel);
445                 updateLocal(bv, false);
446                 break;
447
448         case LFUN_ENDSEL:
449                 sel = true;
450
451         case LFUN_END:
452                 mathcursor->end(sel);
453                 updateLocal(bv, false);
454                 break;
455
456         case LFUN_DELETE_LINE_FORWARD:
457                 bv->lockedInsetStoreUndo(Undo::DELETE);
458                 mathcursor->delLine();
459                 updateLocal(bv, true);
460                 break;
461
462         case LFUN_TAB:
463                 mathcursor->idxNext();
464                 updateLocal(bv, false);
465                 break;
466
467         case LFUN_SHIFT_TAB:
468                 mathcursor->idxPrev();
469                 updateLocal(bv, false);
470                 break;
471
472         case LFUN_TABINSERT:
473                 bv->lockedInsetStoreUndo(Undo::EDIT);
474                 mathcursor->splitCell();
475                 updateLocal(bv, true);
476                 break;
477
478         case LFUN_DELETE_WORD_BACKWARD:
479         case LFUN_BACKSPACE:
480                 bv->lockedInsetStoreUndo(Undo::DELETE);
481                 mathcursor->backspace();
482                 bv->updateInset(this, true);
483                 break;
484
485         case LFUN_DELETE_WORD_FORWARD:
486         case LFUN_DELETE:
487                 bv->lockedInsetStoreUndo(Undo::DELETE);
488                 mathcursor->erase();
489                 bv->updateInset(this, true);
490                 break;
491
492         //    case LFUN_GETXY:
493         //      sprintf(dispatch_buffer, "%d %d",);
494         //      dispatch_result = dispatch_buffer;
495         //      break;
496         case LFUN_SETXY: {
497                 lyxerr << "LFUN_SETXY broken!\n";
498                 int x = 0;
499                 int y = 0;
500                 istringstream is(arg.c_str());
501                 is >> x >> y;
502                 mathcursor->setPos(x, y);
503                 updateLocal(bv, false);
504                 break;
505         }
506
507         case LFUN_PASTE:
508                 if (was_macro)
509                         mathcursor->macroModeClose();
510                 bv->lockedInsetStoreUndo(Undo::EDIT);
511                 mathcursor->selPaste();
512                 updateLocal(bv, true);
513                 break;
514
515         case LFUN_CUT:
516                 bv->lockedInsetStoreUndo(Undo::DELETE);
517                 mathcursor->selCut();
518                 updateLocal(bv, true);
519                 break;
520
521         case LFUN_COPY:
522                 mathcursor->selCopy();
523                 break;
524
525         case LFUN_WORDRIGHTSEL:
526         case LFUN_WORDLEFTSEL:
527                 break;
528
529         // Special casing for superscript in case of LyX handling
530         // dead-keys:
531         case LFUN_CIRCUMFLEX:
532                 if (arg.empty()) {
533                         // do superscript if LyX handles
534                         // deadkeys
535                         bv->lockedInsetStoreUndo(Undo::EDIT);
536                         mathcursor->script(true);
537                         updateLocal(bv, true);
538                 }
539                 break;
540         case LFUN_UMLAUT:
541         case LFUN_ACUTE:
542         case LFUN_GRAVE:
543         case LFUN_BREVE:
544         case LFUN_DOT:
545         case LFUN_MACRON:
546         case LFUN_CARON:
547         case LFUN_TILDE:
548         case LFUN_CEDILLA:
549         case LFUN_CIRCLE:
550         case LFUN_UNDERDOT:
551         case LFUN_TIE:
552         case LFUN_OGONEK:
553         case LFUN_HUNG_UMLAUT:
554                 break;
555
556         //  Math fonts
557         case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
558         case LFUN_BOLD:         handleFont(bv, arg, LM_TC_BF); break;
559         case LFUN_SANS:         handleFont(bv, arg, LM_TC_SF); break;
560         case LFUN_EMPH:         handleFont(bv, arg, LM_TC_CAL); break;
561         case LFUN_ROMAN:        handleFont(bv, arg, LM_TC_RM); break;
562         case LFUN_CODE:         handleFont(bv, arg, LM_TC_TT); break;
563         case LFUN_FRAK:         handleFont(bv, arg, LM_TC_EUFRAK); break;
564         case LFUN_ITAL:         handleFont(bv, arg, LM_TC_IT); break;
565         case LFUN_NOUN:         handleFont(bv, arg, LM_TC_BB); break;
566         case LFUN_DEFAULT:      handleFont(bv, arg, LM_TC_VAR); break;
567         case LFUN_FREE:         handleFont(bv, arg, LM_TC_TEXTRM); break;
568
569         case LFUN_GREEK: 
570                 handleFont(bv, arg, LM_TC_GREEK1);
571                 if (arg.size())
572                         mathcursor->interpret(arg);
573                 break;
574
575         case LFUN_MATH_MODE:
576 #if 1
577                 handleFont(bv, arg, LM_TC_TEXTRM);
578 #endif
579
580 #if 0
581                 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
582                 updateLocal(bv, true);
583 #endif
584
585                 //bv->owner()->message(_("math text mode toggled"));
586                 break;
587
588         case LFUN_MATH_LIMITS:
589                 bv->lockedInsetStoreUndo(Undo::EDIT);
590                 if (mathcursor->toggleLimits())
591                         updateLocal(bv, true);
592                 break;
593
594         case LFUN_MATH_SIZE:
595 #if 0
596                 if (!arg.empty()) {
597                         bv->lockedInsetStoreUndo(Undo::EDIT);
598                         mathcursor->setSize(arg);
599                         updateLocal(bv, true);
600                 }
601 #endif
602                 break;
603
604         case LFUN_INSERT_MATRIX:
605                 if (!arg.empty()) {
606                         bv->lockedInsetStoreUndo(Undo::EDIT);
607                         mathcursor->interpret("matrix " + arg);
608                         updateLocal(bv, true);
609                 }
610                 break;
611
612         case LFUN_MATH_SPACE:
613         {
614                 bv->lockedInsetStoreUndo(Undo::EDIT);
615                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
616                 updateLocal(bv, true);
617                 break;
618         }
619         
620         case LFUN_SUPERSCRIPT:
621         case LFUN_SUBSCRIPT:
622         {
623                 bv->lockedInsetStoreUndo(Undo::EDIT);
624                 mathcursor->script((action == LFUN_SUPERSCRIPT));
625                 updateLocal(bv, true);
626                 break;
627         }
628         
629         case LFUN_MATH_DELIM:
630         {
631                 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
632                 string ls;
633                 string rs;
634                 istringstream is(arg.c_str());
635                 is >> ls >> rs;
636                 if (!is) {
637                         lyxerr << "can't parse delimeters from '" << arg << "'\n";
638                         break;
639                 }
640                 bv->lockedInsetStoreUndo(Undo::EDIT);
641                 mathcursor->handleDelim(ls, rs);
642                 updateLocal(bv, true);
643                 break;
644         }
645
646         case LFUN_PROTECTEDSPACE:
647                 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
648                 bv->lockedInsetStoreUndo(Undo::EDIT);
649                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
650                 updateLocal(bv, true);
651                 break;
652
653         case LFUN_UNDO:
654                 bv->owner()->message(_("Invalid action in math mode!"));
655                 break;
656
657
658         case LFUN_MATH_HALIGN:
659         case LFUN_MATH_VALIGN:
660         case LFUN_MATH_ROW_INSERT:
661         case LFUN_MATH_ROW_DELETE:
662         case LFUN_MATH_COLUMN_INSERT:
663         case LFUN_MATH_COLUMN_DELETE:
664         {
665                 MathInset::idx_type idx = 0;
666                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
667                 if (p) {
668                         bv->lockedInsetStoreUndo(Undo::EDIT);
669                         char al = arg.size() ? arg[0] : 'c';
670                         switch (action) {
671                                 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
672                                 case LFUN_MATH_VALIGN: p->valign(al); break;
673                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
674                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
675                                 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
676                                 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
677                                 default: ;
678                         }
679                         updateLocal(bv, true);
680                 }
681                 break;
682         }
683
684         case LFUN_EXEC_COMMAND:
685                 result = UNDISPATCHED;
686                 break;
687
688         case LFUN_BREAKPARAGRAPH:
689         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
690                 //lyxerr << "LFUN ignored\n";
691                 break;
692
693         case LFUN_INSET_ERT:
694                 // interpret this as if a backslash was typed
695                 bv->lockedInsetStoreUndo(Undo::EDIT);
696                 mathcursor->interpret("\\");
697                 updateLocal(bv, true);
698                 break;
699
700         case -1:
701         case LFUN_INSERT_MATH:
702         case LFUN_SELFINSERT:
703                 if (!arg.empty()) {
704                         bv->lockedInsetStoreUndo(Undo::EDIT);
705                         result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
706                         updateLocal(bv, true);
707                 }
708                 break;
709
710         case LFUN_MATH_PANEL:
711                 result = UNDISPATCHED;
712                 break;
713
714         case LFUN_ESCAPE:
715                 if (mathcursor->selection())
716                         mathcursor->selClear();
717                 else
718                         result = UNDISPATCHED;
719                 break;
720
721         default:
722                 result = UNDISPATCHED;
723         }
724
725         mathcursor->normalize();
726         mathcursor->touch();
727
728         lyx::Assert(mathcursor);
729
730         if (mathcursor->selection() || was_selection)
731                 toggleInsetSelection(bv);
732
733         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
734             result == UNDISPATCHED)
735                 showInsetCursor(bv);
736         else
737                 bv->unlockInset(this);
738
739         revealCodes(bv);
740
741         return result;  // original version
742 }
743
744
745 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
746 {
747         if (!mathcursor)
748                 return;
749 #if 0
750         // write something to the minibuffer
751         // translate to latex
752         mathcursor->markInsert();
753         ostringstream os;
754         write(NULL, os);
755         string str = os.str();
756         mathcursor->markErase();
757         string::size_type pos = 0;
758         string res;
759         for (string::iterator it = str.begin(); it != str.end(); ++it) {
760                 if (*it == '\n')
761                         res += ' ';
762                 else if (*it == '\0') {
763                         res += "  -X-  ";
764                         pos = it - str.begin();
765                 }
766                 else
767                         res += *it;
768         }
769         if (pos > 30)
770                 res = res.substr(pos - 30);
771         if (res.size() > 60)
772                 res = res.substr(0, 60);
773         bv->owner()->message(res);
774 #endif
775 }
776
777
778 Inset::Code InsetFormulaBase::lyxCode() const
779 {
780         return Inset::MATH_CODE;
781 }
782
783
784 int InsetFormulaBase::ylow() const
785 {
786         return yo_ - ascent(view_, font_);
787 }
788
789
790 int InsetFormulaBase::yhigh() const
791 {
792         return yo_ + descent(view_, font_);
793 }
794
795
796 int InsetFormulaBase::xlow() const
797 {
798         return xo_;
799 }
800
801
802 int InsetFormulaBase::xhigh() const
803 {
804         return xo_ + width(view_, font_);
805 }
806
807
808 /////////////////////////////////////////////////////////////////////
809
810
811 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
812                                      bool, bool)
813 {
814 #ifdef WITH_WARNINGS
815 #warning pretty ugly
816 #endif
817         static InsetFormulaBase * lastformula = 0;
818         static MathIterator current = MathIterator(ibegin(par().nucleus()));
819         static MathArray ar;
820         static string laststr;
821
822         if (lastformula != this || laststr != str) {
823                 //lyxerr << "reset lastformula to " << this << "\n";
824                 lastformula = this;
825                 laststr = str;
826                 current = ibegin(par().nucleus());
827                 ar.clear();
828                 mathed_parse_cell(ar, str);
829         } else {
830                 ++current;
831         }
832         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
833
834         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
835                 if (it.cell().matchpart(ar, it.position().pos_)) {
836                         mathcursor->setSelection(it.cursor(), ar.size());
837                         current = it;
838                         it.jump(ar.size());
839                         // I guess some of the following can go
840                         bv->toggleSelection(true);
841                         hideInsetCursor(bv);
842                         updateLocal(bv, true);
843                         showInsetCursor(bv);
844                         metrics(bv);
845                         return true;
846                 }
847         }
848
849         //lyxerr << "not found!\n";
850         lastformula = 0;
851         // we have to unlock ourself in this function by default!
852         // don't ask me why...
853         bv->unlockInset(this);
854         return false;
855 }
856
857
858 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
859                                       bool a, bool b)
860 {
861         lyxerr << "searching backward not implemented in mathed" << endl;
862         return searchForward(bv, what, a, b);
863 }
864
865
866 /////////////////////////////////////////////////////////////////////
867
868
869 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
870 {
871         if (bv->available()) {
872                 // use selection if available..
873                 //string sel;
874                 //if (action == LFUN_MATH_IMPORT_SELECTION)
875                 //      sel = "";
876                 //else
877
878                 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
879
880                 InsetFormulaBase * f;
881                 if (sel.empty()) {
882                         f = new InsetFormula;
883                         if (openNewInset(bv, f)) {
884                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
885                                 // always changing to mathrm when opening an inlined inset
886                                 // -- I really hate "LyXfunc overloading"...
887                                 if (display)
888                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
889                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
890                         }
891                 } else {
892                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
893                         // formula otherwise
894                         if (sel.find("\\newcommand") == string::npos) 
895                                 f = new InsetFormula(sel);
896                         else {
897                                 string name;
898                                 if (!mathed_parse_macro(name, sel))
899                                         return;
900                                 f = new InsetFormulaMacro(sel);
901                         }
902                         bv->getLyXText()->cutSelection(bv);
903                         openNewInset(bv, f);
904                 }
905         }
906         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
907 }
908
909
910 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
911 {
912         mathDispatchCreation(bv, arg, true);
913 }
914
915         
916 void mathDispatchMathMode(BufferView * bv, string const & arg)
917 {
918         mathDispatchCreation(bv, arg, false);
919 }
920
921
922 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
923 {
924         mathDispatchCreation(bv, arg, true);
925 }
926
927
928 void mathDispatchMathMacro(BufferView * bv, string const & arg)
929 {
930         if (bv->available()) {
931                 if (arg.empty())
932                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
933                 else {
934                         string s(arg);
935                         string const s1 = token(s, ' ', 1);
936                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
937                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
938                 }
939         }
940 }
941
942
943 void mathDispatchMathDelim(BufferView * bv, string const & arg)
944 {
945         if (bv->available()) { 
946                 if (openNewInset(bv, new InsetFormula))
947                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
948         }
949 }          
950
951
952 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
953 {          
954         if (bv->available()) { 
955                 if (openNewInset(bv, new InsetFormula))
956                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
957         }
958 }          
959
960
961 void mathDispatchInsertMath(BufferView * bv, string const & arg)
962 {
963         if (bv->available()) {
964                 if (arg.size() && arg[0] == '\\') {
965                         InsetFormula * f = new InsetFormula(arg);
966                         if (!bv->insertInset(f))
967                                 delete f;
968                 } else
969                         mathDispatchMathMode(bv, arg);
970         }
971 }
972
973
974 void mathDispatchGreek(BufferView * bv, string const & arg)
975 {          
976         if (bv->available()) { 
977                 InsetFormula * f = new InsetFormula;
978                 if (openNewInset(bv, f)) {
979                         bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
980                         bv->unlockInset(f);
981                 }
982         }
983 }          
984
985
986 void mathDispatch(BufferView *, kb_action, string const &)
987 {}