+bool reduceSelectionToOneCell(Cursor & cur)
+{
+ if (!cur.selection() || !cur.inMathed())
+ return false;
+
+ CursorSlice i1 = cur.selBegin();
+ CursorSlice i2 = cur.selEnd();
+ if (!i1.inset().asInsetMath())
+ return false;
+
+ // the easy case: do nothing if only one cell is selected
+ if (i1.idx() == i2.idx())
+ return true;
+
+ cur.top().pos() = 0;
+ cur.resetAnchor();
+ cur.top().pos() = cur.top().lastpos();
+
+ return true;
+}
+
+
void switchBetweenClasses(DocumentClass const * const oldone,
DocumentClass const * const newone, InsetText & in, ErrorList & errorlist)
{
void switchBetweenClasses(DocumentClass const * const oldone,
DocumentClass const * const newone, InsetText & in, ErrorList & errorlist)
{
if (!cur.selection())
return docstring();
if (!cur.selection())
return docstring();
- // FIXME: What is wrong with the following?
+ // grab selection by glueing multiple cells together. This is not what
+ // we want because selections spanning multiple cells will get "&" and "\\"
+ // seperators.
ostringstream os;
for (DocIterator dit = cur.selectionBegin();
dit != cur.selectionEnd(); dit.forwardPos())
ostringstream os;
for (DocIterator dit = cur.selectionBegin();
dit != cur.selectionEnd(); dit.forwardPos())
/// Does not handle undo. Does only work if the whole selection is in mathed.
/// Calls saveSelection.
void eraseSelection(Cursor & cur);
/// Does not handle undo. Does only work if the whole selection is in mathed.
/// Calls saveSelection.
void eraseSelection(Cursor & cur);
+/// Reduce the selected text in mathed to only one cell. If it spans multiple
+/// cells, the cursor is moved the end of the current cell and the anchor to the
+/// start. If the selection is inside only one cell, nothing is done. Return
+/// true if the selection now does not span multiple cells anymore.
+bool reduceSelectionToOneCell(Cursor & cur);
/// Erase the selection and return it as a string.
/// Does not handle undo. Does only work if the whole selection is in mathed.
docstring grabAndEraseSelection(Cursor & cur);
/// Erase the selection and return it as a string.
/// Does not handle undo. Does only work if the whole selection is in mathed.
docstring grabAndEraseSelection(Cursor & cur);
if (c == '\\') {
//lyxerr << "starting with macro" << endl;
if (c == '\\') {
//lyxerr << "starting with macro" << endl;
- docstring const safe = cap::grabAndEraseSelection(cur);
- cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
+ bool reduced = cap::reduceSelectionToOneCell(cur);
+ if (reduced || !cur.selection()) {
+ docstring const safe = cap::grabAndEraseSelection(cur);
+ cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
+ }