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