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