- Do not clear the initial selection to allow consecutive changes
  (this is in line with the behavior in texted)
- Make the color changes last so that the underline is the right color
- Allow to operate on selections that span multiple cells in a grid
- Use \boldsymbol instead of \mathbf to make everything bold
This commit is contained in:
Enrico Forestieri 2023-11-17 19:03:46 +01:00
parent ac0a12de68
commit ee19d67524

View File

@ -511,17 +511,15 @@ void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg) void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
{ {
bool include_previous_change = false;
cur.recordUndoSelection(); cur.recordUndoSelection();
bool include_previous_change = false;
bool selection = cur.selection();
DocIterator sel_begin = cur.selectionBegin();
DocIterator sel_end = cur.selectionEnd();
bool multiple_cells = sel_begin.idx() != sel_end.idx();
Font font; Font font;
bool b; bool b;
font.fromString(to_utf8(arg), b); font.fromString(to_utf8(arg), b);
if (font.fontInfo().color() != Color_inherit &&
font.fontInfo().color() != Color_ignore) {
handleNest(cur, MathAtom(new InsetMathColor(buffer_, true, font.fontInfo().color())));
include_previous_change = true;
}
docstring im; docstring im;
InsetMathFont const * f = asFontInset(); InsetMathFont const * f = asFontInset();
@ -559,13 +557,6 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
break; break;
} }
if (!im.empty()) { if (!im.empty()) {
if (include_previous_change) {
Cursor oldcur = cur;
cur.backwardInset();
cur.resetAnchor();
cur = oldcur;
cur.setSelection();
}
handleNest(cur, createInsetMath(im, cur.buffer())); handleNest(cur, createInsetMath(im, cur.buffer()));
im.clear(); im.clear();
include_previous_change = true; include_previous_change = true;
@ -580,18 +571,22 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
case BOLD_SERIES: case BOLD_SERIES:
if (!f || (f->name() != "textbf" && f->name() != "mathbf")) if (!f || (f->name() != "textbf" && f->name() != "mathbf"))
im = currentMode() != MATH_MODE ? from_ascii("textbf") im = currentMode() != MATH_MODE ? from_ascii("textbf")
: from_ascii("mathbf"); : from_ascii("boldsymbol");
break; break;
case INHERIT_SERIES: case INHERIT_SERIES:
case IGNORE_SERIES: case IGNORE_SERIES:
break; break;
} }
if (!im.empty()) { if (!im.empty()) {
if (include_previous_change) { if (multiple_cells) {
Cursor oldcur = cur; cur.setCursor(sel_begin);
cur.backwardInset(); cur.idx() = 0;
cur.pos() = 0;
cur.resetAnchor(); cur.resetAnchor();
cur = oldcur; cur.setCursor(sel_end);
cur.pos() = cur.lastpos();
cur.setSelection();
} else if (include_previous_change && selection) {
cur.setSelection(); cur.setSelection();
} }
handleNest(cur, createInsetMath(im, cur.buffer())); handleNest(cur, createInsetMath(im, cur.buffer()));
@ -613,23 +608,27 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
case SLANTED_SHAPE: case SLANTED_SHAPE:
if (!f || f->name() != "textsl") if (!f || f->name() != "textsl")
im = currentMode() != MATH_MODE ? from_ascii("textsl") im = currentMode() != MATH_MODE ? from_ascii("textsl")
: from_ascii("error"); : docstring();
break; break;
case SMALLCAPS_SHAPE: case SMALLCAPS_SHAPE:
if (!f || f->name() != "textsc") if (!f || f->name() != "textsc")
im = currentMode() != MATH_MODE ? from_ascii("textsc") im = currentMode() != MATH_MODE ? from_ascii("textsc")
: from_ascii("error"); : docstring();
break; break;
case INHERIT_SHAPE: case INHERIT_SHAPE:
case IGNORE_SHAPE: case IGNORE_SHAPE:
break; break;
} }
if (!im.empty() && im != "error") { if (!im.empty()) {
if (include_previous_change) { if (multiple_cells) {
Cursor oldcur = cur; cur.setCursor(sel_begin);
cur.backwardInset(); cur.idx() = 0;
cur.pos() = 0;
cur.resetAnchor(); cur.resetAnchor();
cur = oldcur; cur.setCursor(sel_end);
cur.pos() = cur.lastpos();
cur.setSelection();
} else if (include_previous_change && selection) {
cur.setSelection(); cur.setSelection();
} }
handleNest(cur, createInsetMath(im, cur.buffer())); handleNest(cur, createInsetMath(im, cur.buffer()));
@ -675,11 +674,15 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
break; break;
} }
if (!im.empty()) { if (!im.empty()) {
if (include_previous_change) { if (multiple_cells) {
Cursor oldcur = cur; cur.setCursor(sel_begin);
cur.backwardInset(); cur.idx() = 0;
cur.pos() = 0;
cur.resetAnchor(); cur.resetAnchor();
cur = oldcur; cur.setCursor(sel_end);
cur.pos() = cur.lastpos();
cur.setSelection();
} else if (include_previous_change && selection) {
cur.setSelection(); cur.setSelection();
} }
handleNest(cur, createInsetMath(im, cur.buffer())); handleNest(cur, createInsetMath(im, cur.buffer()));
@ -687,23 +690,22 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
include_previous_change = true; include_previous_change = true;
} }
InsetMathDecoration const * d = asDecorationInset(); InsetMathDecoration const * d = multiple_cells
? nullptr : asDecorationInset();
docstring const name = d ? d->name() : docstring(); docstring const name = d ? d->name() : docstring();
if ((font.fontInfo().underbar() == FONT_OFF && name == "uline") || if ((font.fontInfo().underbar() == FONT_OFF && name == "uline") ||
(font.fontInfo().uuline() == FONT_OFF && name == "uuline") || (font.fontInfo().uuline() == FONT_OFF && name == "uuline") ||
(font.fontInfo().uwave() == FONT_OFF && name == "uwave")) { (font.fontInfo().uwave() == FONT_OFF && name == "uwave")) {
if (include_previous_change) { if (include_previous_change) {
Cursor oldcur = cur; if (!selection)
cur.backwardInset(); cur.popForward();
cur.resetAnchor();
cur = oldcur;
cur.setSelection(); cur.setSelection();
} }
docstring const beg = '\\' + name + '{'; docstring const beg = '\\' + name + '{';
docstring const end = from_ascii("}"); docstring const end = from_ascii("}");
docstring const sel2 = cur.selectionAsString(false); docstring const sel2 = cur.selectionAsString(false);
cap::cutSelection(cur, false); cutSelection(cur, false);
cur.pos() = 0; cur.pos() = 0;
cur.setSelection(); cur.setSelection();
docstring const sel1 = cur.selectionAsString(false); docstring const sel1 = cur.selectionAsString(false);
@ -712,15 +714,17 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
docstring const sel3 = cur.selectionAsString(false); docstring const sel3 = cur.selectionAsString(false);
cur.mathForward(false); cur.mathForward(false);
cur.setSelection(); cur.setSelection();
cap::cutSelection(cur, false); cutSelection(cur, false);
MathData ar; MathData ar;
if (!sel1.empty()) { if (!sel1.empty()) {
mathed_parse_cell(ar, beg + sel1 + end); mathed_parse_cell(ar, beg + sel1 + end);
cur.insert(ar); cur.insert(ar);
} }
cur.resetAnchor(); cur.resetAnchor();
if (!sel2.empty()) {
mathed_parse_cell(ar, sel2); mathed_parse_cell(ar, sel2);
cur.insert(ar); cur.insert(ar);
}
if (!sel3.empty()) { if (!sel3.empty()) {
pos_type pos = cur.pos(); pos_type pos = cur.pos();
mathed_parse_cell(ar, beg + sel3 + end); mathed_parse_cell(ar, beg + sel3 + end);
@ -728,6 +732,8 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
cur.pos() = pos; cur.pos() = pos;
} }
cur.setSelection(); cur.setSelection();
sel_begin = cur.selectionBegin();
sel_end = cur.selectionEnd();
include_previous_change = false; include_previous_change = false;
} }
@ -743,11 +749,15 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
} }
if (!im.empty()) { if (!im.empty()) {
if (include_previous_change) { if (multiple_cells) {
Cursor oldcur = cur; cur.setCursor(sel_begin);
cur.backwardInset(); cur.idx() = 0;
cur.pos() = 0;
cur.resetAnchor(); cur.resetAnchor();
cur = oldcur; cur.setCursor(sel_end);
cur.pos() = cur.lastpos();
cur.setSelection();
} else if (include_previous_change && selection) {
cur.setSelection(); cur.setSelection();
} }
handleNest(cur, createInsetMath(im, cur.buffer())); handleNest(cur, createInsetMath(im, cur.buffer()));
@ -755,6 +765,45 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
include_previous_change = true; include_previous_change = true;
} }
if (font.fontInfo().color() != Color_inherit &&
font.fontInfo().color() != Color_ignore) {
if (multiple_cells) {
cur.setCursor(sel_begin);
cur.idx() = 0;
cur.pos() = 0;
cur.resetAnchor();
cur.setCursor(sel_end);
cur.pos() = cur.lastpos();
cur.setSelection();
} else if (include_previous_change && selection) {
cur.setSelection();
}
handleNest(cur, MathAtom(new InsetMathColor(buffer_, true, font.fontInfo().color())));
include_previous_change = true;
}
if (selection) {
if (multiple_cells) {
cur.setCursor(sel_begin);
cur.idx() = 0;
cur.pos() = 0;
cur.resetAnchor();
cur.setCursor(sel_end);
cur.pos() = cur.lastpos();
cur.setSelection();
} else {
if (include_previous_change) {
sel_end = cur;
cur.backwardInset();
} else {
cur.setCursor(sel_begin);
}
cur.resetAnchor();
cur.setCursor(sel_end);
cur.setSelection();
}
}
// FIXME: support other font changes here as well? // FIXME: support other font changes here as well?
} }