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