+ while (mathcursor->getLabel() == MathedXIter::error_label) {
+ if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
+ return DISPATCHED;
+ }
+ mathcursor->setNumbered();
+ UpdateLocal(bv);
+ }
+ break;
+
+ case LFUN_MATH_LIMITS:
+ bv->lockedInsetStoreUndo(Undo::INSERT);
+ if (mathcursor->Limits())
+ UpdateLocal(bv);
+ // fall through!
+
+ case LFUN_MATH_SIZE:
+ if (!arg.empty()) {
+ latexkeys const * l = in_word_set(arg);
+ int sz = (l) ? l->id: -1;
+ mathcursor->SetSize(sz);
+ UpdateLocal(bv);
+ break;
+ }
+ // possible fall through?
+
+ case LFUN_INSERT_MATH:
+ bv->lockedInsetStoreUndo(Undo::INSERT);
+ InsertSymbol(bv, arg);
+ break;
+
+
+ case LFUN_INSERT_MATRIX:
+ {
+ bv->lockedInsetStoreUndo(Undo::INSERT);
+ int k, m, n;
+ char s[80], arg2[80];
+ // This is just so that too long args won't ooze out of s.
+ strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
+ k = sscanf(arg2, "%d %d %s", &m, &n, s);
+ s[79] = '\0';
+
+ if (k < 1) {
+ m = n = 1;
+ } else if (k == 1) {
+ n = 1;
+ }
+
+ MathMatrixInset * p = new MathMatrixInset(m, n);
+ if (mathcursor && p) {
+ if (k > 2 && int(strlen(s)) > m)
+ p->SetAlign(s[0], &s[1]);
+ mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
+ UpdateLocal(bv);
+ }
+ break;
+ }
+
+ case LFUN_MATH_DELIM:
+ {
+ bv->lockedInsetStoreUndo(Undo::INSERT);
+ char lf[40], rg[40], arg2[40];
+ int ilf = '(', irg = '.';
+ latexkeys const * l;
+ string vdelim("(){}[]./|");
+
+ if (arg.empty())
+ break;
+ ::strncpy(arg2, arg.c_str(), 40);
+ arg2[39]= '\0';
+ int n = sscanf(arg2, "%s %s", lf, rg);
+ lf[39] = '\0';
+ rg[39] = '\0';
+
+ if (n > 0) {
+ if (isdigit(lf[0]))
+ ilf = lyx::atoi(lf);
+ else
+ if (lf[1]) {
+ l = in_word_set(lf, strlen(lf));
+ // Long words will cause l == 0; so check.
+ if (l)
+ ilf = l->id;
+ } else if (vdelim.find(lf[0]) != string::npos)
+ ilf = lf[0];
+
+ if (n > 1) {
+ if (isdigit(rg[0]))
+ irg = lyx::atoi(rg);
+ else
+ if (rg[1]) {
+ l = in_word_set(rg, strlen(rg));
+ if (l)
+ irg = l->id;
+ } else if (vdelim.find(rg[0]) != string::npos)
+ irg = rg[0];
+ }
+ }
+
+ MathDelimInset * p = new MathDelimInset(ilf, irg);
+ mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
+ UpdateLocal(bv);
+ break;
+ }
+
+ case LFUN_PROTECTEDSPACE:
+ bv->lockedInsetStoreUndo(Undo::INSERT);
+ sp = new MathSpaceInset(1);
+ mathcursor->insertInset(sp, LM_TC_INSET);
+ space_on = true;
+ UpdateLocal(bv);
+ break;
+
+ case LFUN_INSERT_LABEL:
+ {
+ bv->lockedInsetStoreUndo(Undo::INSERT);
+ if (par->GetType() < LM_OT_PAR)
+ break;
+
+ string old_label = is_multiline(par->GetType())
+ ? mathcursor->getLabel() : label;
+
+#warning This is a terrible hack! We should find a better solution.
+ // This is needed because in some positions
+ // mathcursor->cursor->crow is equal to 0, and therefore
+ // the label cannot be inserted.
+ // So we move the cursor left until
+ // mathcursor->cursor->crow != 0.
+ while (old_label == MathedXIter::error_label) {
+ if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
+ return DISPATCHED;
+ old_label = mathcursor->getLabel();
+ }
+
+ string new_label = arg;
+ if (new_label.empty()) {
+ string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
+ pair<bool, string> res = old_label.empty()
+ ? askForText(_("Enter new label to insert:"), default_label)
+ : askForText(_("Enter label:"), old_label);
+ if (!res.first)
+ break;
+ new_label = frontStrip(strip(res.second));
+ }
+
+ if (new_label == old_label)
+ break; // Nothing to do
+
+ if (!new_label.empty())
+ SetNumber(true);
+
+ if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
+ bv->redraw();
+
+ if (is_multi_numbered(par->GetType())) {
+ mathcursor->setLabel(new_label);
+ // MathMatrixInset *mt = (MathMatrixInset*)par;
+ // mt->SetLabel(new_label);
+ } else
+ label = new_label;
+
+ UpdateLocal(bv);
+ break;
+ }
+
+ case LFUN_MATH_DISPLAY:
+ bv->lockedInsetStoreUndo(Undo::EDIT);
+ display(!disp_flag);
+ UpdateLocal(bv);
+ break;
+
+ // Invalid actions under math mode
+ case LFUN_MATH_MODE:
+ if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
+ bv->owner()->getMiniBuffer()->Set(_("math text mode"));
+ varcode = LM_TC_TEXTRM;
+ } else {
+ varcode = LM_TC_VAR;
+ }
+ mathcursor->setLastCode(varcode);
+ break;
+
+ case LFUN_UNDO:
+ bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
+ break;
+
+ //------- dummy actions
+ case LFUN_EXEC_COMMAND:
+ bv->owner()->getMiniBuffer()->PrepareForCommand();
+ break;
+
+ default:
+ if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
+ unsigned char c = arg[0];
+ bv->lockedInsetStoreUndo(Undo::INSERT);
+
+ if (c == ' ' && mathcursor->getAccent() == LM_hat) {
+ c = '^';
+ mathcursor->setAccent(0);
+ }
+
+ if (c == 0) { // Dead key, do nothing
+ //lyxerr << "deadkey" << endl;
+ break;
+ }
+
+ if (isalpha(c)) {
+ if (mathcursor->getLastCode() == LM_TC_TEX) {
+ mathcursor->MacroModeOpen();
+ mathcursor->clearLastCode();
+ varcode = LM_TC_MIN;
+ } else if (!varcode) {
+ short f = (mathcursor->getLastCode()) ?
+ mathcursor->getLastCode() :
+ static_cast<MathedTextCodes>(mathcursor->GetFCode());
+ varcode = MathIsAlphaFont(f) ?
+ static_cast<MathedTextCodes>(f) :
+ LM_TC_VAR;
+ }
+
+ // lyxerr << "Varcode << vardoce;
+ MathedTextCodes char_code = varcode;
+ if (greek_kb_flag) {
+ char greek[26] =
+ {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
+ 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
+ 'Y', 0, 0, 0, 0 , 'Z' };
+
+ if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
+ char_code = LM_TC_RM;
+ c = greek[c - 'A'];
+ } else
+ char_code = LM_TC_SYMB;
+ }
+
+ mathcursor->Insert(c, char_code);
+
+ if (greek_kb_flag && char_code == LM_TC_RM )
+ mathcursor->setLastCode(LM_TC_VAR);
+
+ varcode = LM_TC_MIN;
+
+ if (greek_kb_flag<2)
+ greek_kb_flag = 0;
+
+ } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
+ mathcursor->Insert(c, LM_TC_TEX);
+ if (c == '{') {
+ mathcursor->Insert('}', LM_TC_TEX);
+ mathcursor->Left();
+ }
+ mathcursor->clearLastCode();
+ // varcode = LM_TC_MIN;
+ } else if (c == '_' && varcode == LM_TC_TEX) {
+ mathcursor->Insert(c, LM_TC_SPECIAL);
+ mathcursor->clearLastCode();
+ // varcode = LM_TC_MIN;
+ } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
+ mathcursor->MacroModeOpen();
+ mathcursor->clearLastCode();
+ mathcursor->Insert(c, LM_TC_MIN);
+ } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
+ mathcursor->Insert(c, LM_TC_CONST);
+ } else if (strchr("+/-*<>=", c)) {
+ mathcursor->Insert(c, LM_TC_BOP);
+ } else if (strchr(latex_special_chars, c) && c!= '_') {
+ mathcursor->Insert(c, LM_TC_SPECIAL);
+ } else if (c == '_' || c == '^') {
+ char s[2];
+ s[0] = c;
+ s[1] = 0;
+ mathcursor->Interpret(s);
+ } else if (c == ' ') {
+ if (!varcode) {
+ short f = (mathcursor->getLastCode()) ?
+ mathcursor->getLastCode() :
+ static_cast<MathedTextCodes>(mathcursor->GetFCode());
+ varcode = MathIsAlphaFont(f) ?
+ static_cast<MathedTextCodes>(f) :
+ LM_TC_VAR;
+ }
+
+ if (varcode == LM_TC_TEXTRM) {
+ mathcursor->Insert(c, LM_TC_TEXTRM);
+ } else if (was_macro) {
+ mathcursor->MacroModeClose();
+ } else if (sp) {
+ int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
+ sp->SetSpace(isp);
+ space_on = true;
+ } else if (!mathcursor->Pop() && mathcursor->IsEnd())
+ result = FINISHED;
+ } else if (c == '\'' || c == '@') {
+ mathcursor->Insert (c, LM_TC_VAR);
+ } else if (c == '\\') {
+ if (was_macro)
+ mathcursor->MacroModeClose();
+ bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
+ mathcursor->setLastCode(LM_TC_TEX);
+ }
+ UpdateLocal(bv);
+ } else if (action == LFUN_MATH_PANEL) {
+ result = UNDISPATCHED;
+ } else {
+ // lyxerr << "Closed by action " << action << endl;
+ result = FINISHED;
+ }