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