From b2e8d557cc58e2ff05412f8cf058aaf06e52f503 Mon Sep 17 00:00:00 2001 From: Stefan Schimanski Date: Wed, 12 Mar 2008 00:18:39 +0000 Subject: [PATCH] * only use selection of current cell when starting a macro (fixes http://bugzilla.lyx.org/show_bug.cgi?id=4566 for macros) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23670 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/CutAndPaste.cpp | 26 +++++++++++++++++++++++++- src/CutAndPaste.h | 5 +++++ src/mathed/InsetMathNest.cpp | 7 +++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index bd0b3baaa7..98abfe1e98 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -434,6 +434,28 @@ docstring grabAndEraseSelection(Cursor & cur) } +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) { @@ -932,8 +954,10 @@ docstring grabSelection(Cursor const & cur) if (!cur.selection()) return docstring(); - // FIXME: What is wrong with the following? #if 0 + // 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()) diff --git a/src/CutAndPaste.h b/src/CutAndPaste.h index 3796966f09..e16d437734 100644 --- a/src/CutAndPaste.h +++ b/src/CutAndPaste.h @@ -116,6 +116,11 @@ docstring grabSelection(Cursor const & 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); diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index 9d173aac87..6895f2eeb1 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -1415,8 +1415,11 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type c) 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))); + } return true; }